-
-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Backport use of getattr_static
for _ProtocolMeta.__instancecheck__
?
#139
Comments
Cc. @chrisjsewell |
I think we should backport it. The point of typing-extensions is broadly to make typing work the way it works on the most recent version of CPython, so we should generally aim to make our code work the way CPython's most recent changes work. |
Should we use the stdlib |
Has it changed much? I'd generally prefer to use the stdlib, but we can vendor if there's a significant behavior difference otherwise. |
It's 2x faster on 3.12 compared to 3.11 for most calls. Other than the performance boost in 3.12, there have been no significant changes to it for several Python versions. |
If it's just speed, then I say leave it be; people who want the performance improvement should use 3.12. |
In python/cpython#103034, we switched to using
inspect.getattr_static
intyping._ProtocolMeta
. This fixed a longstanding bug where properties and__getattr__
methods with side effects would unexpectedly be "called" duringisinstance()
checks against runtime-checkable protocols. Here's a demonstration of the bug, which is now fixed on the CPythonmain
branch:Following recent changes to the implementation of
typing_extensions.Protocol
in this repo, the backport would now just be a one-line change:Diff:
However, this leads to a performance degradation for runtime-checkable protocols with non-callable members, and a fairly awful performance degradation for classes with lots of non-callable members. This
isinstance()
check would become 3x slower than it is in the latest release oftyping_extensions
:On the CPython
main
branch, we've implemented several optimisations togetattr_static
that have substantially mitigated the performance penalty of usinggetattr_static
in_ProtocolMeta.__instancecheck__
: see python/cpython#103193 for details. So, one way of avoiding the performance hit could be to vendorinspect.getattr_static
as it exists on the CPythonmain
branch.Thoughts?
The text was updated successfully, but these errors were encountered: