mypy doesn't take value restrictions into account when solving type variables
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
This came up in https://github.com/python/typeshed/pull/6762
from typing import Any, TypeVar, Union
T = TypeVar("T")
V = TypeVar("V", str, bytes)
def check_t(x: T | list[T]) -> T: ...
def check_v(x: V | list[V]) -> V: ...
x_str: str
x_list_any: list[Any]
x_str_list_any: str | list[Any]
# mypy does the correct thing here
reveal_type(check_t(x_str)) # str
reveal_type(check_t(x_list_any)) # list[Any] | Any
reveal_type(check_t(x_str_list_any)) # str | list[Any] | Any
reveal_type(check_v(x_str)) # str
# but maybe not here
# E: Value of type variable "V" of "check_v" cannot be "Union[List[Any], Any]"
# N: Revealed type is "Union[builtins.list[Any], Any]"
reveal_type(check_v(x_list_any))
# E: Value of type variable "V" of "check_v" cannot be "Union[str, List[Any], Any]"
# N: Revealed type is "Union[builtins.str, builtins.list[Any], Any]"
reveal_type(check_v(x_str_list_any))
Looking at the output, my guess is that mypy isn't taking value restrictions into account when solving constraints and only using value restriction as a check later. That is, list[Any] cannot match the T part of T | list[T] because it would violate the value restriction.
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 riproducendo gli esempi forniti che coinvolgono check_t, check_v, Any e TypeVars vincolati, quindi segui la risoluzione dei vincoli delle variabili di tipo di mypy e il successivo controllo delle restrizioni sui valori. Il lavoro è completato quando le chiamate con list[Any] e str | list[Any] vengono accettate con i tipi rivelati appropriati, senza gli errori value-of-type-variable segnalati.
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
- Abbastanza chiara
- Idoneità per principianti
- 42/100