conditioned `getattr` seems not understands defaults
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
it seems to me that the getattr with the default option is not parsed correctly so mypy complains that none is not allowed, but in fact it shall be (and it is) caught bu the exception
(A clear and concise description of what the bug is.)
To Reproduce
from typing import Optional
class Mine:
better: Optional[bool]
def play() -> bool:
m = Mine()
if getattr(m, "better", None) is None:
raise RuntimeError()
return m.better
Expected Behavior
code shall be type-compatible
Actual Behavior
error: Incompatible return value type (got "bool | None", expected "bool") [return-value]
Your Environment
- Mypy version used:
mypy==1.11.1 - Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.10
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne damit, das bereitgestellte Python-Beispiel mit mypy 1.11.1 auszuführen und zu untersuchen, wie der bedingte getattr-Ausdruck analysiert wird. Als erledigt gilt die Aufgabe, wenn das Beispiel ohne den gemeldeten return-value-Fehler typgeprüft wird und ein Regressionstest das Verhalten abdeckt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers, devtools
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Klar beschrieben
- Anfängerfreundlichkeit
- 45/100