python / python/mypy

Errors on multi-variable assignments of types

Offen
#10,101 11 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

documentation topic-type-alias
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

MyPy seems to have issues when multi-variable assignments of types to their aliases are involved.

To Reproduce
Run mypy on the following two sources:

from typing import Any

typ_a = Any
typ_b = Any
def test1(x: typ_a, y: typ_b): pass
from typing import Any

typ_a, typ_b = Any, Any
def test2(x: typ_a, y: typ_b): pass

typ_a = typ_b = Any
def test3(x: typ_a, y: typ_b): pass

Expected Behavior
MyPy does not show any errors for any of the function's signatures, since they are all essentially the same.

Actual Behavior
MyPy shows the following errors for test2 and test3:

repro2.py:4: error: Variable "repro2.typ_a" is not valid as a type
repro2.py:4: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
repro2.py:4: error: Variable "repro2.typ_b" is not valid as a type
repro2.py:7: error: Variable "repro2.typ_a" is not valid as a type
repro2.py:7: note: See https://mypy.readthedocs.io/en/latest/common_issues.html#variables-vs-type-aliases
repro2.py:7: error: Variable "repro2.typ_b" is not valid as a type
Found 4 errors in 1 file (checked 1 source file)

Confusingly, if one merges the two source files, no errors are shown.

Your Environment

  • Mypy version used: 0.800
  • Mypy command-line flags:
  • Mypy configuration options from mypy.ini (and other config files):
  • Python version used: 3.9.1
  • Operating system and version: Ubuntu 20.04

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

Führe zunächst mypy für die drei Reproduktionen in repro2.py aus und vergleiche, wie Einzel-, Tupel- und verkettete Zuweisungen als Typaliase erfasst werden. Verfolge den Type-Checking-Pfad für diese Zuweisungen und Funktionsannotationen; fertig ist es, wenn alle drei Signaturen keine Fehler erzeugen, während die bestehenden Alias- und Variablenprüfungen unverändert bleiben.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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