if MYPY guarding an always-return block v. unreachable
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
Bug Report
Given a block dependent on if MYPY, if it always returns, mypy will conclude that everything following is unreachable. This shouldn't be obviously different to using an else block.
To Reproduce
from typing import Any, cast
MYPY = False
def foo(p):
# type: (Any) -> int
doc = p
if MYPY:
return cast(int, doc)
else:
# needs to be in else for MYPY to believe this is reachable
return doc
def bar(p):
# type: (Any) -> int
doc = p
if MYPY:
return cast(int, doc)
return doc
Expected Behavior
Either both output [unreachable] or both type check successfully.
Actual Behavior
foo.py:19: error: Statement is unreachable [unreachable]
i.e.: only bar fails to typecheck
Your Environment
- Mypy version used: 0.910
- Mypy command-line flags:
--warn-unreachable - Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.10.0
- Operating system and version: Arch Linux
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
Keine Quelldatei oder kein Test ist angegeben. Reproduziere zuerst das Beispiel mit mypy 0.910 und --warn-unreachable und verfolge dann die Behandlung der Erreichbarkeit für einen if MYPY-Block im Vergleich zu einem else-Block. Erledigt ist die Aufgabe, wenn beide äquivalenten Beispiele entweder erfolgreich typgeprüft werden oder konsistent als nicht erreichbar gemeldet werden.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100