Skip to content

Commit d90a2f4

Browse files
Become robust to things being removed from typing (#595)
1 parent 2523523 commit d90a2f4

File tree

4 files changed

+62
-42
lines changed

4 files changed

+62
-42
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Unreleased
22

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

57
New features:
68

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ ignore = [
9494
"RUF012",
9595
"RUF022",
9696
"RUF023",
97+
# Ruff doesn't understand the globals() assignment; we test __all__
98+
# directly in test_all_names_in___all__.
99+
"F822",
97100
]
98101

99102
[tool.ruff.lint.per-file-ignores]

src/test_typing_extensions.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6827,6 +6827,15 @@ def test_typing_extensions_defers_when_possible(self):
68276827
getattr(typing_extensions, item),
68286828
getattr(typing, item))
68296829

6830+
def test_alias_names_still_exist(self):
6831+
for name in typing_extensions._typing_names:
6832+
# If this fails, change _typing_names to conditionally add the name
6833+
# depending on the Python version.
6834+
self.assertTrue(
6835+
hasattr(typing_extensions, name),
6836+
f"{name} no longer exists in typing",
6837+
)
6838+
68306839
def test_typing_extensions_compiles_with_opt(self):
68316840
file_path = typing_extensions.__file__
68326841
try:

src/typing_extensions.py

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4223,46 +4223,52 @@ def evaluate_forward_ref(
42234223

42244224

42254225
# Aliases for items that are in typing in all supported versions.
4226-
# Explicitly assign these (rather than using `from typing import *` at the top),
4227-
# so that we get a CI error if one of these is deleted from typing.py
4228-
# in a future version of Python
4229-
AbstractSet = typing.AbstractSet
4230-
Annotated = typing.Annotated
4231-
AnyStr = typing.AnyStr
4232-
BinaryIO = typing.BinaryIO
4233-
Callable = typing.Callable
4234-
Collection = typing.Collection
4235-
Container = typing.Container
4236-
Dict = typing.Dict
4237-
ForwardRef = typing.ForwardRef
4238-
FrozenSet = typing.FrozenSet
4226+
# We use hasattr() checks so this library will continue to import on
4227+
# future versions of Python that may remove these names.
4228+
_typing_names = [
4229+
"AbstractSet",
4230+
"AnyStr",
4231+
"BinaryIO",
4232+
"Callable",
4233+
"Collection",
4234+
"Container",
4235+
"Dict",
4236+
"FrozenSet",
4237+
"Hashable",
4238+
"IO",
4239+
"ItemsView",
4240+
"Iterable",
4241+
"Iterator",
4242+
"KeysView",
4243+
"List",
4244+
"Mapping",
4245+
"MappingView",
4246+
"Match",
4247+
"MutableMapping",
4248+
"MutableSequence",
4249+
"MutableSet",
4250+
"Optional",
4251+
"Pattern",
4252+
"Reversible",
4253+
"Sequence",
4254+
"Set",
4255+
"Sized",
4256+
"TextIO",
4257+
"Tuple",
4258+
"Union",
4259+
"ValuesView",
4260+
"cast",
4261+
"no_type_check",
4262+
"no_type_check_decorator",
4263+
# This is private, but it was defined by typing_extensions for a long time
4264+
# and some users rely on it.
4265+
"_AnnotatedAlias",
4266+
]
4267+
globals().update(
4268+
{name: getattr(typing, name) for name in _typing_names if hasattr(typing, name)}
4269+
)
4270+
# These are defined unconditionally because they are used in
4271+
# typing-extensions itself.
42394272
Generic = typing.Generic
4240-
Hashable = typing.Hashable
4241-
IO = typing.IO
4242-
ItemsView = typing.ItemsView
4243-
Iterable = typing.Iterable
4244-
Iterator = typing.Iterator
4245-
KeysView = typing.KeysView
4246-
List = typing.List
4247-
Mapping = typing.Mapping
4248-
MappingView = typing.MappingView
4249-
Match = typing.Match
4250-
MutableMapping = typing.MutableMapping
4251-
MutableSequence = typing.MutableSequence
4252-
MutableSet = typing.MutableSet
4253-
Optional = typing.Optional
4254-
Pattern = typing.Pattern
4255-
Reversible = typing.Reversible
4256-
Sequence = typing.Sequence
4257-
Set = typing.Set
4258-
Sized = typing.Sized
4259-
TextIO = typing.TextIO
4260-
Tuple = typing.Tuple
4261-
Union = typing.Union
4262-
ValuesView = typing.ValuesView
4263-
cast = typing.cast
4264-
no_type_check = typing.no_type_check
4265-
no_type_check_decorator = typing.no_type_check_decorator
4266-
# This is private, but it was defined by typing_extensions for a long time
4267-
# and some users rely on it.
4268-
_AnnotatedAlias = typing._AnnotatedAlias
4273+
ForwardRef = typing.ForwardRef
4274+
Annotated = typing.Annotated

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy