- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3k
Open
Labels
affects-typeshedAnything that blocks a typeshed changeAnything that blocks a typeshed changebugmypy got something wrongmypy got something wrongtopic-type-variables
Description
This came up in python/typeshed#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.
not-my-profile, SpaghettDev and AlexWaygood
Metadata
Metadata
Assignees
Labels
affects-typeshedAnything that blocks a typeshed changeAnything that blocks a typeshed changebugmypy got something wrongmypy got something wrongtopic-type-variables