|
| 1 | +"""STRING-format edge cases for annotationlib (gh-157056).""" |
| 2 | + |
| 3 | +import unittest |
| 4 | + |
| 5 | +from annotationlib import Format, get_annotations, type_repr |
| 6 | + |
| 7 | + |
| 8 | +class TestStringFormatSpecialAnnotations(unittest.TestCase): |
| 9 | + def test_dict_comprehension_annotation(self): |
| 10 | + def f(x: {k: v for k, v in items}): |
| 11 | + pass |
| 12 | + |
| 13 | + self.assertEqual( |
| 14 | + get_annotations(f, format=Format.STRING), |
| 15 | + {"x": "{k: v for k, v in items}"}, |
| 16 | + ) |
| 17 | + |
| 18 | + def test_lambda_annotation(self): |
| 19 | + def g(x: lambda q: q): |
| 20 | + pass |
| 21 | + |
| 22 | + g_anno = get_annotations(g, format=Format.STRING) |
| 23 | + self.assertEqual(g_anno, {"x": "lambda q: q"}) |
| 24 | + self.assertNotIn("0x", g_anno["x"].lower()) |
| 25 | + |
| 26 | + def test_generator_expression_annotation(self): |
| 27 | + def h(x: (w for w in seq)): |
| 28 | + pass |
| 29 | + |
| 30 | + h_anno = get_annotations(h, format=Format.STRING) |
| 31 | + self.assertEqual(h_anno, {"x": "(w for w in seq)"}) |
| 32 | + self.assertNotIn("0x", h_anno["x"].lower()) |
| 33 | + |
| 34 | + def test_mixed_annotations(self): |
| 35 | + def mixed(a: int, b: {k: v for k, v in items}, c: lambda q: q): |
| 36 | + pass |
| 37 | + |
| 38 | + self.assertEqual( |
| 39 | + get_annotations(mixed, format=Format.STRING), |
| 40 | + { |
| 41 | + "a": "int", |
| 42 | + "b": "{k: v for k, v in items}", |
| 43 | + "c": "lambda q: q", |
| 44 | + }, |
| 45 | + ) |
| 46 | + |
| 47 | + def test_type_repr_lambda_and_genexpr_have_no_address(self): |
| 48 | + lam = lambda q: q |
| 49 | + self.assertTrue(type_repr(lam).endswith("<lambda>")) |
| 50 | + self.assertNotIn("0x", type_repr(lam).lower()) |
| 51 | + gen = (w for w in ()) |
| 52 | + self.assertTrue(type_repr(gen).endswith("<genexpr>")) |
| 53 | + self.assertNotIn("0x", type_repr(gen).lower()) |
| 54 | + |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + unittest.main() |
0 commit comments