0

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]]?

2
  • 1
    why do you need this kind of annotation? Commented May 22, 2020 at 14:05
  • 1
    @AzatIbrakov: There are some built-in functions that behave like this, so it's not that weird. See min/max (which is either one iterable argument or more than one scalar argument), or str.endswith's ability to take a single str or a tuple of str. Commented May 22, 2020 at 14:52

1 Answer 1

1

You can define such a type like this:

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

foo: MaybeSequence[str]
Sign up to request clarification or add additional context in comments.

1 Comment

Alias hint may be an option. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.