`.get(key)` should narrow final TypedDicts
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
in keyword narrowing was implemented for TypedDicts, but not .get(). I understand that it may be hard to implement for arbitrary default arguments of .get(key, default), but the .get(key) variable, without a default value, can be useful as well.
Bug Report
To Reproduce
import typing
@typing.final
class Node(typing.TypedDict):
name: str
children: list[typing.Union['Node', 'Leaf']]
@typing.final
class Leaf(typing.TypedDict):
name: str
x: Node | Leaf
### INCORRECT ERROR
if x.get('children'):
x['children'] # error: TypedDict "Leaf" has no key "children"
### CORRECT
if 'children' in x:
x['children']
Expected Behavior
No error
Actual Behavior
error: TypedDict "Leaf" has no key "children" [typeddict-item]
Your Environment
- Mypy version used: 1.9.0
- Mypy command-line flags: non
- Mypy configuration options from
mypy.ini(and other config files):
warn_redundant_casts = True
warn_unused_ignores = True
warn_no_return = False
warn_return_any = True
disallow_untyped_defs = True
disallow_any_generics = True
- Python version used:
Python 3.12.1
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 eseguendo la riproduzione in Python e individuando il comportamento esistente di narrowing di TypedDict per l’operatore in. Nel report non sono indicati file sorgenti né test; il lavoro è completo quando il controllo con .get('children') restringe x in modo che il successivo accesso a x['children'] non produca errori, mentre il caso dell’argomento predefinito rimane gestito separatamente.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100