Skip to content

Commit

Permalink
[PR #7085/a8809401 backport][stable-7] Avoid direct type comparisons (#…
Browse files Browse the repository at this point in the history
…7086)

Avoid direct type comparisons (#7085)

Avoid direct type comparisons.

(cherry picked from commit a880940)

Co-authored-by: Felix Fontein <[email protected]>
  • Loading branch information
patchback[bot] and felixfontein authored Aug 11, 2023
1 parent 1a80132 commit 719ecc9
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
3 changes: 3 additions & 0 deletions changelogs/fragments/7085-sanity.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bugfixes:
- "oci_utils module utils - avoid direct type comparisons (https://github.com/ansible-collections/community.general/pull/7085)."
- "proxmox_user_info - avoid direct type comparisons (https://github.com/ansible-collections/community.general/pull/7085)."
22 changes: 11 additions & 11 deletions plugins/module_utils/oracle/oci_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ def are_lists_equal(s, t):
s = to_dict(s)
t = to_dict(t)

if type(s[0]) == dict:
if isinstance(s[0], dict):
# Handle list of dicts. Dictionary returned by the API may have additional keys. For example, a get call on
# service gateway has an attribute `services` which is a list of `ServiceIdResponseDetails`. This has a key
# `service_name` which is not provided in the list of `services` by a user while making an update call; only
Expand Down Expand Up @@ -604,9 +604,9 @@ def get_attr_to_update(get_fn, kwargs_get, module, update_attributes):
user_provided_attr_value = module.params.get(attr, None)

unequal_list_attr = (
type(resources_attr_value) == list or type(user_provided_attr_value) == list
isinstance(resources_attr_value, list) or isinstance(user_provided_attr_value, list)
) and not are_lists_equal(user_provided_attr_value, resources_attr_value)
unequal_attr = type(resources_attr_value) != list and to_dict(
unequal_attr = not isinstance(resources_attr_value, list) and to_dict(
resources_attr_value
) != to_dict(user_provided_attr_value)
if unequal_list_attr or unequal_attr:
Expand Down Expand Up @@ -936,9 +936,9 @@ def tuplize(d):
list_of_tuples = []
key_list = sorted(list(d.keys()))
for key in key_list:
if type(d[key]) == list:
if isinstance(d[key], list):
# Convert a value which is itself a list of dict to a list of tuples.
if d[key] and type(d[key][0]) == dict:
if d[key] and isinstance(d[key][0], dict):
sub_tuples = []
for sub_dict in d[key]:
sub_tuples.append(tuplize(sub_dict))
Expand All @@ -948,7 +948,7 @@ def tuplize(d):
list_of_tuples.append((sub_tuples is None, key, sub_tuples))
else:
list_of_tuples.append((d[key] is None, key, d[key]))
elif type(d[key]) == dict:
elif isinstance(d[key], dict):
tupled_value = tuplize(d[key])
list_of_tuples.append((tupled_value is None, key, tupled_value))
else:
Expand All @@ -969,13 +969,13 @@ def sort_dictionary(d):
"""
sorted_d = {}
for key in d:
if type(d[key]) == list:
if d[key] and type(d[key][0]) == dict:
if isinstance(d[key], list):
if d[key] and isinstance(d[key][0], dict):
sorted_value = sort_list_of_dictionary(d[key])
sorted_d[key] = sorted_value
else:
sorted_d[key] = sorted(d[key])
elif type(d[key]) == dict:
elif isinstance(d[key], dict):
sorted_d[key] = sort_dictionary(d[key])
else:
sorted_d[key] = d[key]
Expand Down Expand Up @@ -1044,7 +1044,7 @@ def check_if_user_value_matches_resources_attr(

if (
user_provided_value_for_attr
and type(user_provided_value_for_attr[0]) == dict
and isinstance(user_provided_value_for_attr[0], dict)
):
# Process a list of dict
sorted_user_provided_value_for_attr = sort_list_of_dictionary(
Expand Down Expand Up @@ -1547,7 +1547,7 @@ def delete_and_wait(
except ServiceError as ex:
# DNS API throws a 400 InvalidParameter when a zone id is provided for zone_name_or_id and if the zone
# resource is not available, instead of the expected 404. So working around this for now.
if type(client) == oci.dns.DnsClient:
if isinstance(client, oci.dns.DnsClient):
if ex.status == 400 and ex.code == "InvalidParameter":
_debug(
"Resource {0} with {1} already deleted. So returning changed=False".format(
Expand Down
6 changes: 3 additions & 3 deletions plugins/modules/proxmox_user_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,14 +193,14 @@ def __init__(self, user):
self.user[k] = v
elif k in ['groups', 'tokens'] and (v == '' or v is None):
self.user[k] = []
elif k == 'groups' and type(v) == str:
elif k == 'groups' and isinstance(v, str):
self.user['groups'] = v.split(',')
elif k == 'tokens' and type(v) == list:
elif k == 'tokens' and isinstance(v, list):
for token in v:
if 'privsep' in token:
token['privsep'] = proxmox_to_ansible_bool(token['privsep'])
self.user['tokens'] = v
elif k == 'tokens' and type(v) == dict:
elif k == 'tokens' and isinstance(v, dict):
self.user['tokens'] = list()
for tokenid, tokenvalues in v.items():
t = tokenvalues
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/mock/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class DictDataLoader(DataLoader):

def __init__(self, file_mapping=None):
file_mapping = {} if file_mapping is None else file_mapping
assert type(file_mapping) == dict
assert isinstance(file_mapping, dict)

super(DictDataLoader, self).__init__()

Expand Down
2 changes: 1 addition & 1 deletion tests/unit/plugins/modules/test_keycloak_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def patch_keycloak_api(get_user_by_username=None,
with patch.object(obj, 'get_user_groups', side_effect=get_user_groups) as mock_get_user_groups:
with patch.object(obj, 'delete_user', side_effect=delete_user) as mock_delete_user:
with patch.object(obj, 'update_user', side_effect=update_user) as mock_update_user:
yield mock_get_user_by_username, mock_create_user, mock_update_user_groups_membership,\
yield mock_get_user_by_username, mock_create_user, mock_update_user_groups_membership, \
mock_get_user_groups, mock_delete_user, mock_update_user


Expand Down

0 comments on commit 719ecc9

Please sign in to comment.