Thanks to visit codestin.com
Credit goes to github.com

Skip to content

More fixes for 3.14 and 3.15 #602

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

Merged
merged 2 commits into from
May 24, 2025
Merged
Show file tree
Hide file tree
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
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
- Do not attempt to re-export names that have been removed from `typing`,
anticipating the removal of `typing.no_type_check_decorator` in Python 3.15.
Patch by Jelle Zijlstra.
- Update `typing_extensions.Format` and `typing_extensions.evaluate_forward_ref` to align
with changes in Python 3.14. Patch by Jelle Zijlstra.
- Fix tests for Python 3.14. Patch by Jelle Zijlstra.
- Update `typing_extensions.Format`, `typing_extensions.evaluate_forward_ref`, and
`typing_extensions.TypedDict` to align
with changes in Python 3.14. Patches by Jelle Zijlstra.
- Fix tests for Python 3.14 and 3.15. Patches by Jelle Zijlstra.

New features:

Expand Down
36 changes: 36 additions & 0 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4402,6 +4402,39 @@ class Cat(Animal):
'voice': str,
}

@skipIf(sys.version_info == (3, 14, 0, "beta", 1), "Broken on beta 1, fixed in beta 2")
def test_inheritance_pep563(self):
def _make_td(future, class_name, annos, base, extra_names=None):
lines = []
if future:
lines.append('from __future__ import annotations')
lines.append('from typing import TypedDict')
lines.append(f'class {class_name}({base}):')
for name, anno in annos.items():
lines.append(f' {name}: {anno}')
code = '\n'.join(lines)
ns = {**extra_names} if extra_names else {}
exec(code, ns)
return ns[class_name]

Comment on lines +4407 to +4419
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is all very magical. Wouldn't it make more sense to just hard-code these four fairly short code samples?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, it's eight. So maybe use a template at least?

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 feel that would be more complicated; it would require conditionals on half the lines of the template and textwrap.dedent.

for base_future in (True, False):
for child_future in (True, False):
with self.subTest(base_future=base_future, child_future=child_future):
base = _make_td(
base_future, "Base", {"base": "int"}, "TypedDict"
)
if sys.version_info >= (3, 14):
self.assertIsNotNone(base.__annotate__)
child = _make_td(
child_future, "Child", {"child": "int"}, "Base", {"Base": base}
)
base_anno = typing.ForwardRef("int", module="builtins") if base_future else int
child_anno = typing.ForwardRef("int", module="builtins") if child_future else int
self.assertEqual(base.__annotations__, {'base': base_anno})
self.assertEqual(
child.__annotations__, {'child': child_anno, 'base': base_anno}
)

def test_required_notrequired_keys(self):
self.assertEqual(NontotalMovie.__required_keys__,
frozenset({"title"}))
Expand Down Expand Up @@ -7014,6 +7047,7 @@ class Group(NamedTuple):
self.assertIs(type(a), Group)
self.assertEqual(a, (1, [2]))

@skipUnless(sys.version_info <= (3, 15), "Behavior removed in 3.15")
def test_namedtuple_keyword_usage(self):
with self.assertWarnsRegex(
DeprecationWarning,
Expand Down Expand Up @@ -7049,6 +7083,7 @@ def test_namedtuple_keyword_usage(self):
):
NamedTuple('Name', None, x=int)

@skipUnless(sys.version_info <= (3, 15), "Behavior removed in 3.15")
def test_namedtuple_special_keyword_names(self):
with self.assertWarnsRegex(
DeprecationWarning,
Expand All @@ -7064,6 +7099,7 @@ def test_namedtuple_special_keyword_names(self):
self.assertEqual(a.typename, 'foo')
self.assertEqual(a.fields, [('bar', tuple)])

@skipUnless(sys.version_info <= (3, 15), "Behavior removed in 3.15")
def test_empty_namedtuple(self):
expected_warning = re.escape(
"Failing to pass a value for the 'fields' parameter is deprecated "
Expand Down
8 changes: 5 additions & 3 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,6 +1016,8 @@ def __new__(cls, name, bases, ns, *, total=True, closed=None,
else:
generic_base = ()

ns_annotations = ns.pop('__annotations__', None)

# typing.py generally doesn't let you inherit from plain Generic, unless
# the name of the class happens to be "Protocol"
tp_dict = type.__new__(_TypedDictMeta, "Protocol", (*generic_base, dict), ns)
Expand All @@ -1028,8 +1030,8 @@ def __new__(cls, name, bases, ns, *, total=True, closed=None,

annotations = {}
own_annotate = None
if "__annotations__" in ns:
own_annotations = ns["__annotations__"]
if ns_annotations is not None:
own_annotations = ns_annotations
elif sys.version_info >= (3, 14):
if hasattr(annotationlib, "get_annotate_from_class_namespace"):
own_annotate = annotationlib.get_annotate_from_class_namespace(ns)
Expand Down Expand Up @@ -1119,7 +1121,7 @@ def __annotate__(format):
if base_annotate is None:
continue
base_annos = annotationlib.call_annotate_function(
base.__annotate__, format, owner=base)
base_annotate, format, owner=base)
annos.update(base_annos)
if own_annotate is not None:
own = annotationlib.call_annotate_function(
Expand Down
Loading