Skip to content

Commit 3ad8b3b

Browse files
authored
Reuse stdlib PEP 593 implementation in typing_extensions if present (#699)
Following [1] this prevents multiple runtime implementations of Annotated and get_type_hints from existing on Python 3.9 (which has recently merged PEP 593 changes[2]). Reexporting allows code targetting both Python pre-3.9 and 3.9+ to be able to import from typing_extensions and to keep working without changes. [1] python/cpython#18260 (comment) [2] python/cpython#18260
1 parent 327089b commit 3ad8b3b

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src_py3/typing_extensions.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1707,7 +1707,14 @@ class Point2D(TypedDict):
17071707
"""
17081708

17091709

1710-
if PEP_560:
1710+
# Python 3.9+ has PEP 593 (Annotated and modified get_type_hints)
1711+
if hasattr(typing, 'Annotated'):
1712+
Annotated = typing.Annotated
1713+
get_type_hints = typing.get_type_hints
1714+
# Not exported and not a public API, but needed for get_origin() and get_args()
1715+
# to work.
1716+
_AnnotatedAlias = typing._AnnotatedAlias
1717+
elif PEP_560:
17111718
class _AnnotatedAlias(typing._GenericAlias, _root=True):
17121719
"""Runtime representation of an annotated type.
17131720

0 commit comments

Comments
 (0)