Subclass attribute type narrowing on assignment
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
Documentation
Consider this code snippet:
from typing import Iterable
class A:
attr1: Iterable[str] # narrows to different type, specific number of elements.
attr2: tuple[str, ...] # narrows to specific number of elements
class B(A):
attr1 = "a", "b"
attr2 = "a", "b", "c"
reveal_type(B.attr1)
reveal_type(B.attr2)
With this, mypy will tell us that Revealed type is "Tuple[builtins.str, builtins.str]" and Revealed type is "Tuple[builtins.str, builtins.str, builtins.str]" for attr1 and attr2 respectively. I can't find this behaviour documented, neither in PEP484, nor the Python typing docs, nor mypy docs. I don't even know where this should be documented best. But for what it's worth, I asked pylance, which says Type of "B.attr1" is "tuple[str, ...]" and Type of "B.attr2" is "tuple[str, ...]". Which tells me that the intended behaviour isn't clear for other people, too.
Note: I'm aware that this may be a bug report instead. But I rather err on the side of this not being clearly specified yet than being incorrectly implemented.
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 leggendo PEP 484, la documentazione di typing di Python e la documentazione di mypy riguardo alle assegnazioni di sottoclassi mostrate. Confronta i tipi rivelati da mypy con i report di Pylance, quindi documenta il comportamento previsto o chiarisci se la discrepanza debba essere considerata un bug.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- documentation
- Tipo di issue
- Documentazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 35/100