Skip to content

Commit

Permalink
replace type() with isinstance() (ansible#5541)
Browse files Browse the repository at this point in the history
Replace all use of type() with isintance()

Addresses ansible#18310
  • Loading branch information
jctanner authored and mattclay committed Dec 8, 2016
1 parent 2744fde commit 06e1141
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 17 deletions.
14 changes: 7 additions & 7 deletions lib/ansible/modules/cloud/amazon/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ def find_running_instances_by_count_tag(module, ec2, count_tag, zone=None):
def _set_none_to_blank(dictionary):
result = dictionary
for k in result.iterkeys():
if type(result[k]) == dict:
if isinstance(result[k], dict):
result[k] = _set_none_to_blank(result[k])
elif not result[k]:
result[k] = ""
Expand All @@ -626,27 +626,27 @@ def get_reservations(module, ec2, tags=None, state=None, zone=None):

if tags is not None:

if type(tags) is str:
if isinstance(tags, str):
try:
tags = literal_eval(tags)
except:
pass

# if string, we only care that a tag of that name exists
if type(tags) is str:
if isinstance(tags, str):
filters.update({"tag-key": tags})

# if list, append each item to filters
if type(tags) is list:
if isinstance(tags, list):
for x in tags:
if type(x) is dict:
if isinstance(x, dict):
x = _set_none_to_blank(x)
filters.update(dict(("tag:"+tn, tv) for (tn,tv) in x.iteritems()))
else:
filters.update({"tag-key": x})

# if dict, add the key and value to the filter
if type(tags) is dict:
if isinstance(tags, dict):
tags = _set_none_to_blank(tags)
filters.update(dict(("tag:"+tn, tv) for (tn,tv) in tags.iteritems()))

Expand Down Expand Up @@ -906,7 +906,7 @@ def enforce_count(module, ec2, vpc):
# ensure all instances are dictionaries
all_instances = []
for inst in instances:
if type(inst) is not dict:
if not isinstance(inst, dict):
inst = get_instance_info(inst)
all_instances.append(inst)

Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/modules/cloud/amazon/rds_param_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def set_parameter(param, value, immediate):
# may be based on a variable (ie. {foo*3/4}) so
# just pass it on through to boto
converted_value = str(value)
elif type(value) == bool:
elif isinstance(value, bool):
converted_value = 1 if value else 0
else:
converted_value = int(value)
Expand Down
4 changes: 2 additions & 2 deletions lib/ansible/modules/cloud/amazon/route53.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,10 +450,10 @@ def main():

value_list = ()

if type(value_in) is str:
if isinstance(value_in, str):
if value_in:
value_list = sorted([s.strip() for s in value_in.split(',')])
elif type(value_in) is list:
elif isinstance(value_in, list):
value_list = sorted(value_in)

if zone_in[-1:] != '.':
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/modules/cloud/azure/azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ def __getattr__(self, name):
raise AttributeError(name)

def _wrap(self, func, args, kwargs):
if type(func) == MethodType:
if isinstance(func, MethodType):
result = self._handle_temporary_redirects(lambda: func(*args, **kwargs))
else:
result = self._handle_temporary_redirects(lambda: func(self.other, *args, **kwargs))
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/modules/cloud/docker/_docker.py
Original file line number Diff line number Diff line change
Expand Up @@ -1307,7 +1307,7 @@ def get_differing_containers(self):
for name, value in self.module.params.get('labels').iteritems():
expected_labels[name] = str(value)

if type(container['Config']['Labels']) is dict:
if isinstance(container['Config']['Labels'], dict):
actual_labels = container['Config']['Labels']
else:
for container_label in container['Config']['Labels'] or []:
Expand Down
8 changes: 4 additions & 4 deletions lib/ansible/modules/cloud/openstack/os_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def _exit_hostvars(module, cloud, server, changed=True):

def _parse_nics(nics):
for net in nics:
if type(net) == str:
if isinstance(net, str):
for nic in net.split(','):
yield dict((nic.split('='),))
else:
Expand All @@ -398,11 +398,11 @@ def _network_args(module, cloud):
args = []
nics = module.params['nics']

if type(nics) != list:
if not isinstance(nics, list):
module.fail_json(msg='The \'nics\' parameter must be a list.')

for net in _parse_nics(nics):
if type(net) != dict:
if not isinstance(net, dict):
module.fail_json(
msg='Each entry in the \'nics\' parameter must be a dict.')

Expand Down Expand Up @@ -459,7 +459,7 @@ def _create_server(module, cloud):

nics = _network_args(module, cloud)

if type(module.params['meta']) is str:
if isinstance(module.params['meta'], str):
metas = {}
for kv_str in module.params['meta'].split(","):
k, v = kv_str.split("=")
Expand Down
2 changes: 1 addition & 1 deletion lib/ansible/modules/utilities/logic/async_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def _run_module(wrapped_cmd, jid, job_path):
if json_warnings:
# merge JSON junk warnings with any existing module warnings
module_warnings = result.get('warnings', [])
if type(module_warnings) is not list:
if not isinstance(module_warnings, list):
module_warnings = [module_warnings]
module_warnings.extend(json_warnings)
result['warnings'] = module_warnings
Expand Down

0 comments on commit 06e1141

Please sign in to comment.