Skip to content

Compiled class definition involving TypedDict or dataclass causes KeyError for certain annotations #1087

@aatle

Description

@aatle

Bug report
Defining a subclass of TypedDict, or a class decorated with @dataclasses.dataclass, containing at least one field with an annotation that fulfills some conditions, causes problems.
After successfully compiling the module with the class definition, importing the compiled module and causing the class definition to load will raise KeyError: <name of type>.

For the type annotation to cause this error, it must:

  • access a class as an attribute of a module (e.g. using dot operator)
  • this module is defined in a package (a folder), may be the __init__.py module
  • the class definition is not inside the __init__.py of the package (alias is fine)

Examples: json.JSONDecoder, json.decoder.JSONDecoder, pygame.Surface

Related issue: #1075 (fixed for 1.14)

To reproduce
In test.py:

import json
from dataclasses import dataclass
from typing import TypedDict

# @dataclass
class Eggs(TypedDict):
    spam: json.JSONDecoder

Compile successfully:

mypyc test.py

Import, run compiled module

$ python -c "import test"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import test
  File "test.py", line 7, in <module>
    spam: json.JSONDecoder
KeyError: 'JSONDecoder'

Version: mypy 1.15.0 (compiled: yes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions