diff --git a/CHANGELOG.md b/CHANGELOG.md index 792f25e6..d22b4049 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,8 @@ `typing_extensions` may no longer be considered instances of that protocol using the new release, and vice versa. Most users are unlikely to be affected by this change. Patch by Alex Waygood. +- Speedup `isinstance(3, typing_extensions.SupportsIndex)` by >10x on Python + <3.12. Patch by Alex Waygood. # Release 4.5.0 (February 14, 2023) diff --git a/src/test_typing_extensions.py b/src/test_typing_extensions.py index db4cf899..0e4a1b7e 100644 --- a/src/test_typing_extensions.py +++ b/src/test_typing_extensions.py @@ -3545,7 +3545,7 @@ def test_typing_extensions_defers_when_possible(self): if sys.version_info < (3, 11): exclude |= {'final', 'NamedTuple', 'Any'} if sys.version_info < (3, 12): - exclude |= {'Protocol', 'runtime_checkable'} + exclude |= {'Protocol', 'runtime_checkable', 'SupportsIndex'} for item in typing_extensions.__all__: if item not in exclude and hasattr(typing, item): self.assertIs( diff --git a/src/typing_extensions.py b/src/typing_extensions.py index fc023921..b7864a98 100644 --- a/src/typing_extensions.py +++ b/src/typing_extensions.py @@ -686,10 +686,9 @@ def runtime_checkable(cls): runtime = runtime_checkable -# 3.8+ -if hasattr(typing, 'SupportsIndex'): +# Our version of runtime-checkable protocols is faster on Python 3.7-3.11 +if sys.version_info >= (3, 12): SupportsIndex = typing.SupportsIndex -# 3.7 else: @runtime_checkable class SupportsIndex(Protocol):