Skip to content

Commit 8658a0e

Browse files
committed
Refine version_info checks
1 parent 972bd87 commit 8658a0e

File tree

1 file changed

+69
-31
lines changed

1 file changed

+69
-31
lines changed

src/typing_extensions.py

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import typing
1515
import warnings
1616

17-
if sys.version_info >= (3, 14):
17+
# Breakpoint: https://github.com/python/cpython/pull/119891
18+
if sys.version_info >= (3, 14, 0, "alpha", 1):
1819
import annotationlib
1920

2021
__all__ = [
@@ -151,6 +152,7 @@
151152
# for backward compatibility
152153
PEP_560 = True
153154
GenericMeta = type
155+
# Breakpoint: https://github.com/python/cpython/pull/116129
154156
_PEP_696_IMPLEMENTED = sys.version_info >= (3, 13, 0, "beta")
155157

156158
# Added with bpo-45166 to 3.10.1+ and some 3.9 versions
@@ -168,7 +170,8 @@ def __repr__(self):
168170
_marker = _Sentinel()
169171

170172

171-
if sys.version_info >= (3, 10):
173+
# Breakpoint: https://github.com/python/cpython/pull/27342
174+
if sys.version_info >= (3, 10, 0, "candidate"):
172175
def _should_collect_from_parameters(t):
173176
return isinstance(
174177
t, (typing._GenericAlias, _types.GenericAlias, _types.UnionType)
@@ -189,7 +192,8 @@ def _should_collect_from_parameters(t):
189192
T_contra = typing.TypeVar('T_contra', contravariant=True) # Ditto contravariant.
190193

191194

192-
if sys.version_info >= (3, 11):
195+
# Breakpoint: https://github.com/python/cpython/pull/31841
196+
if sys.version_info >= (3, 11, 0, "alpha", 7):
193197
from typing import Any
194198
else:
195199

@@ -277,7 +281,8 @@ def __repr__(self):
277281

278282
Final = typing.Final
279283

280-
if sys.version_info >= (3, 11):
284+
# Breakpoint: https://github.com/python/cpython/pull/30530
285+
if sys.version_info >= (3, 11, 0, "alpha", 4):
281286
final = typing.final
282287
else:
283288
# @final exists in 3.8+, but we backport it for all versions
@@ -320,6 +325,7 @@ def IntVar(name):
320325

321326

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

482488

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

591598
# `__match_args__` attribute was removed from protocol members in 3.13,
592599
# we want to backport this change to older Python versions.
593-
if sys.version_info >= (3, 13):
600+
# Breakpoint: https://github.com/python/cpython/pull/110683
601+
if sys.version_info >= (3, 13, 0, "alpha", 1):
594602
Protocol = typing.Protocol
595603
else:
596604
def _allow_reckless_class_checks(depth=2):
@@ -770,7 +778,8 @@ def __init_subclass__(cls, *args, **kwargs):
770778
cls.__init__ = _no_init
771779

772780

773-
if sys.version_info >= (3, 13):
781+
# Breakpoint: https://github.com/python/cpython/pull/113401
782+
if sys.version_info >= (3, 13, 0, "alpha", 3):
774783
runtime_checkable = typing.runtime_checkable
775784
else:
776785
def runtime_checkable(cls):
@@ -830,7 +839,8 @@ def close(self): ...
830839

831840

832841
# Our version of runtime-checkable protocols is faster on Python <=3.11
833-
if sys.version_info >= (3, 12):
842+
# Breakpoint: https://github.com/python/cpython/pull/112717
843+
if sys.version_info >= (3, 12, 0, "alpha", 3):
834844
SupportsInt = typing.SupportsInt
835845
SupportsFloat = typing.SupportsFloat
836846
SupportsComplex = typing.SupportsComplex
@@ -1159,7 +1169,8 @@ def __new__(cls, name, bases, ns, *, total=True, closed=None,
11591169
mutable_keys.add(annotation_key)
11601170
readonly_keys.discard(annotation_key)
11611171

1162-
if sys.version_info >= (3, 14):
1172+
# Breakpoint: https://github.com/python/cpython/pull/119891
1173+
if sys.version_info >= (3, 14, 0, "alpha", 1):
11631174
def __annotate__(format):
11641175
annos = {}
11651176
for base in bases:
@@ -1249,7 +1260,8 @@ def _create_typeddict(
12491260
raise TypeError("TypedDict takes either a dict or keyword arguments,"
12501261
" but not both")
12511262
if kwargs:
1252-
if sys.version_info >= (3, 13):
1263+
# Breakpoint: https://github.com/python/cpython/pull/104891
1264+
if sys.version_info >= (3, 13, 0, "alpha", 1):
12531265
raise TypeError("TypedDict takes no keyword arguments")
12541266
warnings.warn(
12551267
"The kwargs-based syntax for TypedDict definitions is deprecated "
@@ -1458,7 +1470,8 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False):
14581470
hint = typing.get_type_hints(
14591471
obj, globalns=globalns, localns=localns, include_extras=True
14601472
)
1461-
if sys.version_info < (3, 11):
1473+
# Breakpoint: https://github.com/python/cpython/pull/30304
1474+
if sys.version_info < (3, 11, 0, "alpha", 6):
14621475
_clean_optional(obj, hint, globalns, localns)
14631476
if include_extras:
14641477
return hint
@@ -1530,7 +1543,8 @@ def _clean_optional(obj, hints, globalns=None, localns=None):
15301543

15311544
# Python 3.9 has get_origin() and get_args() but those implementations don't support
15321545
# ParamSpecArgs and ParamSpecKwargs, so only Python 3.10's versions will do.
1533-
if sys.version_info[:2] >= (3, 10):
1546+
# Breakpoint: https://github.com/python/cpython/pull/25298
1547+
if sys.version_info >= (3, 10, 0, "beta"):
15341548
get_origin = typing.get_origin
15351549
get_args = typing.get_args
15361550
# 3.9
@@ -2096,7 +2110,8 @@ def _concatenate_getitem(self, parameters):
20962110

20972111

20982112
# 3.11+; Concatenate does not accept ellipsis in 3.10
2099-
if sys.version_info >= (3, 11):
2113+
# Breakpoint: https://github.com/python/cpython/pull/30969
2114+
if sys.version_info >= (3, 11, 0, "beta"):
21002115
Concatenate = typing.Concatenate
21012116
# <=3.10
21022117
else:
@@ -2432,7 +2447,9 @@ def foo(**kwargs: Unpack[Movie]): ...
24322447
"""
24332448

24342449

2435-
if sys.version_info >= (3, 12): # PEP 692 changed the repr of Unpack[]
2450+
# PEP 692 changed the repr of Unpack[]
2451+
# Breakpoint: https://github.com/python/cpython/pull/104048
2452+
if sys.version_info >= (3, 12, 0, "beta"):
24362453
Unpack = typing.Unpack
24372454

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

26972714

2698-
if sys.version_info >= (3, 12): # 3.12+
2699-
# dataclass_transform exists in 3.11 but lacks the frozen_default parameter
2715+
# dataclass_transform exists in 3.11 but lacks the frozen_default parameter
2716+
# Breakpoint: https://github.com/python/cpython/pull/99958
2717+
if sys.version_info >= (3, 12, 0, "alpha", 3): # 3.12+
27002718
dataclass_transform = typing.dataclass_transform
27012719
else: # <=3.11
27022720
def dataclass_transform(
@@ -2827,6 +2845,7 @@ def method(self) -> None:
28272845

28282846

28292847
# Python 3.13.3+ contains a fix for the wrapped __new__
2848+
# Breakpoint: https://github.com/python/cpython/pull/132160
28302849
if sys.version_info >= (3, 13, 3):
28312850
deprecated = warnings.deprecated
28322851
else:
@@ -2956,7 +2975,8 @@ def wrapper(*args, **kwargs):
29562975
return arg(*args, **kwargs)
29572976

29582977
if asyncio.coroutines.iscoroutinefunction(arg):
2959-
if sys.version_info >= (3, 12):
2978+
# Breakpoint: https://github.com/python/cpython/pull/99247
2979+
if sys.version_info >= (3, 12, 0 , "alpha", 4):
29602980
wrapper = inspect.markcoroutinefunction(wrapper)
29612981
else:
29622982
wrapper._is_coroutine = asyncio.coroutines._is_coroutine
@@ -2969,12 +2989,8 @@ def wrapper(*args, **kwargs):
29692989
f"a class or callable, not {arg!r}"
29702990
)
29712991

2972-
if sys.version_info < (3, 10):
2973-
def _is_param_expr(arg):
2974-
return arg is ... or isinstance(
2975-
arg, (tuple, list, ParamSpec, _ConcatenateGenericAlias)
2976-
)
2977-
else:
2992+
# Breakpoint: https://github.com/python/cpython/pull/23702
2993+
if sys.version_info >= (3, 10, 0, "alpha", 4):
29782994
def _is_param_expr(arg):
29792995
return arg is ... or isinstance(
29802996
arg,
@@ -2986,6 +3002,11 @@ def _is_param_expr(arg):
29863002
typing._ConcatenateGenericAlias,
29873003
),
29883004
)
3005+
else:
3006+
def _is_param_expr(arg):
3007+
return arg is ... or isinstance(
3008+
arg, (tuple, list, ParamSpec, _ConcatenateGenericAlias)
3009+
)
29893010

29903011

29913012
# We have to do some monkey patching to deal with the dual nature of
@@ -3045,7 +3066,12 @@ def _check_generic(cls, parameters, elen=_marker):
30453066

30463067
expect_val = f"at least {elen}"
30473068

3048-
things = "arguments" if sys.version_info >= (3, 10) else "parameters"
3069+
# Breakpoint: https://github.com/python/cpython/pull/27515
3070+
things = (
3071+
"arguments"
3072+
if sys.version_info >= (3, 10, 0, "candidate")
3073+
else "parameters"
3074+
)
30493075
raise TypeError(f"Too {'many' if alen > elen else 'few'} {things}"
30503076
f" for {cls}; actual {alen}, expected {expect_val}")
30513077
else:
@@ -3238,6 +3264,7 @@ def _collect_parameters(args):
32383264
# This was explicitly disallowed in 3.9-3.10, and only half-worked in <=3.8.
32393265
# On 3.12, we added __orig_bases__ to call-based NamedTuples
32403266
# On 3.13, we deprecated kwargs-based NamedTuples
3267+
# Breakpoint: https://github.com/python/cpython/pull/105609
32413268
if sys.version_info >= (3, 13):
32423269
NamedTuple = typing.NamedTuple
32433270
else:
@@ -3313,7 +3340,8 @@ def __new__(cls, typename, bases, ns):
33133340
# using add_note() until py312.
33143341
# Making sure exceptions are raised in the same way
33153342
# as in "normal" classes seems most important here.
3316-
if sys.version_info >= (3, 12):
3343+
# Breakpoint: https://github.com/python/cpython/pull/95915
3344+
if sys.version_info >= (3, 12, 0, "alpha", 1):
33173345
e.add_note(msg)
33183346
raise
33193347
else:
@@ -3461,7 +3489,8 @@ class Baz(list[str]): ...
34613489

34623490
# NewType is a class on Python 3.10+, making it pickleable
34633491
# The error message for subclassing instances of NewType was improved on 3.11+
3464-
if sys.version_info >= (3, 11):
3492+
# Breakpoint: https://github.com/python/cpython/pull/30268
3493+
if sys.version_info >= (3, 11, 0, "alpha", 7):
34653494
NewType = typing.NewType
34663495
else:
34673496
class NewType:
@@ -3513,7 +3542,8 @@ def __repr__(self):
35133542
def __reduce__(self):
35143543
return self.__qualname__
35153544

3516-
if sys.version_info >= (3, 10):
3545+
# Breakpoint: https://github.com/python/cpython/pull/21515
3546+
if sys.version_info >= (3, 10, 0, "alpha", 1):
35173547
# PEP 604 methods
35183548
# It doesn't make sense to have these methods on Python <3.10
35193549

@@ -3524,11 +3554,13 @@ def __ror__(self, other):
35243554
return typing.Union[other, self]
35253555

35263556

3527-
if sys.version_info >= (3, 14):
3557+
# Breakpoint: https://github.com/python/cpython/pull/124795
3558+
if sys.version_info >= (3, 14, 0, "alpha", 1):
35283559
TypeAliasType = typing.TypeAliasType
35293560
# <=3.13
35303561
else:
3531-
if sys.version_info >= (3, 12):
3562+
# Breakpoint: https://github.com/python/cpython/pull/103764
3563+
if sys.version_info >= (3, 12, 0, "beta"):
35323564
# 3.12-3.13
35333565
def _is_unionable(obj):
35343566
"""Corresponds to is_unionable() in unionobject.c in CPython."""
@@ -3723,7 +3755,8 @@ def __init_subclass__(cls, *args, **kwargs):
37233755
def __call__(self):
37243756
raise TypeError("Type alias is not callable")
37253757

3726-
if sys.version_info >= (3, 10):
3758+
# Breakpoint: https://github.com/python/cpython/pull/21515
3759+
if sys.version_info >= (3, 10, 0, "alpha", 1):
37273760
def __or__(self, right):
37283761
# For forward compatibility with 3.12, reject Unions
37293762
# that are not accepted by the built-in Union.
@@ -3835,15 +3868,19 @@ def __eq__(self, other: object) -> bool:
38353868
__all__.append("CapsuleType")
38363869

38373870

3838-
if sys.version_info >= (3,14):
3871+
if sys.version_info >= (3, 14, 0, "alpha", 3):
38393872
from annotationlib import Format, get_annotations
38403873
else:
3874+
# Available since Python 3.14.0a3
3875+
# PR: https://github.com/python/cpython/pull/124415
38413876
class Format(enum.IntEnum):
38423877
VALUE = 1
38433878
VALUE_WITH_FAKE_GLOBALS = 2
38443879
FORWARDREF = 3
38453880
STRING = 4
38463881

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

4184-
if sys.version_info >= (3, 10):
4221+
# Breakpoint: https://github.com/python/cpython/pull/21515
4222+
if sys.version_info >= (3, 10, 0, "alpha", 1):
41854223
def __or__(self, other):
41864224
return typing.Union[self, other]
41874225

0 commit comments

Comments
 (0)