Too broad type inferred for literal list of heterogeneous tuples
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug Report
When defining a list literal consisting of heterogeneous tuples with different lengths, mypy infers list[tuple[object, ...]], even when some fields of all the tuples have same type (BAD_LIST bellow). This then propagates and may raise false warnings of types not being compatible (see bellow).
This is somewhat similar to #6968, but since here all the container types are inferred correctly, I think it is a separate issue.
To Reproduce
x.py:
NICE_LIST = [
(1, 2),
(4, 5),
]
BAD_LIST = [
(1, 2, 'I am bad'),
(4, 5),
]
PROCESS_NICE = [(x[1], range(x[0]), ['something' for _ in range(x[1])])
for x in NICE_LIST]
PROCESS_BAD = [(x[1], range(x[0]), ['something' for _ in range(x[1])])
for x in BAD_LIST]
reveal_locals()
Expected Behavior
Mypy infers that first two indices of all the tuples are integers, so the call range(x[0]) is correct even for PROCESS_BAD so mypy would not raise an error.
Alternatively, mypy could give up on BAD_LIST as "too hard to infer" and require explicit type hint for it. (Explicitly declaring BAD_LIST: list[tuple[int, int, object] | tuple[int,int]] = [...] fixes the issue, but feels tedious.)
For completeness, in order to be consistent, mypy should have crashed on both ranges, not just on the first one.
Actual Behavior
$ mypy --pretty --show-error-codes x.py
x.py:12: error: No overload variant of "range" matches argument type "object" [call-overload]
PROCESS_BAD = [(x[1], range(x[0]), ['something' for _ in range(x[1])])
^
x.py:12: note: Possible overload variants:
x.py:12: note: def range(self, SupportsIndex) -> range
x.py:12: note: def range(self, SupportsIndex, SupportsIndex, SupportsIndex = ...) -> range
x.py:15: note: Revealed local types are:
x.py:15: note: BAD_LIST: builtins.list[builtins.tuple[builtins.object, ...]]
x.py:15: note: NICE_LIST: builtins.list[Tuple[builtins.int, builtins.int]]
x.py:15: note: PROCESS_BAD: builtins.list[Tuple[builtins.object, Any, builtins.list[builtins.str]]]
x.py:15: note: PROCESS_NICE: builtins.list[Tuple[builtins.int, builtins.range, builtins.list[builtins.str]]]
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: mypy 0.950 (compiled: yes)
- Python version used: 3.10.4
- Operating system and version: Arch Linux
Verbose output of mypy: verb.txt
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia eseguendo mypy sulla riproduzione di x.py e confronta i tipi inferiti per NICE_LIST, BAD_LIST, PROCESS_NICE e PROCESS_BAD con il comportamento previsto. Traccia quindi l'inferenza dei valori letterali di liste e tuple eterogenee nel type checker, poi aggiungi una copertura di regressione che dimostri che i campi di tupla condivisi mantengono i propri tipi compatibili e che entrambe le chiamate a range sono accettate.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 42/100