python / python/mypy

False-positive when using Union[Tuple[...]]

Offen
#9,791 9 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report / To Reproduce

I have a incorrect Unsupported operand types for + ("int" and "str") when using Union[Tuple[...]].

To Reproduce

Have a look at this example:

from typing import Union, Tuple, List

variable: List[Union[Tuple[int, int], Tuple[str, str]]] = []

element1 = variable[0]

print(element1[0] + element1[1])

Run mypy.

Expected Behavior

No warning.

Actual Behavior

I have:

Unsupported operand types for + ("int" and "str")

But it's wrong due to the fact that element1 is a Union (right) but it's the same variable on both side. So both type on the left and the right of the "+" should be the same.

Your Environment

  • Mypy version used: mypy 0.790
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files):
[mypy]
python_version = 3.8
ignore_missing_imports = True
follow_imports = silent
show_column_numbers = True
disallow_untyped_calls = True
disallow_untyped_defs = True
disallow_incomplete_defs = True
check_untyped_defs = True
no_implicit_optional = True
warn_redundant_casts = True
warn_unused_ignores = True
warn_unreachable = True
strict_equality = True
  • Python version used: Python 3.7.3
  • Operating system and version: Windows 10 20H2 (19042.630)

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 mit der angegebenen Python-3.8-Konfiguration für den gemeldeten Python-Reproducer aus und bestätige den False Positive. Verfolge, wie der Type Checker die wiederholte Indizierung einer Union von Tupeln behandelt, und überprüfe anschließend, dass das Beispiel keine Warnung erzeugt, während korrekte Fehler für tatsächlich inkompatible Operanden erhalten 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
42/100

Neue Issues direkt in Ihr Postfach

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