Skip to content

PEP 649 behavior for partially executed modules #130907

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

Closed
JelleZijlstra opened this issue Mar 6, 2025 · 1 comment
Closed

PEP 649 behavior for partially executed modules #130907

JelleZijlstra opened this issue Mar 6, 2025 · 1 comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@JelleZijlstra
Copy link
Member

JelleZijlstra commented Mar 6, 2025

Bug report

Bug description:

Consider this package:

$ ls recmod/
__main__.py	a.py		b.py
$ cat recmod/__main__.py 
from . import a

print(a.__annotations__)
$ cat recmod/a.py 
v1: int

from . import b

v2: int
$ cat recmod/b.py 
from . import a

print(a.__annotations__)

On 3.13, this produces:

$ python3.13 -m recmod
{'v1': <class 'int'>}
{'v1': <class 'int'>, 'v2': <class 'int'>}

But on main, we get this:

$ ~/py/cpython/python.exe -m recmod
{}
{}

This is because we only set the __annotate__ function at the end of the module execution, so when we access annotations on the partially executed module a (in b.py), there aren't any yet. But this also populates the __annotations__ cache, so even accesses to __annotations__ after a has been fully executed still return an empty dictionary.

Should we fix this and how? I don't care much what happens if you access __annotations__ while the module is partially evaluated. However, it seems bad that such access poisons the cache forever. To fix that, we should make ModuleType.__annotations__ not cache its return value if the module is not yet fully evaluated.

CPython versions tested on:

CPython main branch

Operating systems tested on:

macOS

Linked PRs

@JelleZijlstra JelleZijlstra added the type-bug An unexpected behavior, bug, or error label Mar 6, 2025
@picnixz picnixz added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Mar 7, 2025
JelleZijlstra added a commit to JelleZijlstra/cpython that referenced this issue Mar 21, 2025
@JelleZijlstra
Copy link
Member Author

Actually for the case where __annotations__ is accessed while the module is partially executed, we can simply throw an error. Not quite compatible but arguably better than returning something incorrect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants