Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
aatle opened this issue Feb 19, 2025 · 0 comments · May be fixed by python/mypy#18874

Comments

@aatle
Copy link

aatle commented Feb 19, 2025

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant