From 6cfb44b4bbe71f848886aea6a207b2f017d6368c Mon Sep 17 00:00:00 2001 From: jctanner Date: Wed, 9 Nov 2016 13:46:50 -0500 Subject: [PATCH] replace type() with isinstance() (#3404) Replace use of type() with isinstance() Addresses https://github.com/ansible/ansible/issues/18310 --- lib/ansible/modules/extras/cloud/misc/virt.py | 6 +++--- .../modules/extras/cloud/misc/virt_net.py | 6 +++--- .../modules/extras/cloud/misc/virt_pool.py | 10 +++++----- .../modules/extras/cloud/vmware/vmware_guest.py | 16 ++++++++-------- .../modules/extras/database/misc/redis.py | 2 +- .../modules/extras/system/osx_defaults.py | 2 +- lib/ansible/modules/extras/system/zfs.py | 2 +- 7 files changed, 22 insertions(+), 22 deletions(-) mode change 100755 => 100644 lib/ansible/modules/extras/cloud/misc/virt_net.py mode change 100755 => 100644 lib/ansible/modules/extras/cloud/misc/virt_pool.py diff --git a/lib/ansible/modules/extras/cloud/misc/virt.py b/lib/ansible/modules/extras/cloud/misc/virt.py index 68ff4a6c6cb..912eb3b9add 100644 --- a/lib/ansible/modules/extras/cloud/misc/virt.py +++ b/lib/ansible/modules/extras/cloud/misc/virt.py @@ -430,7 +430,7 @@ def core(module): if state and command=='list_vms': res = v.list_vms(state=state) - if type(res) != dict: + if not isinstance(res, dict): res = { command: res } return VIRT_SUCCESS, res @@ -477,13 +477,13 @@ def core(module): res = {'changed': True, 'created': guest} return VIRT_SUCCESS, res res = getattr(v, command)(guest) - if type(res) != dict: + if not isinstance(res, dict): res = { command: res } return VIRT_SUCCESS, res elif hasattr(v, command): res = getattr(v, command)() - if type(res) != dict: + if not isinstance(res, dict): res = { command: res } return VIRT_SUCCESS, res diff --git a/lib/ansible/modules/extras/cloud/misc/virt_net.py b/lib/ansible/modules/extras/cloud/misc/virt_net.py old mode 100755 new mode 100644 index 29cb43c3b97..469603d1aeb --- a/lib/ansible/modules/extras/cloud/misc/virt_net.py +++ b/lib/ansible/modules/extras/cloud/misc/virt_net.py @@ -466,7 +466,7 @@ def core(module): if state and command == 'list_nets': res = v.list_nets(state=state) - if type(res) != dict: + if not isinstance(res, dict): res = { command: res } return VIRT_SUCCESS, res @@ -523,13 +523,13 @@ def core(module): res = {'changed': mod, 'modified': name} return VIRT_SUCCESS, res res = getattr(v, command)(name) - if type(res) != dict: + if not isinstance(res, dict): res = { command: res } return VIRT_SUCCESS, res elif hasattr(v, command): res = getattr(v, command)() - if type(res) != dict: + if not isinstance(res, dict): res = { command: res } return VIRT_SUCCESS, res diff --git a/lib/ansible/modules/extras/cloud/misc/virt_pool.py b/lib/ansible/modules/extras/cloud/misc/virt_pool.py old mode 100755 new mode 100644 index 83b0cda310f..11e3447048c --- a/lib/ansible/modules/extras/cloud/misc/virt_pool.py +++ b/lib/ansible/modules/extras/cloud/misc/virt_pool.py @@ -561,7 +561,7 @@ def core(module): if state and command == 'list_pools': res = v.list_pools(state=state) - if type(res) != dict: + if not isinstance(res, dict): res = { command: res } return VIRT_SUCCESS, res @@ -623,22 +623,22 @@ def core(module): return VIRT_SUCCESS, res elif command == 'build': res = v.build(name, mode) - if type(res) != dict: + if not isinstance(res, dict): res = { 'changed': True, command: res } return VIRT_SUCCESS, res elif command == 'delete': res = v.delete(name, mode) - if type(res) != dict: + if not isinstance(res, dict): res = { 'changed': True, command: res } return VIRT_SUCCESS, res res = getattr(v, command)(name) - if type(res) != dict: + if not isinstance(res, dict): res = { command: res } return VIRT_SUCCESS, res elif hasattr(v, command): res = getattr(v, command)() - if type(res) != dict: + if not isinstance(res, dict): res = { command: res } return VIRT_SUCCESS, res diff --git a/lib/ansible/modules/extras/cloud/vmware/vmware_guest.py b/lib/ansible/modules/extras/cloud/vmware/vmware_guest.py index 7a25bba2508..4757a3c18b2 100644 --- a/lib/ansible/modules/extras/cloud/vmware/vmware_guest.py +++ b/lib/ansible/modules/extras/cloud/vmware/vmware_guest.py @@ -199,13 +199,13 @@ class PyVmomiHelper(object): for child in children: if child == folder or child in tree: continue - if type(child) == vim.Folder: + if isinstance(child, vim.Folder): ctree = self._build_folder_tree(child) tree['subfolders'][child] = dict.copy(ctree) - elif type(child) == vim.VirtualMachine: + elif isinstance(child, vim.VirtualMachine): tree['virtualmachines'].append(child) else: - if type(folder) == vim.VirtualMachine: + if isinstance(folder, vim.VirtualMachine): return folder return tree @@ -214,7 +214,7 @@ class PyVmomiHelper(object): ''' Build a searchable index for vms+uuids+folders ''' - if type(folder) == tuple: + if isinstance(folder, tuple): folder = folder[1] if not 'names' in vmap: @@ -284,13 +284,13 @@ class PyVmomiHelper(object): def compile_folder_path_for_object(self, vobj): ''' make a /vm/foo/bar/baz like folder path for an object ''' paths = [] - if type(vobj) == vim.Folder: + if isinstance(vobj, vim.Folder): paths.append(vobj.name) thisobj = vobj while hasattr(thisobj, 'parent'): thisobj = thisobj.parent - if type(thisobj) == vim.Folder: + if isinstance(thisobj, vim.Folder): paths.append(thisobj.name) paths.reverse() if paths[0] == 'Datacenters': @@ -343,7 +343,7 @@ class PyVmomiHelper(object): if isinstance(fObj, vim.Datacenter): fObj = fObj.vmFolder for cObj in fObj.childEntity: - if not type(cObj) == vim.VirtualMachine: + if not isinstance(cObj, vim.VirtualMachine): continue if cObj.name == name: vm = cObj @@ -362,7 +362,7 @@ class PyVmomiHelper(object): # compare the folder path of each VM against the search path for item in vmList.items(): vobj = item[0] - if not type(vobj.parent) == vim.Folder: + if not isinstance(vobj.parent, vim.Folder): continue if self.compile_folder_path_for_object(vobj) == searchpath: return vobj diff --git a/lib/ansible/modules/extras/database/misc/redis.py b/lib/ansible/modules/extras/database/misc/redis.py index 00c3478d117..396a7abe94f 100644 --- a/lib/ansible/modules/extras/database/misc/redis.py +++ b/lib/ansible/modules/extras/database/misc/redis.py @@ -149,7 +149,7 @@ def set_master_mode(client): def flush(client, db=None): try: - if type(db) != int: + if not isinstance(db, int): return client.flushall() else: # The passed client has been connected to the database already diff --git a/lib/ansible/modules/extras/system/osx_defaults.py b/lib/ansible/modules/extras/system/osx_defaults.py index 5c1444bc0a7..5a9fcf25b77 100644 --- a/lib/ansible/modules/extras/system/osx_defaults.py +++ b/lib/ansible/modules/extras/system/osx_defaults.py @@ -233,7 +233,7 @@ class OSXDefaults(object): def write(self): # We need to convert some values so the defaults commandline understands it - if type(self.value) is bool: + if isinstance(self.value, bool): if self.value: value = "TRUE" else: diff --git a/lib/ansible/modules/extras/system/zfs.py b/lib/ansible/modules/extras/system/zfs.py index 0d79569d77a..1a1bad4a0f9 100644 --- a/lib/ansible/modules/extras/system/zfs.py +++ b/lib/ansible/modules/extras/system/zfs.py @@ -226,7 +226,7 @@ def main(): # All freestyle params are zfs properties if prop not in module.argument_spec: # Reverse the boolification of freestyle zfs properties - if type(value) == bool: + if isinstance(value, bool): if value is True: properties[prop] = 'on' else: