From 719ecc9e85e593ac5fc2aeb0a20eb51da8b8e6d7 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Fri, 11 Aug 2023 10:23:58 +0200 Subject: [PATCH] [PR #7085/a8809401 backport][stable-7] Avoid direct type comparisons (#7086) Avoid direct type comparisons (#7085) Avoid direct type comparisons. (cherry picked from commit a8809401ee61ad18ffee59c500b9b660511fb21e) Co-authored-by: Felix Fontein --- changelogs/fragments/7085-sanity.yml | 3 +++ plugins/module_utils/oracle/oci_utils.py | 22 +++++++++---------- plugins/modules/proxmox_user_info.py | 6 ++--- tests/unit/mock/loader.py | 2 +- .../plugins/modules/test_keycloak_user.py | 2 +- 5 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 changelogs/fragments/7085-sanity.yml diff --git a/changelogs/fragments/7085-sanity.yml b/changelogs/fragments/7085-sanity.yml new file mode 100644 index 0000000000..53fb245e3f --- /dev/null +++ b/changelogs/fragments/7085-sanity.yml @@ -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)." diff --git a/plugins/module_utils/oracle/oci_utils.py b/plugins/module_utils/oracle/oci_utils.py index 3d9c20f2ac..9d8e16e5a3 100644 --- a/plugins/module_utils/oracle/oci_utils.py +++ b/plugins/module_utils/oracle/oci_utils.py @@ -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 @@ -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: @@ -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)) @@ -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: @@ -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] @@ -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( @@ -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( diff --git a/plugins/modules/proxmox_user_info.py b/plugins/modules/proxmox_user_info.py index a515f2b453..20154528a6 100644 --- a/plugins/modules/proxmox_user_info.py +++ b/plugins/modules/proxmox_user_info.py @@ -193,14 +193,14 @@ class ProxmoxUser: 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 diff --git a/tests/unit/mock/loader.py b/tests/unit/mock/loader.py index 948f4eecd9..f7aff17c32 100644 --- a/tests/unit/mock/loader.py +++ b/tests/unit/mock/loader.py @@ -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__() diff --git a/tests/unit/plugins/modules/test_keycloak_user.py b/tests/unit/plugins/modules/test_keycloak_user.py index f5a4e26f8a..26bc33d826 100644 --- a/tests/unit/plugins/modules/test_keycloak_user.py +++ b/tests/unit/plugins/modules/test_keycloak_user.py @@ -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