`__future__` annotations breaks `TypedDict` `__required/optional_keys__`

Aperta
#97,727 7 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
python
Ambito
compilers

Direzione di ricerca

Inizia eseguendo la riproduzione t.py con e senza from __future__ import annotations, nelle versioni di Python elencate e con typing e typing_extensions. Traccia i punti di ingresso di TypedDict, Required e NotRequired per determinare in che modo le annotazioni posticipate influiscono sulla classificazione delle chiavi. Il lavoro è completato quando __required_keys__ e __optional_keys__ corrispondono alle annotazioni dichiarate per le varianti riportate.

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

Descrizione

stdlib topic-typing type-bug

Bug report

from __future__ import annotations appears to break TypedDict required/optional keys ending up in __required_keys__ and __optional_keys__. mypy works as expected though.

Using the example from https://peps.python.org/pep-0655/#usage-in-python-3-11 as the base

$ cat t.py
from __future__ import annotations

from typing_extensions import NotRequired, TypedDict

class Dog(TypedDict):
    name: str
    owner: NotRequired[str]

print("required", Dog.__required_keys__)
print("optional", Dog.__optional_keys__)
$ python3 t.py
required frozenset({'name', 'owner'})
optional frozenset()

With the __future__ import removed, works as expected:

$ cat t.py
from typing_extensions import NotRequired, TypedDict

class Dog(TypedDict):
    name: str
    owner: NotRequired[str]

print("required", Dog.__required_keys__)
print("optional", Dog.__optional_keys__)
$ python3 t.py
required frozenset({'name'})
optional frozenset({'owner'})

Note: breaks across different variations of total and Required/NotRequired and typing_extensions vs typing imports, above is just one example.

https://peps.python.org/pep-0655/#how-to-teach-this contains an example with the __future__ annotations import in place with no mention that it would not cause __required_keys__ and __optional_keys__ becoming populated as expected, so I'm assuming this is a bug.

Your environment

  • CPython versions tested on:

    • 3.9.7 + NotRequired and TypedDict imports from typing_extensions
    • 3.10.7 + above mentioned imports from typing_extensions
    • current 3.11.0rc2+ + above mentioned imports from typing_extensions
    • current 3.11.0rc2+ above mentioned imports from typing
  • Operating system and architecture: Linux x86_64

Lingua principale
Python
Stelle
77.2k
Fork
36k
Merge medio
1g 9h
PR unite (30g)
558

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.

Altre issue di python/cpython

Tutte le issue di python/cpython

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.