From 273009232c3fb0a0e6bf5b3e87ddfc9a07ac39d3 Mon Sep 17 00:00:00 2001 From: Marco Clemencic <marco.clemencic@cern.ch> Date: Wed, 13 Sep 2023 14:33:03 +0200 Subject: [PATCH] Fix flake8 E721 violations (do not compare types, use 'isinstance()') --- Gaudi/python/Gaudi/Main.py | 5 ++--- GaudiAlg/python/GaudiAlg/HistoUtils.py | 8 ++++---- .../tests/python/test_sequence_semantics.py | 2 +- GaudiKernel/python/GaudiConfig/ControlFlow.py | 2 +- GaudiKernel/python/GaudiKernel/Configurable.py | 4 ++-- .../python/GaudiKernel/ConfigurableMeta.py | 2 +- GaudiKernel/python/GaudiKernel/GaudiHandles.py | 16 +++++++--------- .../python/GaudiKernel/ProcessJobOptions.py | 4 ++-- GaudiKernel/python/GaudiKernel/PropertyProxy.py | 6 +++--- GaudiPython/python/GaudiPython/Bindings.py | 11 +++++------ 10 files changed, 28 insertions(+), 32 deletions(-) diff --git a/Gaudi/python/Gaudi/Main.py b/Gaudi/python/Gaudi/Main.py index 9a60cfafa4..2b7d1b1c1d 100644 --- a/Gaudi/python/Gaudi/Main.py +++ b/Gaudi/python/Gaudi/Main.py @@ -213,9 +213,8 @@ def _getAllOpts_old(explicit_defaults=False): ) for p, v in items: # Note: AthenaCommon.Configurable does not have Configurable.PropertyReference - if ( - hasattr(Configurable, "PropertyReference") - and type(v) == Configurable.PropertyReference + if hasattr(Configurable, "PropertyReference") and isinstance( + v, Configurable.PropertyReference ): # this is done in "getFullName", but the exception is ignored, # so we do it again to get it diff --git a/GaudiAlg/python/GaudiAlg/HistoUtils.py b/GaudiAlg/python/GaudiAlg/HistoUtils.py index 6b73e7ff0b..be40b02f1d 100644 --- a/GaudiAlg/python/GaudiAlg/HistoUtils.py +++ b/GaudiAlg/python/GaudiAlg/HistoUtils.py @@ -472,17 +472,17 @@ def fill( """ # if value is a string, try to get the objects from TES - if type(data) == str: + if isinstance(data, str): svc = _getEvtSvc(**kwargs) data = svc[data] return fill(histo, data, fun, cut, **kwargs) # if the function is a string: evaluate it! - if type(fun) == str: + if isinstance(fun, str): fun = eval(fun, globals()) # if the criterion is a string: evaluate it! - if type(cut) == str: + if isinstance(cut, str): cut = eval(cut, globals()) if not hasattr(data, "__iter__"): @@ -1052,7 +1052,7 @@ class HistoFile: a list of them. """ if args: - if type(args[0]) == list: + if isinstance(args[0], list): histoList = args[0] else: histoList = args diff --git a/GaudiConfiguration/tests/python/test_sequence_semantics.py b/GaudiConfiguration/tests/python/test_sequence_semantics.py index 82b5efc9f5..84bb179ecd 100644 --- a/GaudiConfiguration/tests/python/test_sequence_semantics.py +++ b/GaudiConfiguration/tests/python/test_sequence_semantics.py @@ -150,4 +150,4 @@ def testValueSem(): from GaudiConfig2.semantics import FloatSemantics, SequenceSemantics s = SequenceSemantics("std::vector<int>", None, FloatSemantics("float", None)) - assert type(s.value_semantics) == FloatSemantics + assert isinstance(s.value_semantics, FloatSemantics) diff --git a/GaudiKernel/python/GaudiConfig/ControlFlow.py b/GaudiKernel/python/GaudiConfig/ControlFlow.py index 178090fac6..29c19d02a8 100644 --- a/GaudiKernel/python/GaudiConfig/ControlFlow.py +++ b/GaudiKernel/python/GaudiConfig/ControlFlow.py @@ -298,7 +298,7 @@ class DotVisitor(object): if len(self.stack) != 0: mother = self.stack[-1][1] for entry in self.stack[::-1]: - if type(entry[0]) != thetype: + if not isinstance(entry[0], thetype): break mother = entry[1] return mother diff --git a/GaudiKernel/python/GaudiKernel/Configurable.py b/GaudiKernel/python/GaudiKernel/Configurable.py index d6c2debb84..8f5764800b 100644 --- a/GaudiKernel/python/GaudiKernel/Configurable.py +++ b/GaudiKernel/python/GaudiKernel/Configurable.py @@ -214,7 +214,7 @@ class Configurable(six.with_metaclass(ConfigurableMeta.ConfigurableMeta, object) name = cls.DefaultedName else: name = cls.getType() - elif not name or type(name) != str: + elif not name or not isinstance(name, str): # unnamed, highly specialized user code, etc. ... unacceptable raise TypeError( "could not retrieve name from %s.__init__ arguments" % cls.__name__ @@ -552,7 +552,7 @@ class Configurable(six.with_metaclass(ConfigurableMeta.ConfigurableMeta, object) __nonzero__ = __bool__ def remove(self, items): - if type(items) != list and type(items) != tuple: + if not isinstance(items, (list, tuple)): items = [items] self.__children = [e for e in self.__children if e not in items] diff --git a/GaudiKernel/python/GaudiKernel/ConfigurableMeta.py b/GaudiKernel/python/GaudiKernel/ConfigurableMeta.py index fb42966564..cbb669e472 100644 --- a/GaudiKernel/python/GaudiKernel/ConfigurableMeta.py +++ b/GaudiKernel/python/GaudiKernel/ConfigurableMeta.py @@ -51,7 +51,7 @@ class ConfigurableMeta(type): propDict = dct.get("_propertyDocDct") for prop in props: docString = propDict and propDict.get(prop) - if type(slots) == dict: + if isinstance(slots, dict): default = slots[prop] else: default = None diff --git a/GaudiKernel/python/GaudiKernel/GaudiHandles.py b/GaudiKernel/python/GaudiKernel/GaudiHandles.py index 4b064497a1..0ad3274ad3 100644 --- a/GaudiKernel/python/GaudiKernel/GaudiHandles.py +++ b/GaudiKernel/python/GaudiKernel/GaudiHandles.py @@ -33,7 +33,7 @@ class GaudiHandle(object): if hasattr(typeAndName, "toStringProperty"): # this is a GaudiHandle or equivalent typeAndName = typeAndName.toStringProperty() - if type(typeAndName) != str: + if not isinstance(typeAndName, str): raise TypeError( "Argument to %s must be a string. Got a %s instead" % (self.__class__.__name__, type(typeAndName).__name__) @@ -131,7 +131,7 @@ class GaudiHandleArray(list): typesAndNames = [] list.__init__(self) # check the type - if type(typesAndNames) != list: + if not isinstance(typesAndNames, list): raise TypeError( "Argument to %s must be a list. Got a %s instead" % (self.__class__.__name__, type(typesAndNames).__name__) @@ -157,7 +157,7 @@ class GaudiHandleArray(list): # linesep + linesep.join([str(s) for s in self])) def __getitem__(self, index): - if type(index) == str: + if isinstance(index, str): # seach by instance name for h in self: if h.getName() == index: @@ -173,24 +173,22 @@ class GaudiHandleArray(list): super(GaudiHandleArray, self).__delitem__(self.index(self[key])) def __iadd__(self, array): - arrayType = type(array) - if arrayType == list or arrayType == type(self): + if isinstance(array, (list, type(self))): for v in array: self.append(v) else: raise TypeError( - "Can not add a %s to a %s" - % (arrayType.__name__, self.__class__.__name__) + "Can not add a %s to a %s" % (type(array).__name__, type(self).__name__) ) return self def append(self, value): """Only allow appending compatible types. It accepts a string, a handle or a configurable.""" - if type(value) == str: + if isinstance(value, str): # convert string to handle value = self.__class__.handleType(value) - elif type(value) == self.__class__.handleType: + elif isinstance(value, self.__class__.handleType): pass # leave handle as-is elif isinstance(value, GaudiHandle): # do not allow different type of handles diff --git a/GaudiKernel/python/GaudiKernel/ProcessJobOptions.py b/GaudiKernel/python/GaudiKernel/ProcessJobOptions.py index 6036709f90..d8aae15c08 100644 --- a/GaudiKernel/python/GaudiKernel/ProcessJobOptions.py +++ b/GaudiKernel/python/GaudiKernel/ProcessJobOptions.py @@ -438,7 +438,7 @@ class JobOptsParser: if inc == "+": if hasattr(cfg, property): prop = getattr(cfg, property) - if type(prop) == dict: + if isinstance(prop, dict): for k in value: prop[k] = value[k] else: @@ -448,7 +448,7 @@ class JobOptsParser: elif inc == "-": if hasattr(cfg, property): prop = getattr(cfg, property) - if type(prop) is dict: + if isinstance(prop, dict): for k in value: if k in prop: del prop[k] diff --git a/GaudiKernel/python/GaudiKernel/PropertyProxy.py b/GaudiKernel/python/GaudiKernel/PropertyProxy.py index 54cb1aaaba..11779d3885 100644 --- a/GaudiKernel/python/GaudiKernel/PropertyProxy.py +++ b/GaudiKernel/python/GaudiKernel/PropertyProxy.py @@ -267,7 +267,7 @@ class GaudiHandlePropertyProxyBase(PropertyProxy): def convertDefaultToBeSet(self, obj, default): # turn string into handle - isString = type(default) == str + isString = isinstance(default, str) if not isString and self.isConfig(default): # print self.fullPropertyName(obj) + ": Setting default configurable: %r" % default return default @@ -319,7 +319,7 @@ class GaudiHandlePropertyProxyBase(PropertyProxy): def convertValueToBeSet(self, obj, value): if value is None: value = "" - isString = type(value) == str + isString = isinstance(value, str) if isString: # create an new handle return self._handleType(value) @@ -455,7 +455,7 @@ class DataHandlePropertyProxy(PropertyProxy): mode = obj.__class__.getDefaultProperty(self.descr.__name__).mode() _type = obj.__class__.getDefaultProperty(self.descr.__name__).type() - if type(value) == str: + if isinstance(value, str): return DataHandle(value, mode, _type) elif isinstance(value, DataHandle): return DataHandle(value.__str__(), mode, _type) diff --git a/GaudiPython/python/GaudiPython/Bindings.py b/GaudiPython/python/GaudiPython/Bindings.py index 1e986c603d..320edf3a89 100644 --- a/GaudiPython/python/GaudiPython/Bindings.py +++ b/GaudiPython/python/GaudiPython/Bindings.py @@ -316,7 +316,7 @@ class iProperty(object): value = str(value.toStringProperty()) elif hasattr(value, "toString"): value = str(value.toString()) - elif type(value) == long: + elif isinstance(value, long): value = "%d" % value # prevent pending 'L' else: value = str(value) @@ -971,16 +971,15 @@ class AppMgr(iService): for p, v in c.getValuedProperties().items(): v = expandvars(v) # Note: AthenaCommon.Configurable does not have Configurable.PropertyReference - if ( - hasattr(Configurable, "PropertyReference") - and type(v) == Configurable.PropertyReference + if hasattr(Configurable, "PropertyReference") and isinstance( + v, Configurable.PropertyReference ): # this is done in "getFullName", but the exception is ignored, # so we do it again to get it v = v.__resolve__() - if type(v) == str: + if isinstance(v, str): v = repr(v) # need double quotes - if type(v) == long: + if isinstance(v, long): v = "%d" % v # prevent pending 'L' gbl.GaudiPython.Helpers.setProperty( self._svcloc, ".".join([n, p]), str(v) -- GitLab