python / python/mypy

mypy should warn about list invariance when method return types are incompatible with a supertype

Aperta
#13,735 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

diagnostics feature topic-error-reporting
Lingua principale
Python
Stelle
20.6k
Fork
3.3k
Metriche di merge delle PR
Metriche PR in attesa

Descrizione

If you try to assign or return a list of a more specific type, mypy prints a helpful note warning that "List" is invariant, and suggests you use the "Sequence" type instead.
If you have a child class override a method to modify its return type to a list of a more specific type, mypy tells you the return type is incompatible but doesn't let you know that switching to the "Sequence" type might resolve their error.

To Reproduce

https://gist.github.com/1c07da0019991a5f14b9ff3a84e393db

numbers: list[float] = [1, 2.2]
integers: list[int] = [1, 2]
numbers = integers

def get_numbers() -> list[float]:
    return integers

class Parent:
    def get_numbers(self) -> list[float]:
        return [1, 2, 3]

class Child(Parent):
    def get_numbers(self) -> list[int]:
        return [1, 2, 3]

Expected Behavior

The invariant note is included for all 3 errors

main.py:3: error: Incompatible types in assignment (expression has type "List[int]", variable has type "List[float]")
main.py:3: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:3: note: Consider using "Sequence" instead, which is covariant
main.py:6: error: Incompatible return value type (got "List[int]", expected "List[float]")
main.py:6: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:6: note: Consider using "Sequence" instead, which is covariant
main.py:13: error: Return type "List[int]" of "get_numbers" incompatible with return type "List[float]" in supertype "Parent"
main.py:13: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:13: note: Consider using "Sequence" instead, which is covariant
Found 3 errors in 1 file (checked 1 source file)

Actual Behavior

main.py:3: error: Incompatible types in assignment (expression has type "List[int]", variable has type "List[float]")
main.py:3: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:3: note: Consider using "Sequence" instead, which is covariant
main.py:6: error: Incompatible return value type (got "List[int]", expected "List[float]")
main.py:6: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:6: note: Consider using "Sequence" instead, which is covariant
main.py:13: error: Return type "List[int]" of "get_numbers" incompatible with return type "List[float]" in supertype "Parent"
Found 3 errors in 1 file (checked 1 source file)

Your Environment

  • Mypy version used: 0.971
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.10.6

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo la riproduzione con main.py e confronta le diagnosi per gli errori di assegnazione, valore restituito e override. Traccia dove vengono emesse le note esistenti sull’invarianza di List, quindi assicurati che l’errore di override riceva le stesse note e verifica che l’output corrisponda al comportamento previsto mostrato nell’issue.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
tooling
Tipo di issue
Funzionalità
Difficoltà
3/5
Tempo stimato
1-2 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
45/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.