python / python/mypy

Type inference with "or" between two lists doesn't work properly

Offen
#10,588 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

When two lists whose elements have different types appear in an or expression, type inferencing doesn't work properly if the second list is a literal. If the second list is assigned to a variable first, the type inference works.

For instance, if my_str_list is a List[str] and my_int_list is a List[int], the expression my_str_list or my_int_list has inferred type Union[List[str], List[int]], while my_str_list or [1, 2, 3] has inferred type List[str] (accompanied by an error saying the elements of the literal list are the wrong type).

To Reproduce

The following file:

list_of_ints: list[int] = [1,2,3]

reveal_type(["a", "b"] or [1,2,3])  # Produces error. Revealed type is 'builtins.list[builtins.str*]'
reveal_type(["a", "b"] or 123)  # Works. Revealed type is 'Union[builtins.list[builtins.str*], Literal[123]?]'
reveal_type(123 or [1,2,3])  # Works. Revealed type is 'Union[Literal[123]?, builtins.list[builtins.int*]]'
reveal_type(["a", "b"] or list_of_ints)  # Works. Revealed type is 'Union[builtins.list[builtins.str*], builtins.list[builtins.int]]'

gives output

file.py:3: note: Revealed type is 'builtins.list[builtins.str*]'
file.py:3: error: List item 0 has incompatible type "int"; expected "str"
file.py:3: error: List item 1 has incompatible type "int"; expected "str"
file.py:3: error: List item 2 has incompatible type "int"; expected "str"
file.py:4: note: Revealed type is 'Union[builtins.list[builtins.str*], Literal[123]?]'
file.py:5: note: Revealed type is 'Union[Literal[123]?, builtins.list[builtins.int*]]'
file.py:6: note: Revealed type is 'Union[builtins.list[builtins.str*], builtins.list[builtins.int]]'
Found 3 errors in 1 file (checked 1 source file)

Expected Behavior

Expected no error and reveal_type(["a", "b"] or [1,2,3]) to be Union[List[str], List[int]] or even List[int] if mypy is able to determine that ["a", "b"] is a truthy value.

Actual Behavior

reveal_type(["a", "b"] or [1,2,3]) is List[str] and there is error claiming the types of the literal list are wrong.

Your Environment

  • Mypy version used: 0.812
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.9.5
  • Operating system and version: Red Hat Enterprise Linux Server 7.9 (Maipo)

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

Verwende die bereitgestellte Reproduktionsdatei mit mypy und ohne Kommandozeilen-Flags und überprüfe die reveal_type-Ergebnisse für die or-Ausdrücke. Verfolge die Typinferenzbehandlung für or-Ausdrücke, die Listenliterale enthalten. Als abgeschlossen gilt die Aufgabe, wenn der Ausdruck mit zwei Listen keine Elementtypfehler erzeugt und eine Union der Listentypen oder das im Issue beschriebene, präzisere, auf Wahrheitswerten basierende Ergebnis offenlegt.

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
35/100

Neue Issues direkt in Ihr Postfach

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