Debian Codegraph @

pypy/interpreter/test/apptest_annotations.py

raw symbols SCIP
1 import pytest
2
3 def test_toplevel_annotation():
4 # exec because this needs to be in "top level" scope
5 exec("""if True:
6 a: int
7 assert __annotations__['a'] == int
8 """)
9
10 def test_toplevel_invalid():
11 exec("""if True:
12 with pytest.raises(NameError):
13 a: invalid
14 """)
15
16 def test_non_simple_annotation():
17 class C:
18 (a): int
19 assert "a" not in __annotations__
20
21 def test_simple_with_target():
22 class C:
23 a: int = 1
24 assert __annotations__["a"] == int
25 assert a == 1
26
27 def test_attribute_target():
28 class C:
29 a = 1
30 a.x: int
31 assert __annotations__ == {}
32
33 def test_subscript_target():
34 # ensure that these type annotations don't raise exceptions
35 # during compilation
36 class C:
37 a = 1
38 a[0]: int
39 a[1:2]: int
40 a[1:2:2]: int
41 a[1:2:2, ...]: int
42 assert __annotations__ == {}
43
44 def test_class_annotation():
45 class C:
46 a: int
47 b: str
48 assert "__annotations__" in locals()
49 assert C.__annotations__ == {"a": int, "b": str}
50
51 def test_unevaluated_name():
52 class C:
53 def __init__(self):
54 self.x: invalid_name = 1
55 assert self.x == 1
56 C()
57
58 def test_nonexistent_target():
59 with pytest.raises(NameError):
60 y[0]: invalid
61
62 def test_non_simple_func_annotation():
63 a = 5
64 def f():
65 (a): int
66 return a
67 assert f() == 5
68
69 def test_repeated_setup():
70 # each exec will run another SETUP_ANNOTATIONS
71 # we want to confirm that this doesn't blow away
72 # the previous __annotations__
73 d = {}
74 exec('a: int', d)
75 exec('b: int', d)
76 exec('assert __annotations__ == {"a": int, "b": int}', d)
77
78 def test_function_no___annotations__():
79 a: int
80 assert "__annotations__" not in locals()
81
82 def test_unboundlocal():
83 # a simple variable annotation implies its target is a local
84 a: int
85 with pytest.raises(UnboundLocalError):
86 print(a)
87
88 def test_ternary_expression_bug():
89 class C:
90 var: bool = True if False else False
91 assert var is False
92 assert C.__annotations__ == {"var": bool}
93
94 def test_reassigned___annotations__():
95 class C:
96 __annotations__ = None
97 with pytest.raises(TypeError):
98 a: int
99
100 def test_locals_arent_dicts():
101 class O:
102 def __init__(self):
103 self.dct = {}
104
105 def __getitem__(self, name):
106 return self.dct[name]
107
108 def __setitem__(self, name, value):
109 self.dct[name] = value
110
111 # don't crash if locals aren't just a normal dict
112 exec("a: int; assert __annotations__['a'] == int", {}, O())
113
114
115 def test_annotations_leak_upwards():
116 # test weird corner case of the new 3.7 implementation of annotations:
117 # if we delete __annotations__ in a class, the annotation will end up in
118 # the module's __annotations__ see https://bugs.python.org/issue32550
119 d = {}
120 exec("""if 1:
121 b : int
122 class A:
123 del __annotations__
124 a: int
125 assert __annotations__['a'] is int
126 """, d)
127
128 def test_del_global_not_found_regression():
129 with pytest.raises(NameError) as excinfo:
130 exec("del notthere", {}, {})
131 assert "notthere" in str(excinfo.value)
132
133 def test_lineno():
134 s = """
135
136 a: int
137 """
138 c = compile(s, "f", "exec")
139 assert c.co_firstlineno == 1
140
141 def test_scoping():
142 def f(classvar):
143 class C:
144 cls: classvar = 23
145 assert C.__annotations__ == {"cls": "abc"}
146
147 f("abc")
148
149 def test_side_effects():
150 calls = 0
151 def foo():
152 nonlocal calls
153 calls += 1
154 foo()
155 assert calls == 1
156 exec("foo()")
157 assert calls == 2
158 d = {}
159 exec("a: foo() = 1")
160 exec("b: foo()")
161 assert calls == 4
162 c = None
163 exec("c[foo()]: int")
164 assert calls == 5
165 def g():
166 c = None
167 c[foo()]: int
168 assert calls == 5
169 g()
170 assert calls == 6
171
172 def test_side_effects_2():
173 calls = 0
174 def foo():
175 nonlocal calls
176 calls += 1
177 d = {}
178 exec("d[foo()]: int = 1")
179 assert d[None] == 1
180 assert calls == 1
181
182
183 def test_class_mangling():
184 class C:
185 __mangled: int
186 assert C.__annotations__ == {"_C__mangled": int}
187
188 def test_global_annotion_bug():
189 exec("""
190 def set():
191 global name
192 name:int
193 assert __annotations__['name'] == int
194 """)
195
196 def test_lazy_annotation_creation():
197 for obj in [type(pytest)("abc"), type("A", (), {})]:
198 assert '__annotations__' not in obj.__dict__
199 assert obj.__annotations__ == {}
200 assert '__annotations__' in obj.__dict__
201 x = []
202 obj.__annotations__ = x
203 assert obj.__dict__['__annotations__'] is obj.__annotations__ is x
204 del obj.__annotations__
205 assert '__annotations__' not in obj.__dict__
206
207 def test_annotation_builtin_type():
208 with pytest.raises(AttributeError):
209 int.__annotations__
210 with pytest.raises(TypeError):
211 int.__annotations__ = 12
212 assert getattr(int, "__annotations__", None) is None