Skip to content

Commit 920d60d

Browse files
Support my PEP 649 branch (#412)
1 parent e792bce commit 920d60d

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# Unreleased
2+
3+
- Preliminary changes for compatibility with the draft implementation
4+
of PEP 649 in Python 3.14.
5+
16
# Release 4.12.0 (May 23, 2024)
27

38
This release is mostly the same as 4.12.0rc1 but fixes one more

src/test_typing_extensions.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@
6464
)
6565

6666
ANN_MODULE_SOURCE = '''\
67+
import sys
6768
from typing import List, Optional
6869
from functools import wraps
6970
70-
__annotations__[1] = 2
71+
try:
72+
__annotations__[1] = 2
73+
except NameError:
74+
assert sys.version_info >= (3, 14)
7175
7276
class C:
7377
@@ -77,8 +81,10 @@ class C:
7781
x: int = 5; y: str = x; f: Tuple[int, int]
7882
7983
class M(type):
80-
81-
__annotations__['123'] = 123
84+
try:
85+
__annotations__['123'] = 123
86+
except NameError:
87+
assert sys.version_info >= (3, 14)
8288
o: type = object
8389
8490
(pars): bool = True
@@ -1310,7 +1316,10 @@ def tearDownClass(cls):
13101316
del sys.modules[modname]
13111317

13121318
def test_get_type_hints_modules(self):
1313-
ann_module_type_hints = {1: 2, 'f': Tuple[int, int], 'x': int, 'y': str}
1319+
if sys.version_info >= (3, 14):
1320+
ann_module_type_hints = {'f': Tuple[int, int], 'x': int, 'y': str}
1321+
else:
1322+
ann_module_type_hints = {1: 2, 'f': Tuple[int, int], 'x': int, 'y': str}
13141323
self.assertEqual(gth(self.ann_module), ann_module_type_hints)
13151324
self.assertEqual(gth(self.ann_module2), {})
13161325
self.assertEqual(gth(self.ann_module3), {})
@@ -1319,7 +1328,10 @@ def test_get_type_hints_classes(self):
13191328
self.assertEqual(gth(self.ann_module.C, self.ann_module.__dict__),
13201329
{'y': Optional[self.ann_module.C]})
13211330
self.assertIsInstance(gth(self.ann_module.j_class), dict)
1322-
self.assertEqual(gth(self.ann_module.M), {'123': 123, 'o': type})
1331+
if sys.version_info >= (3, 14):
1332+
self.assertEqual(gth(self.ann_module.M), {'o': type})
1333+
else:
1334+
self.assertEqual(gth(self.ann_module.M), {'123': 123, 'o': type})
13231335
self.assertEqual(gth(self.ann_module.D),
13241336
{'j': str, 'k': str, 'y': Optional[self.ann_module.C]})
13251337
self.assertEqual(gth(self.ann_module.Y), {'z': int})
@@ -2992,7 +3004,7 @@ def meth(self): pass # noqa: B027
29923004

29933005
acceptable_extra_attrs = {
29943006
'_is_protocol', '_is_runtime_protocol', '__parameters__',
2995-
'__init__', '__annotations__', '__subclasshook__',
3007+
'__init__', '__annotations__', '__subclasshook__', '__annotate__'
29963008
}
29973009
self.assertLessEqual(vars(NonP).keys(), vars(C).keys() | acceptable_extra_attrs)
29983010
self.assertLessEqual(

src/typing_extensions.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,13 @@ def __new__(cls, name, bases, ns, *, total=True, closed=False):
942942
tp_dict.__orig_bases__ = bases
943943

944944
annotations = {}
945-
own_annotations = ns.get('__annotations__', {})
945+
if "__annotations__" in ns:
946+
own_annotations = ns["__annotations__"]
947+
elif "__annotate__" in ns:
948+
# TODO: Use inspect.VALUE here, and make the annotations lazily evaluated
949+
own_annotations = ns["__annotate__"](1)
950+
else:
951+
own_annotations = {}
946952
msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
947953
if _TAKES_MODULE:
948954
own_annotations = {
@@ -3104,7 +3110,13 @@ def __new__(cls, typename, bases, ns):
31043110
raise TypeError(
31053111
'can only inherit from a NamedTuple type and Generic')
31063112
bases = tuple(tuple if base is _NamedTuple else base for base in bases)
3107-
types = ns.get('__annotations__', {})
3113+
if "__annotations__" in ns:
3114+
types = ns["__annotations__"]
3115+
elif "__annotate__" in ns:
3116+
# TODO: Use inspect.VALUE here, and make the annotations lazily evaluated
3117+
types = ns["__annotate__"](1)
3118+
else:
3119+
types = {}
31083120
default_names = []
31093121
for field_name in types:
31103122
if field_name in ns:

0 commit comments

Comments
 (0)