I'd like to define a generic type. Something like:
from typing import TypeVar, Sequence, Union, Generic
T = TypeVar('T')
RecurSeqOf = Sequence[Union[Generic[T], Sequence[T]]]
# mypy error: Variable "typing.Generic" is not valid as a type
Is there a way to do it?
The whole background
Actually, I need a recursive generic type like
T = TypeVar('T')
RecurSeqOf = Sequence[Union[T, 'RecurSeqOf']]]
But the definition of recursive types is not yet supported by mypy.
That's why I work around this issue by making nested type definitions up to a limited depth (say, usually 5-6 levels, but in the example below two levels only for the sake of conciseness). Hence, all the more the need to shorten the pattern because I need to use it for different parameter types:
from typing import Sequence, Union, TypeVar, Generic
class A:
pass
class B:
pass
# RecurSeqOfA = Sequence[Union[A, 'RecurSeqOfA']] # mypy error: Cannot resolve name "RecurSeqOfA" (possible cyclic definition)
RecurSeqOfA = Sequence[Union[A, Sequence[Union[A, Sequence[A]]]]]
# RecurSeqOfA = Sequence[Union[A, 'RecurSeqOfA']] # mypy error: Cannot resolve name "RecurSeqOfA" (possible cyclic definition)
RecurSeqOfB = Sequence[Union[B, Sequence[Union[B, Sequence[B]]]]]
T = TypeVar('T')
# RecurSeqOf = Sequence[Union[Generic[T], 'RecurSeqOf']] # error: Cannot resolve name "RecurSeqOf" (possible cyclic definition)
# additionally: error: Variable "typing.Generic" is not valid as a type
RecurSeqOf = Sequence[Union[Generic[T], Sequence[Generic[T]]]] # error: Variable "typing.Generic" is not valid as a type
As suggested by the comment of MisterMiyagi:
from typing import TypeVar, MutableSequence
T = TypeVar('T', bound='RecurSeqOf')
RecurSeqOf = MutableSequence[T]
a: RecurSeqOf[str] = []
a.append("abc")
a.append([]) # mypy error: error: Argument 1 to "append" of "MutableSequence" has incompatible type "List[<nothing>]"; expected "str"
b: RecurSeqOf[str] = []
a.append(b) # mypy error: Argument 1 to "append" of "MutableSequence" has incompatible type "MutableSequence[str]"; expected "str"
a.append(["cde"]) # mypy error: Argument 1 to "append" of "MutableSequence" has incompatible type "List[str]"; expected "str"
The definition itself is accepted by mypy. But it does not have the desired effect.