I have a function like this
def f(arg: Sequence[str]):
if isinstance(arg, str):
raise TypeError("arg should be a sequance of str, not str")
for string in arg:
str value is passed to this function, an error would raise.
However, the IDE would not mark this as error. Because,
str is a kind of
f("dummy") # should get a type-hint warning
f(["aaa", "bbb"]) # should be ok
f(MySequence("aaa", "bbb")) # should be ok
So how shall I type-hint this argument to solve this problem?
It is not possible to create a type hint like “Type A except subtype B”. Your only option is to create a union of the types you do in fact support, and optionally alias it.
In your case, a
Sequence can be one of
memoryview. The only
Sequence[str] types that make sense are
tuple[str], so why not use
list[str] | tuple[str]?
Edit: Sorry, I missed that you wanted to create your own
Sequence type. In that case, create an alias for a union of
tuple, and your own type:
SupportedSequence = list[str] | tuple[str] | MySequence[str]
def f(arg: SupportedSequence):
tuple[str] means “a
tuple of length 1, where the sole element is of type
str”. You’ll usually want to use
tuple[str, ...] (meaning “a
tuple of arbitrary length, where all elements are of type
str”) rather than
TIL. Is the same true for
tuple is a very special type in Python. See the “Annotating tuples” section of the typing docs for more details