Oct-18-2023, 07:34 AM
I'm not sure, whether it's a good place to ask such question. If it's not, please tell me, where it suits better.
I use Python 3.11/3.12, and mypy 1.6.0.
Here's the code:
It's perfectly fine, and can be run without any problems. mypy finds it incorrect, though (it's about the
):
No type alias used, no problem for mypy. And anyone is happy. Well, anyone but me: I'd like to use type aliases, since they make my code more readable.
Am I missing something, or mypy has some problems with type parameters in type aliases?
I use Python 3.11/3.12, and mypy 1.6.0.
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
from typing import TypeVar, Callable T = TypeVar( 'T' ) # type alias, for Python 3.11 IntToSomething = Callable [ [ int ], T ] def callPlusOne(f : IntToSomething, val : int ) - > T: return f(val + 1 ) def prettyInt(val : int ) - > str : return f 'Your pretty number is {val}' print ( callPlusOne(prettyInt, 10 ) ) |
callPlusOne
function):Output:error: A function returning TypeVar should receive at least one argument containing the same TypeVar [type-var]
The solution (?) is to get rid of type alias (who needs them, anyway? 
1 2 3 4 5 6 7 8 9 10 11 |
from typing import TypeVar, Callable T = TypeVar( 'T' ) def callPlusOne(f : Callable [ [ int ], T ], val : int ) - > T: return f(val + 1 ) def prettyInt(val : int ) - > str : return f 'Your pretty number is {val}' print ( callPlusOne(prettyInt, 10 ) ) |
Am I missing something, or mypy has some problems with type parameters in type aliases?