Commit c73021f
[mypyc] Fix
Fixes mypyc/mypyc#972.
In CPython, the class body executes as a function where earlier
assignments are available to later ones e.g this is possible:
```Python3
class Foo:
A: t.ClassVar = {1, 2, 3}
B: t.ClassVar = {4, 5, 6}
C: t.ClassVar = A | B
```
mypyc previously resolved such names via `load_global()`, looking them
up in the module globals dict where they don't exist causing a KeyError
at runtime.
This PR fixes this by tracking `ClassVar` names as they're defined
during class body processing, and redirecting lookups to the class being
built: the type object (`py_get_attr`) for extension classes, or the
class dict (`dict_get_item_op`) for non-extension classes.
---------
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>ClassVar self-references in class bodies (#21011)1 parent ac07166 commit c73021f
File tree
5 files changed
+421
-0
lines changed- mypyc
- irbuild
- test-data
5 files changed
+421
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
236 | 244 | | |
237 | 245 | | |
238 | 246 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
137 | 137 | | |
138 | 138 | | |
139 | 139 | | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
140 | 147 | | |
141 | 148 | | |
142 | 149 | | |
| |||
179 | 186 | | |
180 | 187 | | |
181 | 188 | | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
182 | 192 | | |
183 | 193 | | |
184 | 194 | | |
185 | 195 | | |
186 | 196 | | |
187 | 197 | | |
188 | 198 | | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
189 | 204 | | |
190 | 205 | | |
191 | 206 | | |
| |||
232 | 247 | | |
233 | 248 | | |
234 | 249 | | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
235 | 258 | | |
236 | 259 | | |
237 | 260 | | |
238 | 261 | | |
239 | 262 | | |
240 | 263 | | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
241 | 267 | | |
242 | 268 | | |
243 | 269 | | |
| |||
293 | 319 | | |
294 | 320 | | |
295 | 321 | | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
296 | 325 | | |
297 | 326 | | |
298 | 327 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
213 | 213 | | |
214 | 214 | | |
215 | 215 | | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
216 | 227 | | |
217 | 228 | | |
218 | 229 | | |
| |||
0 commit comments