You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Union subtyping doesn't work with covariant type constructors Tuple and Callable #7187
Are those types actually equivalent? Union[Callable[..., int], Callable[..., str]] sounds like a function that always returns ints or always returns strings, whereas Callable[..., Union[int, str]] is a function that can return a different thing each time. This argument does not apply to the tuple example, though it would if it was a list.
Reacted by swergas, lukgarn, Garrett Reynolds and Gabriele Angeletti
For mypy if x has type Union[int, str] then [x, x, x] has type List[Union[int, str]], not Union[List[int], List[str]], and this is not easy to fix. So the difference you mention is not essential ATM.
Anyway, we need to fix subtyping for unions in Callable and Tuple, it is currently broken.
Reacted by swergas, Saulo Martiello Mastelini and Garrett Reynolds
Is there any movement on this? I'm trying to use mypy with Flask and the only way to make it happy is to make the return type Response which means it could be anything. Otherwise I get the Value of type variable "T_route" of function cannot be "Callable[[], Foo]"
Activity
mortoray commentedon Jul 17, 2019
Is there any workaround to this now? I'm trying to get my Flask return types correct.
JukkaL commentedon Jul 18, 2019
There are a few options. You can use an
Anyreturn type. You can use# type: ignore. Sometimes you can use an overloaded function:Perhaps the best option would to change the stubs for flask to use a union return type (the change was made in https://github.com/python/typeshed/pull/3003/files).
ilevkivskyi commentedon Aug 15, 2019
(In view of coming implementation of recursive types this will be important so raising priority to high.)
Satisfy mypy
jingw commentedon Sep 6, 2019
Are those types actually equivalent?
Union[Callable[..., int], Callable[..., str]]sounds like a function that always returns ints or always returns strings, whereasCallable[..., Union[int, str]]is a function that can return a different thing each time. This argument does not apply to the tuple example, though it would if it was a list.ilevkivskyi commentedon Sep 6, 2019
For mypy if
xhas typeUnion[int, str]then[x, x, x]has typeList[Union[int, str]], notUnion[List[int], List[str]], and this is not easy to fix. So the difference you mention is not essential ATM.Anyway, we need to fix subtyping for unions in
CallableandTuple, it is currently broken.tjprescott commentedon Jan 29, 2024
Is there any movement on this? I'm trying to use mypy with Flask and the only way to make it happy is to make the return type
Responsewhich means it could be anything. Otherwise I get theValue of type variable "T_route" of function cannot be "Callable[[], Foo]"