[issue30230] Move quick test in PyObject_IsSubClass outside of PyType_CheckExact guard
Jim Fasarakis-Hilliard
report at bugs.python.org
Wed May 10 22:50:16 EDT 2017
Jim Fasarakis-Hilliard added the comment:
> he said, "I'm not sure if this was placed inside there due to some wild edge-case,"
As a new contributor, I'll always lean on the side of me not seeing something rather than confidently stating that something is definitely wrong. I'm new, the code-base is 20 years old and worked on by a dozen of experienced devs :-)
> Does a "Not-A-Class" class which is not a subclass of itself (issubclass(C, C) returns False) makes any sense?
I'm not sure. Apart from the performance impact, there's a behavioral discrepancy between isinstance and issubclass as you also stated.
In isinstance, __instancecheck__ doesn't *always* get a chance to override the behavior. The `if type(inst) == Cls` [1] stops it before it gets a chance.
In issubclass, __subclasscheck__ does override it:
class Meta(type):
def __instancecheck__(self, other):
print("invoked")
return False
def __subclasscheck__(self, other):
print("invoked")
return False
class Cls(metaclass=Meta):
pass
isinstance(Cls(), Cls)
True
issubclass(Cls, Cls)
invoked
False
So, I guess the question might be re-framed to: Is it guaranteed that __instancecheck__ and __subclasscheck__ are *always* called?
If yes: PyObject_IsInstance should be tweaked.
If no: PyObject_IsSubclass should be tweaked.
p.s Should I maybe move this to python-dev?
[1]: https://github.com/python/cpython/blob/master/Objects/abstract.c#L2338
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30230>
_______________________________________
More information about the Python-bugs-list
mailing list