Skip to content

Speedup isinstance(3, typing_extensions.SupportsIndex) by >10x #141

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

Merged
merged 1 commit into from
Apr 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down