I'm currently in pdb trace to figure this out
ipdb> isinstance(var, Type)
False
ipdb> type(var)
<class 'module.Type'>
ipdb> Type
<class 'module.Type'>
Why can this happen?
P. S. isinstance(var, type(var))
returns True
as expected
I'm currently in pdb trace to figure this out
ipdb> isinstance(var, Type)
False
ipdb> type(var)
<class 'module.Type'>
ipdb> Type
<class 'module.Type'>
Why can this happen?
P. S. isinstance(var, type(var))
returns True
as expected
I can only guess, but if you do in module
class Type(object): pass
var = Type()
class Type(object): pass
then both types look like <class 'module.Type'>
, but are nevertheless different.
You could check that with
print(id(Type), id(var.__class__))
or with
print(Type is var.__class__)
Note that these comparisons work for both old-, and new-style classes.
For new-style classes, they are equivalent to print(Type is type(var))
.
But this is not the case for old-style classes.
Another quite common trap is that you call this module with
python -m module
or
python module.py
making it known as __main__
module. If it is imported somewhere else under its true name, it is known under that name as well with a different name space.
Another guess could be that you are working with ABCs or otherwise let the class have a __instancecheck__()
method.
Users of the "autoreload" setting in iPython will probably run into this. When a class is reloaded, it will become a new class with the exact same name. Instances of the old class will not have their type updated:
# After reloading the Role class
[ins] In [19]: y = Role()
[ins] In [20]: isinstance(x, Role)
Out[20]: False
[nav] In [21]: isinstance(y, Role)
Out[21]: True
[ins] In [22]: type(x)
Out[22]: myproject.auth.Role
[ins] In [23]: type(y)
Out[23]: myproject.auth.Role
I encountered this problem as well, for me to problem laid in my imports. I imported Type
once from parent.child.Type
and once from child.Type
. I assume that when using the full path the module is imported from the installed pip package, whereas using the relative path it is imported as a new module on the fly.