Type narrowing in while loop
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
Feature
example.py
from __future__ import annotations
import typing as t
class Node:
parent: Node | None = None
N = t.TypeVar("N", bound=Node)
def find_ancestor(node: Node, node_type: t.Type[N]) -> N | None:
ancestor = node.parent
while ancestor and not isinstance(ancestor, node_type):
ancestor = ancestor.parent
return ancestor
mypy 1.0.1 errors with:
example.py:13: error: Incompatible return value type (got "Optional[Node]", expected "Optional[N]") [return-value]
Would be great to narrow the type to N here and avoid the spurious error.
Pitch
Type narrowing in a while loop would improve inference. pyright already does this.
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 mypy sull’example.py fornito e riproduci l’errore relativo al valore restituito. Poi esamina il flusso di controllo del restringimento dei tipi per i cicli while e confronta il comportamento con pyright. Il lavoro è completato quando il ciclo restringe il valore a N dopo isinstance e l’esempio non segnala più l’errore.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Funzionalità
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 45/100