Skip to content

Commit

Permalink
Support my PEP 649 branch (#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
JelleZijlstra authored May 27, 2024
1 parent e792bce commit 920d60d
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Unreleased

- Preliminary changes for compatibility with the draft implementation
of PEP 649 in Python 3.14.

# Release 4.12.0 (May 23, 2024)

This release is mostly the same as 4.12.0rc1 but fixes one more
Expand Down
24 changes: 18 additions & 6 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@
)

ANN_MODULE_SOURCE = '''\
import sys
from typing import List, Optional
from functools import wraps
__annotations__[1] = 2
try:
__annotations__[1] = 2
except NameError:
assert sys.version_info >= (3, 14)
class C:
Expand All @@ -77,8 +81,10 @@ class C:
x: int = 5; y: str = x; f: Tuple[int, int]
class M(type):
__annotations__['123'] = 123
try:
__annotations__['123'] = 123
except NameError:
assert sys.version_info >= (3, 14)
o: type = object
(pars): bool = True
Expand Down Expand Up @@ -1310,7 +1316,10 @@ def tearDownClass(cls):
del sys.modules[modname]

def test_get_type_hints_modules(self):
ann_module_type_hints = {1: 2, 'f': Tuple[int, int], 'x': int, 'y': str}
if sys.version_info >= (3, 14):
ann_module_type_hints = {'f': Tuple[int, int], 'x': int, 'y': str}
else:
ann_module_type_hints = {1: 2, 'f': Tuple[int, int], 'x': int, 'y': str}
self.assertEqual(gth(self.ann_module), ann_module_type_hints)
self.assertEqual(gth(self.ann_module2), {})
self.assertEqual(gth(self.ann_module3), {})
Expand All @@ -1319,7 +1328,10 @@ def test_get_type_hints_classes(self):
self.assertEqual(gth(self.ann_module.C, self.ann_module.__dict__),
{'y': Optional[self.ann_module.C]})
self.assertIsInstance(gth(self.ann_module.j_class), dict)
self.assertEqual(gth(self.ann_module.M), {'123': 123, 'o': type})
if sys.version_info >= (3, 14):
self.assertEqual(gth(self.ann_module.M), {'o': type})
else:
self.assertEqual(gth(self.ann_module.M), {'123': 123, 'o': type})
self.assertEqual(gth(self.ann_module.D),
{'j': str, 'k': str, 'y': Optional[self.ann_module.C]})
self.assertEqual(gth(self.ann_module.Y), {'z': int})
Expand Down Expand Up @@ -2992,7 +3004,7 @@ def meth(self): pass # noqa: B027

acceptable_extra_attrs = {
'_is_protocol', '_is_runtime_protocol', '__parameters__',
'__init__', '__annotations__', '__subclasshook__',
'__init__', '__annotations__', '__subclasshook__', '__annotate__'
}
self.assertLessEqual(vars(NonP).keys(), vars(C).keys() | acceptable_extra_attrs)
self.assertLessEqual(
Expand Down
16 changes: 14 additions & 2 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,13 @@ def __new__(cls, name, bases, ns, *, total=True, closed=False):
tp_dict.__orig_bases__ = bases

annotations = {}
own_annotations = ns.get('__annotations__', {})
if "__annotations__" in ns:
own_annotations = ns["__annotations__"]
elif "__annotate__" in ns:
# TODO: Use inspect.VALUE here, and make the annotations lazily evaluated
own_annotations = ns["__annotate__"](1)
else:
own_annotations = {}
msg = "TypedDict('Name', {f0: t0, f1: t1, ...}); each t must be a type"
if _TAKES_MODULE:
own_annotations = {
Expand Down Expand Up @@ -3104,7 +3110,13 @@ def __new__(cls, typename, bases, ns):
raise TypeError(
'can only inherit from a NamedTuple type and Generic')
bases = tuple(tuple if base is _NamedTuple else base for base in bases)
types = ns.get('__annotations__', {})
if "__annotations__" in ns:
types = ns["__annotations__"]
elif "__annotate__" in ns:
# TODO: Use inspect.VALUE here, and make the annotations lazily evaluated
types = ns["__annotate__"](1)
else:
types = {}
default_names = []
for field_name in types:
if field_name in ns:
Expand Down

0 comments on commit 920d60d

Please sign in to comment.