Skip to content
Merged
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
42 changes: 38 additions & 4 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import typing
import warnings

# Breakpoint: https://github.com/python/cpython/pull/119891
if sys.version_info >= (3, 14):
import annotationlib

Expand Down Expand Up @@ -151,6 +152,7 @@
# for backward compatibility
PEP_560 = True
GenericMeta = type
# Breakpoint: https://github.com/python/cpython/pull/116129
_PEP_696_IMPLEMENTED = sys.version_info >= (3, 13, 0, "beta")

# Added with bpo-45166 to 3.10.1+ and some 3.9 versions
Expand All @@ -168,6 +170,7 @@ def __repr__(self):
_marker = _Sentinel()


# Breakpoint: https://github.com/python/cpython/pull/27342
if sys.version_info >= (3, 10):
def _should_collect_from_parameters(t):
return isinstance(
Expand All @@ -189,6 +192,7 @@ def _should_collect_from_parameters(t):
T_contra = typing.TypeVar('T_contra', contravariant=True) # Ditto contravariant.


# Breakpoint: https://github.com/python/cpython/pull/31841
if sys.version_info >= (3, 11):
from typing import Any
else:
Expand Down Expand Up @@ -277,6 +281,7 @@ def __repr__(self):

Final = typing.Final

# Breakpoint: https://github.com/python/cpython/pull/30530
if sys.version_info >= (3, 11):
final = typing.final
else:
Expand Down Expand Up @@ -320,6 +325,7 @@ def IntVar(name):


# A Literal bug was fixed in 3.11.0, 3.10.1 and 3.9.8
# Breakpoint: https://github.com/python/cpython/pull/29334
if sys.version_info >= (3, 10, 1):
Literal = typing.Literal
else:
Expand Down Expand Up @@ -480,6 +486,7 @@ def clear_overloads():
TYPE_CHECKING = typing.TYPE_CHECKING


# Breakpoint: https://github.com/python/cpython/pull/118681
if sys.version_info >= (3, 13, 0, "beta"):
from typing import AsyncContextManager, AsyncGenerator, ContextManager, Generator
else:
Expand Down Expand Up @@ -590,6 +597,7 @@ def _caller(depth=1, default='__main__'):

# `__match_args__` attribute was removed from protocol members in 3.13,
# we want to backport this change to older Python versions.
# Breakpoint: https://github.com/python/cpython/pull/110683
if sys.version_info >= (3, 13):
Protocol = typing.Protocol
else:
Expand Down Expand Up @@ -770,6 +778,7 @@ def __init_subclass__(cls, *args, **kwargs):
cls.__init__ = _no_init


# Breakpoint: https://github.com/python/cpython/pull/113401
if sys.version_info >= (3, 13):
runtime_checkable = typing.runtime_checkable
else:
Expand Down Expand Up @@ -830,6 +839,7 @@ def close(self): ...


# Our version of runtime-checkable protocols is faster on Python <=3.11
# Breakpoint: https://github.com/python/cpython/pull/112717
if sys.version_info >= (3, 12):
SupportsInt = typing.SupportsInt
SupportsFloat = typing.SupportsFloat
Expand Down Expand Up @@ -1159,6 +1169,7 @@ def __new__(cls, name, bases, ns, *, total=True, closed=None,
mutable_keys.add(annotation_key)
readonly_keys.discard(annotation_key)

# Breakpoint: https://github.com/python/cpython/pull/119891
if sys.version_info >= (3, 14):
def __annotate__(format):
annos = {}
Expand Down Expand Up @@ -1249,6 +1260,7 @@ def _create_typeddict(
raise TypeError("TypedDict takes either a dict or keyword arguments,"
" but not both")
if kwargs:
# Breakpoint: https://github.com/python/cpython/pull/104891
if sys.version_info >= (3, 13):
raise TypeError("TypedDict takes no keyword arguments")
warnings.warn(
Expand Down Expand Up @@ -1458,6 +1470,7 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
hint = typing.get_type_hints(
obj, globalns=globalns, localns=localns, include_extras=True
)
# Breakpoint: https://github.com/python/cpython/pull/30304
if sys.version_info < (3, 11):
_clean_optional(obj, hint, globalns, localns)
if include_extras:
Expand Down Expand Up @@ -1530,7 +1543,8 @@ def _clean_optional(obj, hints, globalns=None, localns=None):

# Python 3.9 has get_origin() and get_args() but those implementations don't support
# ParamSpecArgs and ParamSpecKwargs, so only Python 3.10's versions will do.
if sys.version_info[:2] >= (3, 10):
# Breakpoint: https://github.com/python/cpython/pull/25298
if sys.version_info >= (3, 10):
get_origin = typing.get_origin
get_args = typing.get_args
# 3.9
Expand Down Expand Up @@ -2096,6 +2110,7 @@ def _concatenate_getitem(self, parameters):


# 3.11+; Concatenate does not accept ellipsis in 3.10
# Breakpoint: https://github.com/python/cpython/pull/30969
if sys.version_info >= (3, 11):
Concatenate = typing.Concatenate
# <=3.10
Expand Down Expand Up @@ -2432,7 +2447,9 @@ def foo(**kwargs: Unpack[Movie]): ...
"""


if sys.version_info >= (3, 12): # PEP 692 changed the repr of Unpack[]
# PEP 692 changed the repr of Unpack[]
# Breakpoint: https://github.com/python/cpython/pull/104048
if sys.version_info >= (3, 12):
Unpack = typing.Unpack

def _is_unpack(obj):
Expand Down Expand Up @@ -2695,8 +2712,9 @@ def int_or_str(arg: int | str) -> None:
raise AssertionError(f"Expected code to be unreachable, but got: {value}")


# dataclass_transform exists in 3.11 but lacks the frozen_default parameter
# Breakpoint: https://github.com/python/cpython/pull/99958
if sys.version_info >= (3, 12): # 3.12+
# dataclass_transform exists in 3.11 but lacks the frozen_default parameter
dataclass_transform = typing.dataclass_transform
else: # <=3.11
def dataclass_transform(
Expand Down Expand Up @@ -2827,6 +2845,7 @@ def method(self) -> None:


# Python 3.13.3+ contains a fix for the wrapped __new__
# Breakpoint: https://github.com/python/cpython/pull/132160
if sys.version_info >= (3, 13, 3):
deprecated = warnings.deprecated
else:
Expand Down Expand Up @@ -2956,6 +2975,7 @@ def wrapper(*args, **kwargs):
return arg(*args, **kwargs)

if asyncio.coroutines.iscoroutinefunction(arg):
# Breakpoint: https://github.com/python/cpython/pull/99247
if sys.version_info >= (3, 12):
wrapper = inspect.markcoroutinefunction(wrapper)
else:
Expand All @@ -2969,6 +2989,7 @@ def wrapper(*args, **kwargs):
f"a class or callable, not {arg!r}"
)

# Breakpoint: https://github.com/python/cpython/pull/23702
if sys.version_info < (3, 10):
def _is_param_expr(arg):
return arg is ... or isinstance(
Expand Down Expand Up @@ -3045,6 +3066,7 @@ def _check_generic(cls, parameters, elen=_marker):

expect_val = f"at least {elen}"

# Breakpoint: https://github.com/python/cpython/pull/27515
things = "arguments" if sys.version_info >= (3, 10) else "parameters"
raise TypeError(f"Too {'many' if alen > elen else 'few'} {things}"
f" for {cls}; actual {alen}, expected {expect_val}")
Expand Down Expand Up @@ -3238,6 +3260,7 @@ def _collect_parameters(args):
# This was explicitly disallowed in 3.9-3.10, and only half-worked in <=3.8.
# On 3.12, we added __orig_bases__ to call-based NamedTuples
# On 3.13, we deprecated kwargs-based NamedTuples
# Breakpoint: https://github.com/python/cpython/pull/105609
if sys.version_info >= (3, 13):
NamedTuple = typing.NamedTuple
else:
Expand Down Expand Up @@ -3313,6 +3336,7 @@ def __new__(cls, typename, bases, ns):
# using add_note() until py312.
# Making sure exceptions are raised in the same way
# as in "normal" classes seems most important here.
# Breakpoint: https://github.com/python/cpython/pull/95915
if sys.version_info >= (3, 12):
e.add_note(msg)
raise
Expand Down Expand Up @@ -3461,6 +3485,7 @@ class Baz(list[str]): ...

# NewType is a class on Python 3.10+, making it pickleable
# The error message for subclassing instances of NewType was improved on 3.11+
# Breakpoint: https://github.com/python/cpython/pull/30268
if sys.version_info >= (3, 11):
NewType = typing.NewType
else:
Expand Down Expand Up @@ -3513,6 +3538,7 @@ def __repr__(self):
def __reduce__(self):
return self.__qualname__

# Breakpoint: https://github.com/python/cpython/pull/21515
if sys.version_info >= (3, 10):
# PEP 604 methods
# It doesn't make sense to have these methods on Python <3.10
Expand All @@ -3524,10 +3550,12 @@ def __ror__(self, other):
return typing.Union[other, self]


# Breakpoint: https://github.com/python/cpython/pull/124795
if sys.version_info >= (3, 14):
TypeAliasType = typing.TypeAliasType
# <=3.13
else:
# Breakpoint: https://github.com/python/cpython/pull/103764
if sys.version_info >= (3, 12):
# 3.12-3.13
def _is_unionable(obj):
Expand Down Expand Up @@ -3723,6 +3751,7 @@ def __init_subclass__(cls, *args, **kwargs):
def __call__(self):
raise TypeError("Type alias is not callable")

# Breakpoint: https://github.com/python/cpython/pull/21515
if sys.version_info >= (3, 10):
def __or__(self, right):
# For forward compatibility with 3.12, reject Unions
Expand Down Expand Up @@ -3835,15 +3864,19 @@ def __eq__(self, other: object) -> bool:
__all__.append("CapsuleType")


if sys.version_info >= (3,14):
if sys.version_info >= (3, 14):
from annotationlib import Format, get_annotations
else:
# Available since Python 3.14.0a3
# PR: https://github.com/python/cpython/pull/124415
class Format(enum.IntEnum):
VALUE = 1
VALUE_WITH_FAKE_GLOBALS = 2
FORWARDREF = 3
STRING = 4

# Available since Python 3.14.0a1
# PR: https://github.com/python/cpython/pull/119891
def get_annotations(obj, *, globals=None, locals=None, eval_str=False,
format=Format.VALUE):
"""Compute the annotations dict for an object.
Expand Down Expand Up @@ -4181,6 +4214,7 @@ def __repr__(self):
def __call__(self, *args, **kwargs):
raise TypeError(f"{type(self).__name__!r} object is not callable")

# Breakpoint: https://github.com/python/cpython/pull/21515
if sys.version_info >= (3, 10):
def __or__(self, other):
return typing.Union[self, other]
Expand Down
Loading