Debian Codegraph @

Lib/test/test_type_annotations.py

raw
1 import annotationlib
2 import inspect
3 import textwrap
4 import types
5 import unittest
6 from test.support import run_code, check_syntax_error, import_helper, cpython_only
7 from test.test_inspect import inspect_stringized_annotations
8
9
10 class TypeAnnotationTests(unittest.TestCase):
11
12 def test_lazy_create_annotations(self):
13 # type objects lazy create their __annotations__ dict on demand.
14 # the annotations dict is stored in type.__dict__ (as __annotations_cache__).
15 # a freshly created type shouldn't have an annotations dict yet.
16 foo = type("Foo", (), {})
17 for i in range(3):
18 self.assertFalse("__annotations_cache__" in foo.__dict__)
19 d = foo.__annotations__
20 self.assertTrue("__annotations_cache__" in foo.__dict__)
21 self.assertEqual(foo.__annotations__, d)
22 self.assertEqual(foo.__dict__['__annotations_cache__'], d)
23 del foo.__annotations__
24
25 def test_setting_annotations(self):
26 foo = type("Foo", (), {})
27 for i in range(3):
28 self.assertFalse("__annotations_cache__" in foo.__dict__)
29 d = {'a': int}
30 foo.__annotations__ = d
31 self.assertTrue("__annotations_cache__" in foo.__dict__)
32 self.assertEqual(foo.__annotations__, d)
33 self.assertEqual(foo.__dict__['__annotations_cache__'], d)
34 del foo.__annotations__
35
36 def test_annotations_getset_raises(self):
37 # builtin types don't have __annotations__ (yet!)
38 with self.assertRaises(AttributeError):
39 print(float.__annotations__)
40 with self.assertRaises(TypeError):
41 float.__annotations__ = {}
42 with self.assertRaises(TypeError):
43 del float.__annotations__
44
45 # double delete
46 foo = type("Foo", (), {})
47 foo.__annotations__ = {}
48 del foo.__annotations__
49 with self.assertRaises(AttributeError):
50 del foo.__annotations__
51
52 def test_annotations_are_created_correctly(self):
53 class C:
54 a:int=3
55 b:str=4
56 self.assertEqual(C.__annotations__, {"a": int, "b": str})
57 self.assertTrue("__annotations_cache__" in C.__dict__)
58 del C.__annotations__
59 self.assertFalse("__annotations_cache__" in C.__dict__)
60
61 def test_pep563_annotations(self):
62 isa = inspect_stringized_annotations
63 self.assertEqual(
64 isa.__annotations__, {"a": "int", "b": "str"},
65 )
66 self.assertEqual(
67 isa.MyClass.__annotations__, {"a": "int", "b": "str"},
68 )
69
70 def test_explicitly_set_annotations(self):
71 class C:
72 __annotations__ = {"what": int}
73 self.assertEqual(C.__annotations__, {"what": int})
74
75 def test_explicitly_set_annotate(self):
76 class C:
77 __annotate__ = lambda format: {"what": int}
78 self.assertEqual(C.__annotations__, {"what": int})
79 self.assertIsInstance(C.__annotate__, types.FunctionType)
80 self.assertEqual(C.__annotate__(annotationlib.Format.VALUE), {"what": int})
81
82 def test_del_annotations_and_annotate(self):
83 # gh-132285
84 called = False
85 class A:
86 def __annotate__(format):
87 nonlocal called
88 called = True
89 return {'a': int}
90
91 self.assertEqual(A.__annotations__, {'a': int})
92 self.assertTrue(called)
93 self.assertTrue(A.__annotate__)
94
95 del A.__annotations__
96 called = False
97
98 self.assertEqual(A.__annotations__, {})
99 self.assertFalse(called)
100 self.assertIs(A.__annotate__, None)
101
102 def test_descriptor_still_works(self):
103 class C:
104 def __init__(self, name=None, bases=None, d=None):
105 self.my_annotations = None
106
107 @property
108 def __annotations__(self):
109 if not hasattr(self, 'my_annotations'):
110 self.my_annotations = {}
111 if not isinstance(self.my_annotations, dict):
112 self.my_annotations = {}
113 return self.my_annotations
114
115 @__annotations__.setter
116 def __annotations__(self, value):
117 if not isinstance(value, dict):
118 raise ValueError("can only set __annotations__ to a dict")
119 self.my_annotations = value
120
121 @__annotations__.deleter
122 def __annotations__(self):
123 if getattr(self, 'my_annotations', False) is None:
124 raise AttributeError('__annotations__')
125 self.my_annotations = None
126
127 c = C()
128 self.assertEqual(c.__annotations__, {})
129 d = {'a':'int'}
130 c.__annotations__ = d
131 self.assertEqual(c.__annotations__, d)
132 with self.assertRaises(ValueError):
133 c.__annotations__ = 123
134 del c.__annotations__
135 with self.assertRaises(AttributeError):
136 del c.__annotations__
137 self.assertEqual(c.__annotations__, {})
138
139
140 class D(metaclass=C):
141 pass
142
143 self.assertEqual(D.__annotations__, {})
144 d = {'a':'int'}
145 D.__annotations__ = d
146 self.assertEqual(D.__annotations__, d)
147 with self.assertRaises(ValueError):
148 D.__annotations__ = 123
149 del D.__annotations__
150 with self.assertRaises(AttributeError):
151 del D.__annotations__
152 self.assertEqual(D.__annotations__, {})
153
154 def test_partially_executed_module(self):
155 partialexe = import_helper.import_fresh_module("test.typinganndata.partialexecution")
156 self.assertEqual(
157 partialexe.a.__annotations__,
158 {"v1": int, "v2": int},
159 )
160 self.assertEqual(partialexe.b.annos, {"v1": int})
161
162 @cpython_only
163 def test_no_cell(self):
164 # gh-130924: Test that uses of annotations in local scopes do not
165 # create cell variables.
166 def f(x):
167 a: x
168 return x
169
170 self.assertEqual(f.__code__.co_cellvars, ())
171
172
173 def build_module(code: str, name: str = "top") -> types.ModuleType:
174 ns = run_code(code)
175 mod = types.ModuleType(name)
176 mod.__dict__.update(ns)
177 return mod
178
179
180 class TestSetupAnnotations(unittest.TestCase):
181 def check(self, code: str):
182 code = textwrap.dedent(code)
183 for scope in ("module", "class"):
184 with self.subTest(scope=scope):
185 if scope == "class":
186 code = f"class C:\n{textwrap.indent(code, ' ')}"
187 ns = run_code(code)
188 annotations = ns["C"].__annotations__
189 else:
190 annotations = build_module(code).__annotations__
191 self.assertEqual(annotations, {"x": int})
192
193 def test_top_level(self):
194 self.check("x: int = 1")
195
196 def test_blocks(self):
197 self.check("if True:\n x: int = 1")
198 self.check("""
199 while True:
200 x: int = 1
201 break
202 """)
203 self.check("""
204 while False:
205 pass
206 else:
207 x: int = 1
208 """)
209 self.check("""
210 for i in range(1):
211 x: int = 1
212 """)
213 self.check("""
214 for i in range(1):
215 pass
216 else:
217 x: int = 1
218 """)
219
220 def test_try(self):
221 self.check("""
222 try:
223 x: int = 1
224 except:
225 pass
226 """)
227 self.check("""
228 try:
229 pass
230 except:
231 pass
232 else:
233 x: int = 1
234 """)
235 self.check("""
236 try:
237 pass
238 except:
239 pass
240 finally:
241 x: int = 1
242 """)
243 self.check("""
244 try:
245 1/0
246 except:
247 x: int = 1
248 """)
249
250 def test_try_star(self):
251 self.check("""
252 try:
253 x: int = 1
254 except* Exception:
255 pass
256 """)
257 self.check("""
258 try:
259 pass
260 except* Exception:
261 pass
262 else:
263 x: int = 1
264 """)
265 self.check("""
266 try:
267 pass
268 except* Exception:
269 pass
270 finally:
271 x: int = 1
272 """)
273 self.check("""
274 try:
275 1/0
276 except* Exception:
277 x: int = 1
278 """)
279
280 def test_match(self):
281 self.check("""
282 match 0:
283 case 0:
284 x: int = 1
285 """)
286
287
288 class AnnotateTests(unittest.TestCase):
289 """See PEP 649."""
290 def test_manual_annotate(self):
291 def f():
292 pass
293 mod = types.ModuleType("mod")
294 class X:
295 pass
296
297 for obj in (f, mod, X):
298 with self.subTest(obj=obj):
299 self.check_annotations(obj)
300
301 def check_annotations(self, f):
302 self.assertEqual(f.__annotations__, {})
303 self.assertIs(f.__annotate__, None)
304
305 with self.assertRaisesRegex(TypeError, "__annotate__ must be callable or None"):
306 f.__annotate__ = 42
307 f.__annotate__ = lambda: 42
308 with self.assertRaisesRegex(TypeError, r"takes 0 positional arguments but 1 was given"):
309 print(f.__annotations__)
310
311 f.__annotate__ = lambda x: 42
312 with self.assertRaisesRegex(TypeError, r"__annotate__ returned non-dict of type 'int'"):
313 print(f.__annotations__)
314
315 f.__annotate__ = lambda x: {"x": x}
316 self.assertEqual(f.__annotations__, {"x": 1})
317
318 # Setting annotate to None does not invalidate the cached __annotations__
319 f.__annotate__ = None
320 self.assertEqual(f.__annotations__, {"x": 1})
321
322 # But setting it to a new callable does
323 f.__annotate__ = lambda x: {"y": x}
324 self.assertEqual(f.__annotations__, {"y": 1})
325
326 # Setting f.__annotations__ also clears __annotate__
327 f.__annotations__ = {"z": 43}
328 self.assertIs(f.__annotate__, None)
329
330 def test_user_defined_annotate(self):
331 class X:
332 a: int
333
334 def __annotate__(format):
335 return {"a": str}
336 self.assertEqual(X.__annotate__(annotationlib.Format.VALUE), {"a": str})
337 self.assertEqual(annotationlib.get_annotations(X), {"a": str})
338
339 mod = build_module(
340 """
341 a: int
342 def __annotate__(format):
343 return {"a": str}
344 """
345 )
346 self.assertEqual(mod.__annotate__(annotationlib.Format.VALUE), {"a": str})
347 self.assertEqual(annotationlib.get_annotations(mod), {"a": str})
348
349
350 class DeferredEvaluationTests(unittest.TestCase):
351 def test_function(self):
352 def func(x: undefined, /, y: undefined, *args: undefined, z: undefined, **kwargs: undefined) -> undefined:
353 pass
354
355 with self.assertRaises(NameError):
356 func.__annotations__
357
358 undefined = 1
359 self.assertEqual(func.__annotations__, {
360 "x": 1,
361 "y": 1,
362 "args": 1,
363 "z": 1,
364 "kwargs": 1,
365 "return": 1,
366 })
367
368 def test_async_function(self):
369 async def func(x: undefined, /, y: undefined, *args: undefined, z: undefined, **kwargs: undefined) -> undefined:
370 pass
371
372 with self.assertRaises(NameError):
373 func.__annotations__
374
375 undefined = 1
376 self.assertEqual(func.__annotations__, {
377 "x": 1,
378 "y": 1,
379 "args": 1,
380 "z": 1,
381 "kwargs": 1,
382 "return": 1,
383 })
384
385 def test_class(self):
386 class X:
387 a: undefined
388
389 with self.assertRaises(NameError):
390 X.__annotations__
391
392 undefined = 1
393 self.assertEqual(X.__annotations__, {"a": 1})
394
395 def test_module(self):
396 ns = run_code("x: undefined = 1")
397 anno = ns["__annotate__"]
398 with self.assertRaises(NotImplementedError):
399 anno(3)
400
401 with self.assertRaises(NameError):
402 anno(1)
403
404 ns["undefined"] = 1
405 self.assertEqual(anno(1), {"x": 1})
406
407 def test_class_scoping(self):
408 class Outer:
409 def meth(self, x: Nested): ...
410 x: Nested
411 class Nested: ...
412
413 self.assertEqual(Outer.meth.__annotations__, {"x": Outer.Nested})
414 self.assertEqual(Outer.__annotations__, {"x": Outer.Nested})
415
416 def test_no_exotic_expressions(self):
417 preludes = [
418 "",
419 "class X:\n ",
420 "def f():\n ",
421 "async def f():\n ",
422 ]
423 for prelude in preludes:
424 with self.subTest(prelude=prelude):
425 check_syntax_error(self, prelude + "def func(x: (yield)): ...", "yield expression cannot be used within an annotation")
426 check_syntax_error(self, prelude + "def func(x: (yield from x)): ...", "yield expression cannot be used within an annotation")
427 check_syntax_error(self, prelude + "def func(x: (y := 3)): ...", "named expression cannot be used within an annotation")
428 check_syntax_error(self, prelude + "def func(x: (await 42)): ...", "await expression cannot be used within an annotation")
429 check_syntax_error(self, prelude + "def func(x: [y async for y in x]): ...", "asynchronous comprehension outside of an asynchronous function")
430 check_syntax_error(self, prelude + "def func(x: {y async for y in x}): ...", "asynchronous comprehension outside of an asynchronous function")
431 check_syntax_error(self, prelude + "def func(x: {y: y async for y in x}): ...", "asynchronous comprehension outside of an asynchronous function")
432
433 def test_no_exotic_expressions_in_unevaluated_annotations(self):
434 preludes = [
435 "",
436 "class X: ",
437 "def f(): ",
438 "async def f(): ",
439 ]
440 for prelude in preludes:
441 with self.subTest(prelude=prelude):
442 check_syntax_error(self, prelude + "(x): (yield)", "yield expression cannot be used within an annotation")
443 check_syntax_error(self, prelude + "(x): (yield from x)", "yield expression cannot be used within an annotation")
444 check_syntax_error(self, prelude + "(x): (y := 3)", "named expression cannot be used within an annotation")
445 check_syntax_error(self, prelude + "(x): (__debug__ := 3)", "named expression cannot be used within an annotation")
446 check_syntax_error(self, prelude + "(x): (await 42)", "await expression cannot be used within an annotation")
447 check_syntax_error(self, prelude + "(x): [y async for y in x]", "asynchronous comprehension outside of an asynchronous function")
448 check_syntax_error(self, prelude + "(x): {y async for y in x}", "asynchronous comprehension outside of an asynchronous function")
449 check_syntax_error(self, prelude + "(x): {y: y async for y in x}", "asynchronous comprehension outside of an asynchronous function")
450
451 def test_ignore_non_simple_annotations(self):
452 ns = run_code("class X: (y): int")
453 self.assertEqual(ns["X"].__annotations__, {})
454 ns = run_code("class X: int.b: int")
455 self.assertEqual(ns["X"].__annotations__, {})
456 ns = run_code("class X: int[str]: int")
457 self.assertEqual(ns["X"].__annotations__, {})
458
459 def test_generated_annotate(self):
460 def func(x: int):
461 pass
462 class X:
463 x: int
464 mod = build_module("x: int")
465 for obj in (func, X, mod):
466 with self.subTest(obj=obj):
467 annotate = obj.__annotate__
468 self.assertIsInstance(annotate, types.FunctionType)
469 self.assertEqual(annotate.__name__, "__annotate__")
470 with self.assertRaises(NotImplementedError):
471 annotate(annotationlib.Format.FORWARDREF)
472 with self.assertRaises(NotImplementedError):
473 annotate(annotationlib.Format.STRING)
474 with self.assertRaises(TypeError):
475 annotate(None)
476 self.assertEqual(annotate(annotationlib.Format.VALUE), {"x": int})
477
478 sig = inspect.signature(annotate)
479 self.assertEqual(sig, inspect.Signature([
480 inspect.Parameter("format", inspect.Parameter.POSITIONAL_ONLY)
481 ]))
482
483 def test_comprehension_in_annotation(self):
484 # This crashed in an earlier version of the code
485 ns = run_code("x: [y for y in range(10)]")
486 self.assertEqual(ns["__annotate__"](1), {"x": list(range(10))})
487
488 def test_future_annotations(self):
489 code = """
490 from __future__ import annotations
491
492 def f(x: int) -> int: pass
493 """
494 ns = run_code(code)
495 f = ns["f"]
496 self.assertIsInstance(f.__annotate__, types.FunctionType)
497 annos = {"x": "int", "return": "int"}
498 self.assertEqual(f.__annotate__(annotationlib.Format.VALUE), annos)
499 self.assertEqual(f.__annotations__, annos)
500
501 def test_set_annotations(self):
502 function_code = textwrap.dedent("""
503 def f(x: int):
504 pass
505 """)
506 class_code = textwrap.dedent("""
507 class f:
508 x: int
509 """)
510 for future in (False, True):
511 for label, code in (("function", function_code), ("class", class_code)):
512 with self.subTest(future=future, label=label):
513 if future:
514 code = "from __future__ import annotations\n" + code
515 ns = run_code(code)
516 f = ns["f"]
517 anno = "int" if future else int
518 self.assertEqual(f.__annotations__, {"x": anno})
519
520 f.__annotations__ = {"x": str}
521 self.assertEqual(f.__annotations__, {"x": str})
522
523 def test_name_clash_with_format(self):
524 # this test would fail if __annotate__'s parameter was called "format"
525 # during symbol table construction
526 code = """
527 class format: pass
528
529 def f(x: format): pass
530 """
531 ns = run_code(code)
532 f = ns["f"]
533 self.assertEqual(f.__annotations__, {"x": ns["format"]})
534
535 code = """
536 class Outer:
537 class format: pass
538
539 def meth(self, x: format): ...
540 """
541 ns = run_code(code)
542 self.assertEqual(ns["Outer"].meth.__annotations__, {"x": ns["Outer"].format})
543
544 code = """
545 def f(format):
546 def inner(x: format): pass
547 return inner
548 res = f("closure var")
549 """
550 ns = run_code(code)
551 self.assertEqual(ns["res"].__annotations__, {"x": "closure var"})
552
553 code = """
554 def f(x: format):
555 pass
556 """
557 ns = run_code(code)
558 # picks up the format() builtin
559 self.assertEqual(ns["f"].__annotations__, {"x": format})
560
561 code = """
562 def outer():
563 def f(x: format):
564 pass
565 if False:
566 class format: pass
567 return f
568 f = outer()
569 """
570 ns = run_code(code)
571 with self.assertRaisesRegex(
572 NameError,
573 "cannot access free variable 'format' where it is not associated with a value in enclosing scope",
574 ):
575 ns["f"].__annotations__
576
577
578 class ConditionalAnnotationTests(unittest.TestCase):
579 def check_scopes(self, code, true_annos, false_annos):
580 for scope in ("class", "module"):
581 for (cond, expected) in (
582 # Constants (so code might get optimized out)
583 (True, true_annos), (False, false_annos),
584 # Non-constant expressions
585 ("not not len", true_annos), ("not len", false_annos),
586 ):
587 with self.subTest(scope=scope, cond=cond):
588 code_to_run = code.format(cond=cond)
589 if scope == "class":
590 code_to_run = "class Cls:\n" + textwrap.indent(textwrap.dedent(code_to_run), " " * 4)
591 ns = run_code(code_to_run)
592 if scope == "class":
593 self.assertEqual(ns["Cls"].__annotations__, expected)
594 else:
595 self.assertEqual(ns["__annotate__"](annotationlib.Format.VALUE),
596 expected)
597
598 def test_with(self):
599 code = """
600 class Swallower:
601 def __enter__(self):
602 pass
603
604 def __exit__(self, *args):
605 return True
606
607 with Swallower():
608 if {cond}:
609 about_to_raise: int
610 raise Exception
611 in_with: "with"
612 """
613 self.check_scopes(code, {"about_to_raise": int}, {"in_with": "with"})
614
615 def test_simple_if(self):
616 code = """
617 if {cond}:
618 in_if: "if"
619 else:
620 in_if: "else"
621 """
622 self.check_scopes(code, {"in_if": "if"}, {"in_if": "else"})
623
624 def test_if_elif(self):
625 code = """
626 if not len:
627 in_if: "if"
628 elif {cond}:
629 in_elif: "elif"
630 else:
631 in_else: "else"
632 """
633 self.check_scopes(
634 code,
635 {"in_elif": "elif"},
636 {"in_else": "else"}
637 )
638
639 def test_try(self):
640 code = """
641 try:
642 if {cond}:
643 raise Exception
644 in_try: "try"
645 except Exception:
646 in_except: "except"
647 finally:
648 in_finally: "finally"
649 """
650 self.check_scopes(
651 code,
652 {"in_except": "except", "in_finally": "finally"},
653 {"in_try": "try", "in_finally": "finally"}
654 )
655
656 def test_try_star(self):
657 code = """
658 try:
659 if {cond}:
660 raise Exception
661 in_try_star: "try"
662 except* Exception:
663 in_except_star: "except"
664 finally:
665 in_finally: "finally"
666 """
667 self.check_scopes(
668 code,
669 {"in_except_star": "except", "in_finally": "finally"},
670 {"in_try_star": "try", "in_finally": "finally"}
671 )
672
673 def test_while(self):
674 code = """
675 while {cond}:
676 in_while: "while"
677 break
678 else:
679 in_else: "else"
680 """
681 self.check_scopes(
682 code,
683 {"in_while": "while"},
684 {"in_else": "else"}
685 )
686
687 def test_for(self):
688 code = """
689 for _ in ([1] if {cond} else []):
690 in_for: "for"
691 else:
692 in_else: "else"
693 """
694 self.check_scopes(
695 code,
696 {"in_for": "for", "in_else": "else"},
697 {"in_else": "else"}
698 )
699
700 def test_match(self):
701 code = """
702 match {cond}:
703 case True:
704 x: "true"
705 case False:
706 x: "false"
707 """
708 self.check_scopes(
709 code,
710 {"x": "true"},
711 {"x": "false"}
712 )
713
714 def test_nesting_override(self):
715 code = """
716 if {cond}:
717 x: "foo"
718 if {cond}:
719 x: "bar"
720 """
721 self.check_scopes(
722 code,
723 {"x": "bar"},
724 {}
725 )
726
727 def test_nesting_outer(self):
728 code = """
729 if {cond}:
730 outer_before: "outer_before"
731 if len:
732 inner_if: "inner_if"
733 else:
734 inner_else: "inner_else"
735 outer_after: "outer_after"
736 """
737 self.check_scopes(
738 code,
739 {"outer_before": "outer_before", "inner_if": "inner_if",
740 "outer_after": "outer_after"},
741 {}
742 )
743
744 def test_nesting_inner(self):
745 code = """
746 if len:
747 outer_before: "outer_before"
748 if {cond}:
749 inner_if: "inner_if"
750 else:
751 inner_else: "inner_else"
752 outer_after: "outer_after"
753 """
754 self.check_scopes(
755 code,
756 {"outer_before": "outer_before", "inner_if": "inner_if",
757 "outer_after": "outer_after"},
758 {"outer_before": "outer_before", "inner_else": "inner_else",
759 "outer_after": "outer_after"},
760 )
761
762 def test_non_name_annotations(self):
763 code = """
764 before: "before"
765 if {cond}:
766 a = "x"
767 a[0]: int
768 else:
769 a = object()
770 a.b: str
771 after: "after"
772 """
773 expected = {"before": "before", "after": "after"}
774 self.check_scopes(code, expected, expected)
775
776
777 class RegressionTests(unittest.TestCase):
778 # gh-132479
779 def test_complex_comprehension_inlining(self):
780 # Test that the various repro cases from the issue don't crash
781 cases = [
782 """
783 (unique_name_0): 0
784 unique_name_1: (
785 0
786 for (
787 0
788 for unique_name_2 in 0
789 for () in (0 for unique_name_3 in unique_name_4 for unique_name_5 in name_1)
790 ).name_3 in {0: 0 for name_1 in unique_name_8}
791 if name_1
792 )
793 """,
794 """
795 unique_name_0: 0
796 unique_name_1: {
797 0: 0
798 for unique_name_2 in [0 for name_0 in unique_name_4]
799 if {
800 0: 0
801 for unique_name_5 in 0
802 if name_0
803 if ((name_0 for unique_name_8 in unique_name_9) for [] in 0)
804 }
805 }
806 """,
807 """
808 0[0]: {0 for name_0 in unique_name_1}
809 unique_name_2: {
810 0: (lambda: name_0 for unique_name_4 in unique_name_5)
811 for unique_name_6 in ()
812 if name_0
813 }
814 """,
815 ]
816 for case in cases:
817 case = textwrap.dedent(case)
818 compile(case, "<test>", "exec")
819
820 def test_complex_comprehension_inlining_exec(self):
821 code = """
822 unique_name_1 = unique_name_5 = [1]
823 name_0 = 42
824 unique_name_7: {name_0 for name_0 in unique_name_1}
825 unique_name_2: {
826 0: (lambda: name_0 for unique_name_4 in unique_name_5)
827 for unique_name_6 in [1]
828 if name_0
829 }
830 """
831 mod = build_module(code)
832 annos = mod.__annotations__
833 self.assertEqual(annos.keys(), {"unique_name_7", "unique_name_2"})
834 self.assertEqual(annos["unique_name_7"], {True})
835 genexp = annos["unique_name_2"][0]
836 lamb = list(genexp)[0]
837 self.assertEqual(lamb(), 42)
838
839 def test_annotate_qualname(self):
840 code = """
841 def f() -> None:
842 def nested() -> None: pass
843 return nested
844 class Outer:
845 x: int
846 def method(self, x: int):
847 pass
848 """
849 ns = run_code(code)
850 method = ns["Outer"].method
851 self.assertEqual(ns["f"].__annotate__.__qualname__, "f.__annotate__")
852 self.assertEqual(ns["f"]().__annotate__.__qualname__, "f.<locals>.nested.__annotate__")
853 self.assertEqual(method.__annotate__.__qualname__, "Outer.method.__annotate__")
854 self.assertEqual(ns["Outer"].__annotate__.__qualname__, "Outer.__annotate__")
855
856 # gh-138349
857 def test_module_level_annotation_plus_listcomp(self):
858 cases = [
859 """
860 def report_error():
861 pass
862 try:
863 [0 for name_2 in unique_name_0 if (lambda: name_2)]
864 except:
865 pass
866 annotated_name: 0
867 """,
868 """
869 class Generic:
870 pass
871 try:
872 [0 for name_2 in unique_name_0 if (0 for unique_name_1 in unique_name_2 for unique_name_3 in name_2)]
873 except:
874 pass
875 annotated_name: 0
876 """,
877 """
878 class Generic:
879 pass
880 annotated_name: 0
881 try:
882 [0 for name_2 in [[0]] for unique_name_1 in unique_name_2 if (lambda: name_2)]
883 except:
884 pass
885 """,
886 ]
887 for code in cases:
888 with self.subTest(code=code):
889 mod = build_module(code)
890 annos = mod.__annotations__
891 self.assertEqual(annos, {"annotated_name": 0})