The types of `builtins.locals` and `types.FrameType.f_locals` aren't correct
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 5.1k
- Fork
- 2.1k
- Merge medio
- 1g 19h
- PR unite (30g)
- 82
Descrizione
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?
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con le dichiarazioni referenziate in stdlib/builtins.pyi e stdlib/types.pyi, poi leggi la descrizione di FrameType.f_locals in PEP 667 e confrontala con gli esempi di runtime nell'issue. Il lavoro è completato quando sono stati determinati e documentati tipi compatibili sia per locals() sia per f_locals, compreso se sia necessario un protocollo di mapping strutturale.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- tooling
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100