Andrey
Andrey

Reputation: 925

Python Optional equivalent for Sequence

Assuming T = TypeVar('T')
Optional[T] hint is interpreted to Union[T, None].
Is there any equivalent for Sequence hint, so that it will be interpreted to Union[T, Sequence[T]]?

Upvotes: 0

Views: 163

Answers (1)

deceze
deceze

Reputation: 522034

You can define such a type like this:

T = TypeVar('T')
MaybeSequence = Union[T, Sequence[T]]

foo: MaybeSequence[str]

Upvotes: 1

Related Questions