This came up in python/typeshed#6762
fromtypingimportAny, TypeVar, UnionT=TypeVar("T")
V=TypeVar("V", str, bytes)
defcheck_t(x: T|list[T]) ->T: ...
defcheck_v(x: V|list[V]) ->V: ...
x_str: strx_list_any: list[Any]
x_str_list_any: str|list[Any]
# mypy does the correct thing herereveal_type(check_t(x_str)) # strreveal_type(check_t(x_list_any)) # list[Any] | Anyreveal_type(check_t(x_str_list_any)) # str | list[Any] | Anyreveal_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.
This came up in python/typeshed#6762
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.