Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support my PEP 649 branch #412

Merged
merged 2 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
23 changes: 17 additions & 6 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@
from typing import List, Optional
from functools import wraps

__annotations__[1] = 2
try:
__annotations__[1] = 2
except NameError:
pass # 3.14+
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add an assert system.version >= (3, 14) inside the except block?


class C:

Expand All @@ -77,8 +80,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:
pass # 3.14+
o: type = object

(pars): bool = True
Expand Down Expand Up @@ -1312,7 +1317,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 @@ -1321,7 +1329,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 @@ -2994,7 +3005,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 = {}
Comment on lines +945 to +951
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mentioned in python/cpython#119361 (comment) that you'd be adding a helper function for this kind of thing at some point -- would it be worth waiting for that, since the first 3.14 alpha is some way off?

I can also see the advantages of getting typing_extensions working with your branch, though, so that you can start testing with the rest of the ecosystem that depends on typing_extensions. So I definitely don't want to block you merging this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do expect we'll need more changes later, but it's helpful to get this merged so I can test the rest of the ecosystem too.

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
Loading