python / python/mypy

Mypy is still too eager about outer context

Offen
#5,874 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug false-positive priority-1-normal topic-type-variables topic-union-types
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Consider this example:

T = TypeVar('T')
S = TypeVar('S')

def add(x: List[T], y: List[S]) -> List[Union[T, S]]: ...

a: List[int]
b: List[str]

e: List[Union[int, str]] = add(a, b)

It fails on the last line with the following (I skip invariance notes):

error: Argument 1 to "add" has incompatible type "List[int]"; expected "List[Union[int, str]]"
error: Argument 2 to "add" has incompatible type "List[str]"; expected "List[Union[int, str]]"

If I remove the annotation for e, everything works fine.

When previously working on the external context issues, I found that I don't like how constrains for unions against unions are inferred (not that they are wrong, just counterintuitive). For example, Union[T, S] <: Union[str, int] infers T < : Union[str, int] and S <: Union[str, int], which is technically fine, but leads to errors like above.

This blocks https://github.com/python/mypy/pull/5873

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 annotierte add(a, b)-Beispiel mit mypy zu reproduzieren, und lies die Diskussion rund um den referenzierten Pull Request 5873. Bestimme die beabsichtigte Constraint-Inferenz für Unions gegenüber Unions und verifiziere anschließend, dass das Beispiel erfolgreich ist, ohne Regressionen einzuführen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Bug
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

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