Try block handling assumes all statements could have succeeded
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 35/100
Rechercherichtung
Führe zunächst das minimale Beispiel in type_test.py mit mypy aus und reproduziere den Fehler im except-Block. Untersuche die Typverengung bei try/except für die abschließende Zuweisung und überprüfe anschließend, dass das Beispiel keine Fehler meldet und dabei das korrekte Verhalten für mehrere Anweisungen beibehält.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Bug Report
When a try block is used to attempt to convert/consolidate the type of a variable, the except block assumes the conversion may or may not have happened. For try blocks with multiple statements, this is the correct behavior for every line but the final one as mypy can't know which of those lines succeeded. However, I would expect mypy to understand that the last/only line can't have executed.
To Reproduce
Minimal example similar to where I encountered the issue:
# file: type_test.py
import sys
from importlib import import_module
from types import ModuleType
def ensure_module(module: str | ModuleType) -> ModuleType:
if isinstance(module, str):
try:
module = import_module(module)
except ImportError:
sys.path.append('/path/to/extra/modules')
# mypy thinks module could be either a string or ModuleType
module = import_module(module)
return module
Expected Behavior
mypy shouldn't have any errors:
$ python -m mypy type_test.py
Success: no issues found in 1 source file
Actual Behavior
mypy finds an error in the except block:
$ python -m mypy type_test.py
type_test.py:14: error: Argument 1 to "import_module" has incompatible type "Union[str, Module]"; expected "str"
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used:
mypy 0.961 (compiled: yes) - Mypy command-line flags: N/A
- Mypy configuration options from
mypy.ini(and other config files): N/A - Python version used: 3.10.5
- Operating system and version: Arch Linux (5.18.11-zen1-1-zen)
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- Ø Merge
- 1 T. 18 Std.
- Gemergte PRs (30 T.)
- 54
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/mypy
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
bug topic-configuration topic-error-reporting
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
bancolombia/sentinel#23 ·
-
test md OffenCI
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100