Skip to content

Commit 1da29ff

Browse files
authored
Update typing.py to 3.14.5 (RustPython#7862)
1 parent a702e9c commit 1da29ff

4 files changed

Lines changed: 52 additions & 5 deletions

File tree

Lib/test/test_type_annotations.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,24 @@ def test_complex_comprehension_inlining_exec(self):
836836
lamb = list(genexp)[0]
837837
self.assertEqual(lamb(), 42)
838838

839+
@unittest.expectedFailure # TODO: RUSTPYTHON; AssertionError: '__annotate__' != 'f.__annotate__'
840+
def test_annotate_qualname(self):
841+
code = """
842+
def f() -> None:
843+
def nested() -> None: pass
844+
return nested
845+
class Outer:
846+
x: int
847+
def method(self, x: int):
848+
pass
849+
"""
850+
ns = run_code(code)
851+
method = ns["Outer"].method
852+
self.assertEqual(ns["f"].__annotate__.__qualname__, "f.__annotate__")
853+
self.assertEqual(ns["f"]().__annotate__.__qualname__, "f.<locals>.nested.__annotate__")
854+
self.assertEqual(method.__annotate__.__qualname__, "Outer.method.__annotate__")
855+
self.assertEqual(ns["Outer"].__annotate__.__qualname__, "Outer.__annotate__")
856+
839857
# gh-138349
840858
def test_module_level_annotation_plus_listcomp(self):
841859
cases = [

Lib/test/test_type_params.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,13 @@ def test_incorrect_mro_explicit_object(self):
152152
with self.assertRaisesRegex(TypeError, r"\(MRO\) for bases object, Generic"):
153153
class My[X](object): ...
154154

155+
def test_compile_error_in_type_param_bound(self):
156+
# This should not crash, see gh-145187
157+
check_syntax_error(
158+
self,
159+
"if True:\n class h[l:{7for*()in 0}]:2"
160+
)
161+
155162

156163
class TypeParamsNonlocalTest(unittest.TestCase):
157164
def test_nonlocal_disallowed_01(self):

Lib/test/test_typing.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import pickle
1414
import re
1515
import sys
16-
from unittest import TestCase, main, skip
16+
from unittest import TestCase, main
1717
from unittest.mock import patch
1818
from copy import copy, deepcopy
1919

@@ -6691,11 +6691,7 @@ def test_get_type_hints_modules(self):
66916691
self.assertEqual(gth(ann_module2), {})
66926692
self.assertEqual(gth(ann_module3), {})
66936693

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

6782+
def test_get_type_hints_wrapped_cycle_self(self):
6783+
# gh-146553: __wrapped__ self-reference must raise ValueError,
6784+
# not loop forever.
6785+
def f(x: int) -> str: ...
6786+
f.__wrapped__ = f
6787+
with self.assertRaisesRegex(ValueError, 'wrapper loop'):
6788+
get_type_hints(f)
6789+
6790+
def test_get_type_hints_wrapped_cycle_mutual(self):
6791+
# gh-146553: mutual __wrapped__ cycle (a -> b -> a) must raise
6792+
# ValueError, not loop forever.
6793+
def a(): ...
6794+
def b(): ...
6795+
a.__wrapped__ = b
6796+
b.__wrapped__ = a
6797+
with self.assertRaisesRegex(ValueError, 'wrapper loop'):
6798+
get_type_hints(a)
6799+
67866800
def test_get_type_hints_annotated(self):
67876801
def foobar(x: List['X']): ...
67886802
X = Annotated[int, (1, 10)]
@@ -10850,6 +10864,10 @@ def test_no_attributes(self):
1085010864
with self.assertRaises(AttributeError):
1085110865
type(NoDefault).foo
1085210866

10867+
def test_no_subclassing(self):
10868+
with self.assertRaises(TypeError):
10869+
class Test(type(NoDefault)): ...
10870+
1085310871

1085410872
class AllTests(BaseTestCase):
1085510873
"""Tests for __all__."""

Lib/typing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,8 +2416,12 @@ def get_type_hints(obj, globalns=None, localns=None, include_extras=False,
24162416
else:
24172417
nsobj = obj
24182418
# Find globalns for the unwrapped object.
2419+
seen = {id(nsobj)}
24192420
while hasattr(nsobj, '__wrapped__'):
24202421
nsobj = nsobj.__wrapped__
2422+
if id(nsobj) in seen:
2423+
raise ValueError(f'wrapper loop when unwrapping {obj!r}')
2424+
seen.add(id(nsobj))
24212425
globalns = getattr(nsobj, '__globals__', {})
24222426
if localns is None:
24232427
localns = globalns

0 commit comments

Comments
 (0)