The types of `builtins.locals` and `types.FrameType.f_locals` aren't correct
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.1k
- Forks
- 2.1k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 82
Description
builtins.locals and types.FrameType.f_locals are currently typed with dict[str, Any], which is incorrect:
-
Counterexample:
from collections import UserDict class A(type): @classmethod def __prepare__(cls, name, bases): return UserDict() def __new__(mcs, name, bases, namespace, /, **kw): return super().__new__(mcs, name, bases, dict(namespace), **kw) class B(metaclass=A): assert isinstance(locals(), dict) # AssertionError -
Counterexample as a consequence of PEP 667 (Python >= 3.13):
from inspect import currentframe def f(): print(type(currentframe().f_locals)) # <class 'FrameLocalsProxy'> (runtime type is `_collections_abc.framelocalsproxy`; not in `.pyi` stubs yet) assert isinstance(currentframe().f_locals, dict) # AssertionError f()
However, I'm not sure what the actual type should be. From what I can gather, the type should implement a mapping interface, with a few (but not all?) MutableMapping methods (PEP 667 describes what these mutable mapping methods are for FrameType.f_locals, but I'm not sure what's exactly expected of builtins.locals()).
In addition, collections.abc.Mapping and collections.abc.MutableMapping themselves are unsuitable, because they're nominal types, although this could be fixed by defining a Protocol which looks like a Mapping.
So, what should the types of builtins.locals and types.FrameType.f_locals be?
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the referenced declarations in stdlib/builtins.pyi and stdlib/types.pyi, then read PEP 667's description of FrameType.f_locals and compare it with the runtime examples in the issue. Done means determining and documenting compatible types for both locals() and f_locals, including whether a structural mapping protocol is needed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100