-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Typing of return type of a generic function #8946
Comments
I have made a very similar observation on #8941, but since 1 - It impedes the use you are showing, of specializing the return type of a function, unless In my case, I ended up (for And by the way, 0.770 deduced the type as In the end, I still need to use |
As explained by @tarcisioe, the new behavior better aligns with runtime Python behavior. |
So just out of curiosity, forgetting the entire Optional thing, say that I have this function: def create(t: Type[T]) -> T:
return t()
a = create(int)
b = create(str) What should the types be to let mypy know that |
@ltworf |
It seems incorrect, or well it works for those but not if I start passing like from typing import *
T = TypeVar('T')
def create(t: Type[T]) -> T:
...
reveal_type(create(int))
reveal_type(create(str))
reveal_type(create(List[int]))
reveal_type(create(Dict[int, int]))
reveal_type(create(Tuple[int]))
reveal_type(create(Union[int,str]))
reveal_type(create(Optional[int])) Result:
the I could understand it not working for union types, but why does it also fail for Tuple, but work for List and Dict??? I was using this for my typedload module with a Should I open a different bug to track this? |
@ltworf I think this won't be treated as a bug. Since #8941 and this issue were closed for the same reason, this seems like the intended behaviour. But it is a missing feature. Basically what we are looking for is something mirroring what could be written in many other languages with generics as something along the lines of (pardon the C++) template <typename T>
T load(std::string data) {} // std::string for lack of a better type here Unfortunately, there is no real syntax for that. The closest you can get is by using a class just to get the generic: T = TypeVar('T')
class Converter(Generic[T]):
@staticmethod
def convert(data: Any) -> T:
... But there are two issues here: this is pretty verbose, and there is no (easy, at least) runtime access to whatever T was, as far as I am aware, so I don't see that as a very good solution. If there is a way to get the real T at runtime I would find it acceptable, though I'd rather look at something terser. |
Note: if you are reporting a wrong signature of a function or a class in
the standard library, then the typeshed tracker is better suited
for this report: https://github.com/python/typeshed/issues
Please provide more information to help us understand the issue:
Reporting Bug
or a mock-up repro if the source is private. We would appreciate
if you try to simplify your case to a minimal repro.
Actually since this answer https://stackoverflow.com/a/42226930 I would expect it work. It also worked in previsous versions.
mypy 0.780
python 3.7.7
(You can freely edit this text, please remove all the lines
you believe are unnecessary.)
The text was updated successfully, but these errors were encountered: