Skip to content

Commit e9ee8a1

Browse files
committed
Update PEP 649/749 implementation
1 parent d90a2f4 commit e9ee8a1

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

src/test_typing_extensions.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from _typed_dict_test_helper import Foo, FooGeneric, VeryAnnotated
3030
from typing_extensions import (
3131
_FORWARD_REF_HAS_CLASS,
32-
_PEP_649_OR_749_IMPLEMENTED,
3332
Annotated,
3433
Any,
3534
AnyStr,
@@ -8533,7 +8532,7 @@ def test_stock_annotations_in_module(self):
85338532
get_annotations(isa.MyClass, format=Format.STRING),
85348533
{"a": "int", "b": "str"},
85358534
)
8536-
mycls = "MyClass" if _PEP_649_OR_749_IMPLEMENTED else "inspect_stock_annotations.MyClass"
8535+
mycls = "MyClass" if sys.version_info >= (3, 14) else "inspect_stock_annotations.MyClass"
85378536
self.assertEqual(
85388537
get_annotations(isa.function, format=Format.STRING),
85398538
{"a": "int", "b": "str", "return": mycls},
@@ -8581,7 +8580,7 @@ def test_stock_annotations_on_wrapper(self):
85818580
get_annotations(wrapped, format=Format.FORWARDREF),
85828581
{"a": int, "b": str, "return": isa.MyClass},
85838582
)
8584-
mycls = "MyClass" if _PEP_649_OR_749_IMPLEMENTED else "inspect_stock_annotations.MyClass"
8583+
mycls = "MyClass" if sys.version_info >= (3, 14) else "inspect_stock_annotations.MyClass"
85858584
self.assertEqual(
85868585
get_annotations(wrapped, format=Format.STRING),
85878586
{"a": "int", "b": "str", "return": mycls},

src/typing_extensions.py

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3821,27 +3821,15 @@ def __eq__(self, other: object) -> bool:
38213821
__all__.append("CapsuleType")
38223822

38233823

3824-
# Using this convoluted approach so that this keeps working
3825-
# whether we end up using PEP 649 as written, PEP 749, or
3826-
# some other variation: in any case, inspect.get_annotations
3827-
# will continue to exist and will gain a `format` parameter.
3828-
_PEP_649_OR_749_IMPLEMENTED = (
3829-
hasattr(inspect, 'get_annotations')
3830-
and inspect.get_annotations.__kwdefaults__ is not None
3831-
and "format" in inspect.get_annotations.__kwdefaults__
3832-
)
3833-
3834-
3835-
class Format(enum.IntEnum):
3836-
VALUE = 1
3837-
VALUE_WITH_FAKE_GLOBALS = 2
3838-
FORWARDREF = 3
3839-
STRING = 4
3840-
3841-
3842-
if _PEP_649_OR_749_IMPLEMENTED:
3843-
get_annotations = inspect.get_annotations
3824+
if sys.version_info >= (3,14):
3825+
from annotationlib import get_annotations, Format
38443826
else:
3827+
class Format(enum.IntEnum):
3828+
VALUE = 1
3829+
VALUE_WITH_FAKE_GLOBALS = 2
3830+
FORWARDREF = 3
3831+
STRING = 4
3832+
38453833
def get_annotations(obj, *, globals=None, locals=None, eval_str=False,
38463834
format=Format.VALUE):
38473835
"""Compute the annotations dict for an object.
@@ -4122,7 +4110,7 @@ def evaluate_forward_ref(
41224110
globals=None,
41234111
locals=None,
41244112
type_params=None,
4125-
format=Format.VALUE,
4113+
format=None,
41264114
_recursive_guard=frozenset(),
41274115
):
41284116
"""Evaluate a forward reference as a type hint.

0 commit comments

Comments
 (0)