Cannot infer value of constrained type parameter when calling function with union
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
I have a function that takes in either a list of X or a list of Y, and returns a list of the same X or Y that was input. When calling the function with a variable that's either list of X or a list of Y, I expect it to work the same as calling the function with either.
To Reproduce
from typing import TypeVar
StrOrInt = TypeVar("StrOrInt", str, int)
def func(vals: list[StrOrInt]) -> list[StrOrInt]:
return vals
def action(strs: list[str], ints: list[int]) -> None:
reveal_type(func(strs))
reveal_type(func(ints))
strOrInt: list[str] | list[int] = strs if strs else ints
reveal_type(func(strOrInt))
Expected Behavior
test.py:9: note: Revealed type is "builtins.list[builtins.str]"
test.py:10: note: Revealed type is "builtins.list[builtins.int]"
test.py:13: note: Revealed type is "builtins.list[builtins.str] | builtins.list[builtins.int]"
Actual Behavior
test.py:9: note: Revealed type is "builtins.list[builtins.str]"
test.py:10: note: Revealed type is "builtins.list[builtins.int]"
test.py:13: error: Cannot infer value of type parameter "StrOrInt" of "func" [misc]
test.py:13: note: Revealed type is "builtins.list[Any]"
Your Environment
- Mypy version used:
- Mypy command-line flags:
- Mypy configuration options from
mypy.ini(and other config files): - Python version used:
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 il riproduttore Python fornito con mypy e confronta i tre tipi rivelati con l’output previsto. Analizza l’inferenza dei parametri di tipo per un argomento list[str] | list[int]; il lavoro è completato quando la chiamata con l’unione viene accettata e rivela list[str] | list[int] senza un errore di inferenza.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 35/100