Skip to content

Fix test failures on Python 3.14 #566

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 4 commits into from
May 3, 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
40 changes: 28 additions & 12 deletions src/test_typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,10 +882,12 @@ async def coro(self):

class DeprecatedCoroTests(BaseTestCase):
def test_asyncio_iscoroutinefunction(self):
self.assertFalse(asyncio.coroutines.iscoroutinefunction(func))
self.assertFalse(asyncio.coroutines.iscoroutinefunction(Cls.func))
self.assertTrue(asyncio.coroutines.iscoroutinefunction(coro))
self.assertTrue(asyncio.coroutines.iscoroutinefunction(Cls.coro))
with warnings.catch_warnings():
warnings.simplefilter("ignore", DeprecationWarning)
self.assertFalse(asyncio.coroutines.iscoroutinefunction(func))
self.assertFalse(asyncio.coroutines.iscoroutinefunction(Cls.func))
self.assertTrue(asyncio.coroutines.iscoroutinefunction(coro))
self.assertTrue(asyncio.coroutines.iscoroutinefunction(Cls.coro))

@skipUnless(TYPING_3_12_ONLY or TYPING_3_13_0_RC, "inspect.iscoroutinefunction works differently on Python < 3.12")
def test_inspect_iscoroutinefunction(self):
Expand Down Expand Up @@ -7209,7 +7211,7 @@ def test_cannot_instantiate_vars(self):

def test_bound_errors(self):
with self.assertRaises(TypeError):
TypeVar('X', bound=Union)
TypeVar('X', bound=Optional)
with self.assertRaises(TypeError):
TypeVar('X', str, float, bound=Employee)
with self.assertRaisesRegex(TypeError,
Expand Down Expand Up @@ -8194,19 +8196,26 @@ def f2(a: "undefined"): # noqa: F821
get_annotations(f2, format=Format.FORWARDREF),
{"a": "undefined"},
)
self.assertEqual(get_annotations(f2, format=2), {"a": "undefined"})
# Test that the raw int also works
self.assertEqual(
get_annotations(f2, format=Format.FORWARDREF.value),
{"a": "undefined"},
)

self.assertEqual(
get_annotations(f1, format=Format.STRING),
{"a": "int"},
)
self.assertEqual(get_annotations(f1, format=3), {"a": "int"})
self.assertEqual(
get_annotations(f1, format=Format.STRING.value),
{"a": "int"},
)

with self.assertRaises(ValueError):
get_annotations(f1, format=0)

with self.assertRaises(ValueError):
get_annotations(f1, format=4)
get_annotations(f1, format=42)

def test_custom_object_with_annotations(self):
class C:
Expand Down Expand Up @@ -8245,10 +8254,17 @@ def foo(a: int, b: str):
foo.__annotations__ = {"a": "foo", "b": "str"}
for format in Format:
with self.subTest(format=format):
self.assertEqual(
get_annotations(foo, format=format),
{"a": "foo", "b": "str"},
)
if format is Format.VALUE_WITH_FAKE_GLOBALS:
with self.assertRaisesRegex(
ValueError,
"The VALUE_WITH_FAKE_GLOBALS format is for internal use only"
):
get_annotations(foo, format=format)
else:
self.assertEqual(
get_annotations(foo, format=format),
{"a": "foo", "b": "str"},
)

self.assertEqual(
get_annotations(foo, eval_str=True, locals=locals()),
Expand Down
9 changes: 7 additions & 2 deletions src/typing_extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4152,8 +4152,9 @@ def __eq__(self, other: object) -> bool:

class Format(enum.IntEnum):
VALUE = 1
FORWARDREF = 2
STRING = 3
VALUE_WITH_FAKE_GLOBALS = 2
FORWARDREF = 3
STRING = 4


if _PEP_649_OR_749_IMPLEMENTED:
Expand Down Expand Up @@ -4197,6 +4198,10 @@ def get_annotations(obj, *, globals=None, locals=None, eval_str=False,

"""
format = Format(format)
if format is Format.VALUE_WITH_FAKE_GLOBALS:
raise ValueError(
"The VALUE_WITH_FAKE_GLOBALS format is for internal use only"
)

if eval_str and format is not Format.VALUE:
raise ValueError("eval_str=True is only supported with format=Format.VALUE")
Expand Down
Loading