Skip to content

Class level scoping is miscompiled #862

@JelleZijlstra

Description

@JelleZijlstra

Compiling the below:

from typing import Final

class StringParser:
    DEFAULT_TOKEN: Final = -1
    d: Final = {DEFAULT_TOKEN: 1}

And running it produces:

$ mypyc fud.py
$ python -c 'import fud'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "fud.py", line 5, in <module>
    d: Final = {DEFAULT_TOKEN: 1}
KeyError: 'DEFAULT_TOKEN'

The relevant snippet of C code is:

    cpy_r_r39 = CPyStatic_globals;
    cpy_r_r40 = CPyStatics[3]; /* 'DEFAULT_TOKEN' */
    cpy_r_r41 = CPyDict_GetItem(cpy_r_r39, cpy_r_r40);

This looks like it's looking up the name in the globals dict instead of the class dict.

It doesn't reproduce with a positive integer, float, or string Final.

This is minimized from code in Black, originally found by @ichard26.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions