python / python/cpython

types.MappingProxyType states that proxy[key] will throw a KeyError, but that is not always true

Aperta
#117,956 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

docs
Lingua principale
Python
Stelle
77.2k
Fork
35.9k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

Documentation

In the documentation, it states proxy[key] will:

Return the item of the underlying mapping with key key. Raises a KeyError if key is not in the underlying mapping.

https://docs.python.org/3/library/types.html#types.MappingProxyType

However, it is possible to get an AttributeError instead, and in fact, unit tests for the dataclasses module require this to be true (metadata in dataclasses.field is wrapped in a mapping proxy):

https://github.com/python/cpython/blob/5f4c7cf3f49629bcf890c305e0a90ffd708110a2/Lib/test/test_dataclasses/__init__.py#L2065-L2066

The unit tests above are from Python3.12.

Looks like the CPython source code only forwards the getitem call to the inner mapping:

https://github.com/python/cpython/blob/5f4c7cf3f49629bcf890c305e0a90ffd708110a2/Objects/descrobject.c#L1042-L1046

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia dalla documentazione di MappingProxyType nella pagina collegata della documentazione Python, quindi confrontane la formulazione con i test dataclasses indicati e con l’implementazione in Objects/descrobject.c. Il lavoro è concluso quando la documentazione descrive accuratamente il comportamento delle eccezioni dimostrato dai test e dall’implementazione.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
documentation
Tipo di issue
Documentazione
Difficoltà
1/5
Tempo stimato
Meno di un'ora
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
58/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.