python / python/mypy

mypy ignores type checking and produces error for union types when using `is` keyword to check the boolean result from `isinstance()`

Offen
#14,289 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature topic-type-narrowing
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

When using the is True or is False keywords in the if statement along with isinstance, mypy does not recognize the type checking and produces an error, and ignores the instance type checking.

To Reproduce

from typing import Union

class A:
    prop: str = "Test"
    prop2: int = 999

class B:
    prop: str = "Test 2"

AB = Union[A, B]

if __name__ == '__main__':
    test_var: AB = A()

    if isinstance(test_var, A) is True:
        print(test_var.prop2)```

Playground: https://mypy-play.net/?mypy=latest&python=3.9&gist=7c4401069c861413b72a395a4ce92757

Expected Behavior

I expected that both expressions below were interpreted the same way by mypy as both are syntactically valid and produce the same result:

Using the is keyword to check the result:

    if isinstance(test_var, A) is True:
      pass

No is keyword:

    if isinstance(test_var, A):
      pass

Actual Behavior

main.py:19: error: Item "B" of "Union[A, B]" has no attribute "prop2"  [union-attr]
Found 1 error in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.991
  • Mypy command-line flags: --follow-imports silent
  • Mypy configuration options from mypy.ini (and other config files):
    •        python_version = 3.9
             warn_return_any = True
             warn_unused_configs = True
             ignore_missing_imports = True
             disallow_untyped_defs = True
             disallow_incomplete_defs = True
             check_untyped_defs = True
             disallow_untyped_decorators = True
             no_implicit_optional = True
             strict_equality = True
             warn_redundant_casts = True
             warn_unused_ignores = True
             show_error_codes = True
      
  • Python version used: 3.9

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne damit, das gemeldete Beispiel im verknüpften mypy playground mit Python 3.9 und mypy 0.991 zu reproduzieren, und vergleiche isinstance(test_var, A) mit isinstance(test_var, A) is True. Verfolge, wie mypy Union-Typen für diese Bedingungen eingrenzt, und füge einen Regressionstest hinzu; erledigt ist die Aufgabe, wenn beide Formen test_var als A erkennen, ohne den Union-Attributfehler zu melden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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