python / python/mypy

Loop Variable over Literal List Not Recognised as Having Literal Type

Offen
#18,826 6 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 a for-loop is used to iterate over a list-literal (or other collection-literal) which contains only scalar literals, then MyPy should be smart enough to recognize that the implicit type of the loop variable is not merely the type of the values in the list but the specific values in that list.

To Reproduce

import typing as _tp

MyLiteral: _tp.TypeAlias = _tp.Literal["foo", "bar", "baz"]

def func(a1: MyLiteral):
    ...

def func2() -> None:
    a: MyLiteral
    a = "foo"
    func(a)
    a = "bar"
    func(a)

    b: MyLiteral
    for b in ["foo", "bar"]:
        func(b)

    for c in ["foo", "bar"]:
        func(c)

Expected Behavior

  • At a minimum, there should be no warnings emitted on the first loop: MyPy should be smart enough to identify that all the values the loop is assigning to b are compatible with its explicit type of MyLiteral, the same way it identifies that the values being assigned to a on lines 10 & 12 are legal.
  • Ideally, there should also be no warnings emitted on the second loop: it would be nice if MyPy correctly identified the implicit type of c as Literal["foo", "bar"], which is compatible with the first argument of func()

Actual Behavior

$ mypy [REDACTED]/tmp.py
[REDACTED]\tmp.py:16: error: Incompatible types in assignment (expression has type "str", variable has type "Literal['foo', 'bar', 'baz']")  [assignment]
[REDACTED]\tmp.py:20: error: Argument 1 to "func" has incompatible type "str"; expected "Literal['foo', 'bar', 'baz']"  [arg-type]
Found 2 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: mypy 1.5.1 (compiled: yes)
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: Python 3.11.2

Related Issues

  • #9230: Issue complaining about the same problem, in the specific case where the loop variable is being used to access members of TypedDict. A fix was applied which solved the problem, but only for the special-case of accessing members of TypedDict, not for other, similar cases such as using the loop variable as a function argument as in the above example

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

Es wird keine Quelldatei oder kein Test genannt. Beginne damit, die gemeldete Python-Reproduktion mit mypy auszuführen und bestehende Tests zu Literal-Typen und Schleifeninferenz zu untersuchen; abgeschlossen ist es, wenn die Fälle mit expliziter und impliziter Schleifenvariable keine Fehler erzeugen und dabei das erwartete Literal-Verhalten erhalten bleibt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
tooling
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.