Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Become robust to things being removed from typing #595

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 4 commits into from
May 10, 2025
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
@@ -1,6 +1,8 @@
# Unreleased

- Drop support for Python 3.8 (including PyPy-3.8). Patch by [Victorien Plot](https://github.com/Viicos).
- Do not attempt to re-export names that have been removed from `typing`,
anticipating the removal of `typing.no_type_check_decorator` in Python 3.15.

New features:

Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ ignore = [
"RUF012",
"RUF022",
"RUF023",
# Ruff doesn't understand the globals() assignment; we test __all__
# directly in test_all_names_in___all__.
"F822",
]

[tool.ruff.lint.per-file-ignores]
Expand Down
9 changes: 9 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6827,6 +6827,15 @@ def test_typing_extensions_defers_when_possible(self):
getattr(typing_extensions, item),
getattr(typing, item))

def test_alias_names_still_exist(self):
for name in typing_extensions._typing_names:
# If this fails, change _typing_names to conditionally add the name
# depending on the Python version.
self.assertTrue(
hasattr(typing_extensions, name),
f"{name} no longer exists in typing",
)

def test_typing_extensions_compiles_with_opt(self):
file_path = typing_extensions.__file__
try:
Expand Down
90 changes: 48 additions & 42 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4223,46 +4223,52 @@ def evaluate_forward_ref(


# Aliases for items that are in typing in all supported versions.
# Explicitly assign these (rather than using `from typing import *` at the top),
# so that we get a CI error if one of these is deleted from typing.py
# in a future version of Python
AbstractSet = typing.AbstractSet
Annotated = typing.Annotated
AnyStr = typing.AnyStr
BinaryIO = typing.BinaryIO
Callable = typing.Callable
Collection = typing.Collection
Container = typing.Container
Dict = typing.Dict
ForwardRef = typing.ForwardRef
FrozenSet = typing.FrozenSet
# We use hasattr() checks so this library will continue to import on
# future versions of Python that may remove these names.
_typing_names = [
"AbstractSet",
"AnyStr",
"BinaryIO",
"Callable",
"Collection",
"Container",
"Dict",
"FrozenSet",
"Hashable",
"IO",
"ItemsView",
"Iterable",
"Iterator",
"KeysView",
"List",
"Mapping",
"MappingView",
"Match",
"MutableMapping",
"MutableSequence",
"MutableSet",
"Optional",
"Pattern",
"Reversible",
"Sequence",
"Set",
"Sized",
"TextIO",
"Tuple",
"Union",
"ValuesView",
"cast",
"no_type_check",
"no_type_check_decorator",
# This is private, but it was defined by typing_extensions for a long time
# and some users rely on it.
"_AnnotatedAlias",
]
globals().update(
{name: getattr(typing, name) for name in _typing_names if hasattr(typing, name)}
)
# These are defined unconditionally because they are used in
# typing-extensions itself.
Generic = typing.Generic
Hashable = typing.Hashable
IO = typing.IO
ItemsView = typing.ItemsView
Iterable = typing.Iterable
Iterator = typing.Iterator
KeysView = typing.KeysView
List = typing.List
Mapping = typing.Mapping
MappingView = typing.MappingView
Match = typing.Match
MutableMapping = typing.MutableMapping
MutableSequence = typing.MutableSequence
MutableSet = typing.MutableSet
Optional = typing.Optional
Pattern = typing.Pattern
Reversible = typing.Reversible
Sequence = typing.Sequence
Set = typing.Set
Sized = typing.Sized
TextIO = typing.TextIO
Tuple = typing.Tuple
Union = typing.Union
ValuesView = typing.ValuesView
cast = typing.cast
no_type_check = typing.no_type_check
no_type_check_decorator = typing.no_type_check_decorator
# This is private, but it was defined by typing_extensions for a long time
# and some users rely on it.
_AnnotatedAlias = typing._AnnotatedAlias
ForwardRef = typing.ForwardRef
Annotated = typing.Annotated
Loading