Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
Description
Bug report
Bug description:
Consider this package:
$ ls recmod/__main__.pya.pyb.py$ cat recmod/__main__.py from . import aprint(a.__annotations__)$ cat recmod/a.py v1: intfrom . import bv2: int$ cat recmod/b.py from . import aprint(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 (inb.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 makeModuleType.__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