type(foo) == bar -> isinstance(foo, bar)

Changed type checks in most places.
This commit is contained in:
Todd Gamblin 2013-12-09 00:47:17 -08:00
parent fa2e8dab11
commit 936f54761b
5 changed files with 21 additions and 19 deletions

View file

@ -61,7 +61,7 @@ def sys_type():
if sys_type == None: if sys_type == None:
raise NoSysTypeError() raise NoSysTypeError()
if not type(sys_type) == str: if not isinstance(sys_type, basestring):
raise InvalidSysTypeError(sys_type) raise InvalidSysTypeError(sys_type)
return sys_type return sys_type

View file

@ -300,7 +300,7 @@ def __init__(self, spec):
except UndetectableVersionError: except UndetectableVersionError:
tty.die("Couldn't extract a default version from %s. You " + tty.die("Couldn't extract a default version from %s. You " +
"must specify it explicitly in the package." % self.url) "must specify it explicitly in the package." % self.url)
elif type(self.version) != Version: elif not isinstance(self.version, Version):
self.version = Version(self.version) self.version = Version(self.version)
# This is set by scraping a web page. # This is set by scraping a web page.
@ -308,7 +308,7 @@ def __init__(self, spec):
# This list overrides available_versions if set by the user. # This list overrides available_versions if set by the user.
attr_setdefault(self, 'versions', None) attr_setdefault(self, 'versions', None)
if self.versions and type(self.versions) != VersionList: if self.versions and not isinstance(self.versions, VersionList):
self.versions = VersionList(self.versions) self.versions = VersionList(self.versions)
# Empty at first; only compute dependent packages if necessary # Empty at first; only compute dependent packages if necessary

View file

@ -97,11 +97,11 @@ def _parse_local_spec(spec_like, pkg_name):
e.g., provides('mpi@2', when='@1.9:') says that this package provides e.g., provides('mpi@2', when='@1.9:') says that this package provides
MPI-3 when its version is higher than 1.9. MPI-3 when its version is higher than 1.9.
""" """
if type(spec_like) not in (str, Spec): if not isinstance(spec_like, (str, Spec)):
raise TypeError('spec must be Spec or spec string. Found %s' raise TypeError('spec must be Spec or spec string. Found %s'
% type(spec_like)) % type(spec_like))
if type(spec_like) == str: if isinstance(spec_like, str):
try: try:
local_spec = Spec(spec_like) local_spec = Spec(spec_like)
except spack.parse.ParseError: except spack.parse.ParseError:

View file

@ -246,12 +246,12 @@ def __str__(self):
class Spec(object): class Spec(object):
def __init__(self, spec_like, *dep_like): def __init__(self, spec_like, *dep_like):
# Copy if spec_like is a Spec. # Copy if spec_like is a Spec.
if type(spec_like) == Spec: if isinstance(spec_like, Spec):
self._dup(spec_like) self._dup(spec_like)
return return
# Parse if the spec_like is a string. # Parse if the spec_like is a string.
if type(spec_like) != str: if not isinstance(spec_like, basestring):
raise TypeError("Can't make spec out of %s" % type(spec_like)) raise TypeError("Can't make spec out of %s" % type(spec_like))
spec_list = SpecParser().parse(spec_like) spec_list = SpecParser().parse(spec_like)
@ -277,7 +277,7 @@ def __init__(self, spec_like, *dep_like):
# Note that given two specs a and b, Spec(a) copies a, but # Note that given two specs a and b, Spec(a) copies a, but
# Spec(a, b) will copy a but just add b as a dep. # Spec(a, b) will copy a but just add b as a dep.
for dep in dep_like: for dep in dep_like:
spec = dep if type(dep) == Spec else Spec(dep) spec = dep if isinstance(dep, Spec) else Spec(dep)
self._add_dependency(spec) self._add_dependency(spec)
@ -632,6 +632,9 @@ def normalize(self):
package that provides a virtual package that is in the spec, package that provides a virtual package that is in the spec,
then we replace the virtual package with the non-virtual one. then we replace the virtual package with the non-virtual one.
4) The spec DAG matches package DAG. 4) The spec DAG matches package DAG.
TODO: normalize should probably implement some form of cycle detection,
to ensure that the spec is actually a DAG.
""" """
# Ensure first that all packages in the DAG exist. # Ensure first that all packages in the DAG exist.
self.validate_package_names() self.validate_package_names()
@ -699,7 +702,7 @@ def constrain(self, other):
def satisfies(self, other): def satisfies(self, other):
if type(other) != Spec: if not isinstance(other, Spec):
other = Spec(other) other = Spec(other)
def sat(attribute): def sat(attribute):
@ -772,7 +775,7 @@ def __getitem__(self, name):
def __contains__(self, spec): def __contains__(self, spec):
"""True if this spec has any dependency that satisfies the supplied """True if this spec has any dependency that satisfies the supplied
spec.""" spec."""
if type(spec) != Spec: if not isinstance(spec, Spec):
spec = Spec(spec) spec = Spec(spec)
for s in self.preorder_traversal(): for s in self.preorder_traversal():

View file

@ -240,9 +240,9 @@ def intersection(self, other):
@total_ordering @total_ordering
class VersionRange(object): class VersionRange(object):
def __init__(self, start, end): def __init__(self, start, end):
if type(start) == str: if isinstance(start, basestring):
start = Version(start) start = Version(start)
if type(end) == str: if isinstance(end, basestring):
end = Version(end) end = Version(end)
self.start = start self.start = start
@ -347,7 +347,7 @@ class VersionList(object):
def __init__(self, vlist=None): def __init__(self, vlist=None):
self.versions = [] self.versions = []
if vlist != None: if vlist != None:
if type(vlist) == str: if isinstance(vlist, basestring):
vlist = _string_to_version(vlist) vlist = _string_to_version(vlist)
if type(vlist) == VersionList: if type(vlist) == VersionList:
self.versions = vlist.versions self.versions = vlist.versions
@ -542,14 +542,13 @@ def ver(obj):
"""Parses a Version, VersionRange, or VersionList from a string """Parses a Version, VersionRange, or VersionList from a string
or list of strings. or list of strings.
""" """
t = type(obj) if isinstance(obj, (list, tuple)):
if t == list:
return VersionList(obj) return VersionList(obj)
elif t == str: elif isinstance(obj, basestring):
return _string_to_version(obj) return _string_to_version(obj)
elif t in (int, float): elif isinstance(obj, (int, float)):
return _string_to_version(str(obj)) return _string_to_version(str(obj))
elif t in (Version, VersionRange, VersionList): elif type(obj) in (Version, VersionRange, VersionList):
return obj return obj
else: else:
raise TypeError("ver() can't convert %s to version!" % t) raise TypeError("ver() can't convert %s to version!" % type(obj))