python / python/typeshed

The types of `builtins.locals` and `types.FrameType.f_locals` aren't correct

Open
#14,584 0 comments 0 reactions 0 assignees View on GitHub

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:

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.