-
-
Notifications
You must be signed in to change notification settings - Fork 30.9k
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
gh-74690: Avoid a costly type check where possible in _ProtocolMeta.__subclasscheck__
#112717
gh-74690: Avoid a costly type check where possible in _ProtocolMeta.__subclasscheck__
#112717
Conversation
…lMeta.__subclasscheck__`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Is it important that the "issubclass() arg 1 must be a class" error message takes precedence over the others? It's not obvious to me that it should, but I'm not very familiar with this code.
Thanks for the review! :D
Yeah, so to clarify, with the type checks, this is the behaviour we get with this PR branch: Python 3.13.0a2+ (heads/main:9560e0d6d7, Dec 4 2023, 13:50:58) [MSC v.1932 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import typing as t
>>> @t.runtime_checkable
... class Foo(t.Protocol):
... X = 1
...
>>> issubclass("not a class", Foo)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
issubclass("not a class", Foo)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
File "C:\Users\alexw\coding\fast-cpython\Lib\typing.py", line 1842, in __subclasscheck__
_type_check_subclasscheck_second_arg(other)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
File "C:\Users\alexw\coding\fast-cpython\Lib\typing.py", line 1796, in _type_check_subclasscheck_second_arg
raise TypeError('issubclass() arg 1 must be a class')
TypeError: issubclass() arg 1 must be a class But without the type checks, we'd get this behaviour instead: >>> import typing as t
>>> @t.runtime_checkable
... class Foo(t.Protocol):
... X = 1
...
>>> issubclass("not a class", Foo)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
issubclass("not a class", Foo)
~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^
File "C:\Users\alexw\coding\fast-cpython\Lib\typing.py", line 1847, in __subclasscheck__
raise TypeError(
...<2 lines>...
)
TypeError: Protocols with non-method members don't support issubclass(). Non-method members: 'X'. It wouldn't be the worst thing in the world if we had that error message instead. But the fact that |
I added a docstring in 3c7eb19 -- does that make things a bit clearer? :) |
It does indeed! Thank you. |
…lMeta.__subclasscheck__` (python#112717)
Release 4.10.0 (February 24, 2024) This feature release adds support for PEP 728 (TypedDict with extra items) and PEP 742 (``TypeIs``). There are no changes since 4.10.0rc1. Release 4.10.0rc1 (February 17, 2024) - Add support for PEP 728, supporting the `closed` keyword argument and the special `__extra_items__` key for TypedDict. Patch by Zixuan James Li. - Add support for PEP 742, adding `typing_extensions.TypeIs`. Patch by Jelle Zijlstra. - Drop runtime error when a read-only `TypedDict` item overrides a mutable one. Type checkers should still flag this as an error. Patch by Jelle Zijlstra. - Speedup `issubclass()` checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717, by Alex Waygood). - Fix a regression in the implementation of protocols where `typing.Protocol` classes that were not marked as `@runtime_checkable` would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. Patch by Alex Waygood, backporting python/cpython#113401.
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 0a1b1a2eefa66ff5d8eb12085d3ecfed0eff7ac1) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 0a1b1a2eefa66ff5d8eb12085d3ecfed0eff7ac1) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 2fee4e85e88bea2ac7c42a1c93a3049258a47c45) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 2fee4e85e88bea2ac7c42a1c93a3049258a47c45) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 5b138eb2bf420f1dc81db76b2eb7d74fd88c0c29) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 5b138eb2bf420f1dc81db76b2eb7d74fd88c0c29) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 5b138eb2bf420f1dc81db76b2eb7d74fd88c0c29) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 955ab06da61db7393ad54a87e5f2536f0dae3cf2) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 955ab06da61db7393ad54a87e5f2536f0dae3cf2) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 955ab06da61db7393ad54a87e5f2536f0dae3cf2) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 955ab06da61db7393ad54a87e5f2536f0dae3cf2) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 91dd6f2878bcdbb6f9ba65927f6c6f981b0b3f1a) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 91dd6f2878bcdbb6f9ba65927f6c6f981b0b3f1a) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 91dd6f2878bcdbb6f9ba65927f6c6f981b0b3f1a) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 91dd6f2878bcdbb6f9ba65927f6c6f981b0b3f1a) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 91dd6f2878bcdbb6f9ba65927f6c6f981b0b3f1a) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 91dd6f2878bcdbb6f9ba65927f6c6f981b0b3f1a) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 91dd6f2878bcdbb6f9ba65927f6c6f981b0b3f1a) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Changelog: ============ -Add support for PEP 728, supporting the closed keyword argument and the special __extra_items__ key for TypedDict. -Add support for PEP 742, adding typing_extensions.TypeIs. -Drop runtime error when a read-only TypedDict item overrides a mutable one. Type checkers should still flag this as an error. -Speedup issubclass() checks against simple runtime-checkable protocols by around 6% (backporting python/cpython#112717 -Fix a regression in the implementation of protocols where typing.Protocol classes that were not marked as @runtime_checkable would be unnecessarily introspected, potentially causing exceptions to be raised if the protocol had problematic members. (From OE-Core rev: 91dd6f2878bcdbb6f9ba65927f6c6f981b0b3f1a) Signed-off-by: Wang Mingyu <wangmy@fujitsu.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
…lMeta.__subclasscheck__` (python#112717)
This speeds up
issubclass(int, typing.SupportsIndex)
by around 6%.The approach is to move the code around a bit so that we delegate the type-checking to
type.__subclasscheck__
wherever possible;type.__subclasscheck__
can do it faster. We now only check from_ProtocolMeta.__subclasscheck__
whetherother
is an instance oftype
if we know we're about to raise an exception anyway, and we want theissubclass() arg 2 must be a type
message to take priority over the other error message we could potentially give the user.