Skip to content
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
18 changes: 18 additions & 0 deletions Lib/test/test_type_annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,6 +836,24 @@ def test_complex_comprehension_inlining_exec(self):
lamb = list(genexp)[0]
self.assertEqual(lamb(), 42)

@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: '__annotate__' != 'f.__annotate__'
def test_annotate_qualname(self):
code = """
def f() -> None:
def nested() -> None: pass
return nested
class Outer:
x: int
def method(self, x: int):
pass
"""
ns = run_code(code)
method = ns["Outer"].method
self.assertEqual(ns["f"].__annotate__.__qualname__, "f.__annotate__")
self.assertEqual(ns["f"]().__annotate__.__qualname__, "f.<locals>.nested.__annotate__")
self.assertEqual(method.__annotate__.__qualname__, "Outer.method.__annotate__")
self.assertEqual(ns["Outer"].__annotate__.__qualname__, "Outer.__annotate__")

# gh-138349
def test_module_level_annotation_plus_listcomp(self):
cases = [
Expand Down
7 changes: 7 additions & 0 deletions Lib/test/test_type_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ def test_incorrect_mro_explicit_object(self):
with self.assertRaisesRegex(TypeError, r"\(MRO\) for bases object, Generic"):
class My[X](object): ...

def test_compile_error_in_type_param_bound(self):
# This should not crash, see gh-145187
check_syntax_error(
self,
"if True:\n class h[l:{7for*()in 0}]:2"
)


class TypeParamsNonlocalTest(unittest.TestCase):
def test_nonlocal_disallowed_01(self):
Expand Down
28 changes: 23 additions & 5 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import pickle
import re
import sys
from unittest import TestCase, main, skip
from unittest import TestCase, main
from unittest.mock import patch
from copy import copy, deepcopy

Expand Down Expand Up @@ -6691,11 +6691,7 @@ def test_get_type_hints_modules(self):
self.assertEqual(gth(ann_module2), {})
self.assertEqual(gth(ann_module3), {})

@skip("known bug")
def test_get_type_hints_modules_forwardref(self):
# FIXME: This currently exposes a bug in typing. Cached forward references
# don't account for the case where there are multiple types of the same
# name coming from different modules in the same program.
mgc_hints = {'default_a': Optional[mod_generics_cache.A],
'default_b': Optional[mod_generics_cache.B]}
self.assertEqual(gth(mod_generics_cache), mgc_hints)
Expand Down Expand Up @@ -6783,6 +6779,24 @@ def test_get_type_hints_wrapped_decoratored_func(self):
self.assertEqual(gth(ForRefExample.func), expects)
self.assertEqual(gth(ForRefExample.nested), expects)

def test_get_type_hints_wrapped_cycle_self(self):
# gh-146553: __wrapped__ self-reference must raise ValueError,
# not loop forever.
def f(x: int) -> str: ...
f.__wrapped__ = f
with self.assertRaisesRegex(ValueError, 'wrapper loop'):
get_type_hints(f)

def test_get_type_hints_wrapped_cycle_mutual(self):
# gh-146553: mutual __wrapped__ cycle (a -> b -> a) must raise
# ValueError, not loop forever.
def a(): ...
def b(): ...
a.__wrapped__ = b
b.__wrapped__ = a
with self.assertRaisesRegex(ValueError, 'wrapper loop'):
get_type_hints(a)

def test_get_type_hints_annotated(self):
def foobar(x: List['X']): ...
X = Annotated[int, (1, 10)]
Expand Down Expand Up @@ -10850,6 +10864,10 @@ def test_no_attributes(self):
with self.assertRaises(AttributeError):
type(NoDefault).foo

def test_no_subclassing(self):
with self.assertRaises(TypeError):
class Test(type(NoDefault)): ...


class AllTests(BaseTestCase):
"""Tests for __all__."""
Expand Down
4 changes: 4 additions & 0 deletions Lib/typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2416,8 +2416,12 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
else:
nsobj = obj
# Find globalns for the unwrapped object.
seen = {id(nsobj)}
while hasattr(nsobj, '__wrapped__'):
nsobj = nsobj.__wrapped__
if id(nsobj) in seen:
raise ValueError(f'wrapper loop when unwrapping {obj!r}')
seen.add(id(nsobj))
globalns = getattr(nsobj, '__globals__', {})
if localns is None:
localns = globalns
Expand Down
Loading