Sorry, something went wrong.
|
while maintaining all invariants Argh, I was wrong. While this PR has no impact on behaviour in 99% of common cases, there is a behaviour change in this edge case: >>> from typing import *
>>> @runtime_checkable
... class Foo(Protocol):
... def meth(self): ...
...
>>> class Bar:
... def __init__(self):
... self.meth = lambda: None
...
>>> isinstance(Bar(), Foo) # True on `main`, False with this PR
|
Sorry, something went wrong.
|
Strictly speaking, I suppose the optimisation would be safe for dunder methods, which are always looked up on the class instead of the instance. But not sure if this optimisation is worth complicating the logic such that we start differentiating between dunder and non-dunder methods in __instancecheck__. |
Sorry, something went wrong.
|
Strictly speaking, I suppose the optimisation would be safe for dunder methods I hate this idea, and can't come up with a better one that provides this optimisation. So, closing for now; will add more tests in a different PR. |
Sorry, something went wrong.
This speeds up this isinstance() call 12x, while maintaining all invariants:
The performance of other kinds of isinstance() calls is not impacted.
Benchmark script