Issue
Let's say I have a type defined with TypeVar
, like so:
T = TypeVar('T')
MyType = Union[list[T], tuple[T]]
def func(a: MyType[int]):
pass
I also want to have an optional version of it:
MyTypeOpt = Optional[MyType]
def opt_func(a: MyTypeOpt[int] = None):
pass
But this is not to mypy's liking and I'm getting an error: Bad number of arguments for type alias, expected: 0, given: 1 [type-arg]
.
Is there a way to make it work without writing Optional[MyType[...]]
every time?
Solution
I think you are missing the type parameter for your generic type MyType
. mypy is also complaining about that.
from typing import Optional, Sequence, Tuple, Union
from typing_extensions import TypeVar
T = TypeVar('T')
MyType = Union[Sequence[T], Tuple[T]]
MyTypeOpt = Optional[MyType[T]] # <---------
def func(a: MyType[int]) -> None:
pass
def opt_func(a: MyTypeOpt[int] = None) -> None:
pass
def opt_func2(a: Optional[MyType[int]] = None) -> None:
pass
Answered By - anit3res
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.