Try block handling assumes all statements could have succeeded

Offen
#13,123 2 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
35/100
Issue-Typ
Bug
Klarheit
Größtenteils klar
Aktivitätsstatus
Veraltet
Tech-Stack
python
Bereich
compilers, devtools

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

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

Beitragsleitfaden öffnen

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus python/mypy

Alle Issues in python/mypy

Ähnliche Issues

Weitere Issues zu Python

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.