1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Avoid direct type comparisons (#7085)

Avoid direct type comparisons.
This commit is contained in:
Felix Fontein 2023-08-11 09:00:54 +02:00 committed by GitHub
parent 2089769ccc
commit a8809401ee
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 19 additions and 16 deletions

View file

@ -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)."

View file

@ -570,7 +570,7 @@ def are_lists_equal(s, t):
s = to_dict(s) s = to_dict(s)
t = to_dict(t) 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 # 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 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 # `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) user_provided_attr_value = module.params.get(attr, None)
unequal_list_attr = ( 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) ) 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 resources_attr_value
) != to_dict(user_provided_attr_value) ) != to_dict(user_provided_attr_value)
if unequal_list_attr or unequal_attr: if unequal_list_attr or unequal_attr:
@ -936,9 +936,9 @@ def tuplize(d):
list_of_tuples = [] list_of_tuples = []
key_list = sorted(list(d.keys())) key_list = sorted(list(d.keys()))
for key in key_list: 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. # 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 = [] sub_tuples = []
for sub_dict in d[key]: for sub_dict in d[key]:
sub_tuples.append(tuplize(sub_dict)) sub_tuples.append(tuplize(sub_dict))
@ -948,7 +948,7 @@ def tuplize(d):
list_of_tuples.append((sub_tuples is None, key, sub_tuples)) list_of_tuples.append((sub_tuples is None, key, sub_tuples))
else: else:
list_of_tuples.append((d[key] is None, key, d[key])) 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]) tupled_value = tuplize(d[key])
list_of_tuples.append((tupled_value is None, key, tupled_value)) list_of_tuples.append((tupled_value is None, key, tupled_value))
else: else:
@ -969,13 +969,13 @@ def sort_dictionary(d):
""" """
sorted_d = {} sorted_d = {}
for key in d: for key in d:
if type(d[key]) == list: if isinstance(d[key], list):
if d[key] and type(d[key][0]) == dict: if d[key] and isinstance(d[key][0], dict):
sorted_value = sort_list_of_dictionary(d[key]) sorted_value = sort_list_of_dictionary(d[key])
sorted_d[key] = sorted_value sorted_d[key] = sorted_value
else: else:
sorted_d[key] = sorted(d[key]) sorted_d[key] = sorted(d[key])
elif type(d[key]) == dict: elif isinstance(d[key], dict):
sorted_d[key] = sort_dictionary(d[key]) sorted_d[key] = sort_dictionary(d[key])
else: else:
sorted_d[key] = d[key] sorted_d[key] = d[key]
@ -1044,7 +1044,7 @@ def check_if_user_value_matches_resources_attr(
if ( if (
user_provided_value_for_attr 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 # Process a list of dict
sorted_user_provided_value_for_attr = sort_list_of_dictionary( sorted_user_provided_value_for_attr = sort_list_of_dictionary(
@ -1547,7 +1547,7 @@ def delete_and_wait(
except ServiceError as ex: 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 # 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. # 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": if ex.status == 400 and ex.code == "InvalidParameter":
_debug( _debug(
"Resource {0} with {1} already deleted. So returning changed=False".format( "Resource {0} with {1} already deleted. So returning changed=False".format(

View file

@ -193,14 +193,14 @@ class ProxmoxUser:
self.user[k] = v self.user[k] = v
elif k in ['groups', 'tokens'] and (v == '' or v is None): elif k in ['groups', 'tokens'] and (v == '' or v is None):
self.user[k] = [] self.user[k] = []
elif k == 'groups' and type(v) == str: elif k == 'groups' and isinstance(v, str):
self.user['groups'] = v.split(',') self.user['groups'] = v.split(',')
elif k == 'tokens' and type(v) == list: elif k == 'tokens' and isinstance(v, list):
for token in v: for token in v:
if 'privsep' in token: if 'privsep' in token:
token['privsep'] = proxmox_to_ansible_bool(token['privsep']) token['privsep'] = proxmox_to_ansible_bool(token['privsep'])
self.user['tokens'] = v self.user['tokens'] = v
elif k == 'tokens' and type(v) == dict: elif k == 'tokens' and isinstance(v, dict):
self.user['tokens'] = list() self.user['tokens'] = list()
for tokenid, tokenvalues in v.items(): for tokenid, tokenvalues in v.items():
t = tokenvalues t = tokenvalues

View file

@ -17,7 +17,7 @@ class DictDataLoader(DataLoader):
def __init__(self, file_mapping=None): def __init__(self, file_mapping=None):
file_mapping = {} if file_mapping is None else file_mapping file_mapping = {} if file_mapping is None else file_mapping
assert type(file_mapping) == dict assert isinstance(file_mapping, dict)
super(DictDataLoader, self).__init__() super(DictDataLoader, self).__init__()