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:
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)