You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Due to the lack of character type in Python, a function that accept List[Text] will need to be extra careful when generalize the accepted argument type. Iterable[Text] and Sequence[Text] clearly doesn't work:
The closest thing you can do is to do something like Union[List[Text], Set[Text], Tuple[Text]] but it will not work with things like KeysView.
How about having a typing.Char to represent Text with length=1, and dedicate Text to a type with at least 2 characters?
Some examples to illustrate the usage:
deffoo(xs: Iterable[Text]):
return'|'.join(xs)
# list(Text) should return List[Char]foo("abcd") # errorfoo(list("abcd")) # correct, List[Char] is a special case of List[Text]defbar(xs: Iterable[Char]):
return'|'.join(xs)
bar("abcd") # correct, Text is a Iterable[Char]bar(list("abcd")) # correct, List[Char] is a Iterable[Char]bar(["ab", "cd"]) # error, List[Text] is not a Iterable[Char]a: Char='a'a[1] # error, Char cannot be indexed beyond index 0a[0] # should be an error from type checking's PoV, Char should never be indexeda[:] # should be an error from type checking's PoV, Char should never be copied as a sliceb: Text='b'a+b# should be an error from type checking's PoV, Char and Text should be considered incompatible typestyping.cast(Text, a) +b# correctc=a+a# correct, concatenate Char gets Text (but I'm not sure if this is reasonable though, what if we do (a + a) + a ?)
The text was updated successfully, but these errors were encountered:
It is actually an interesting idea to have typing.Char as a string of length one. But for backwards compatibility we need to accept Char where Text is expected. I would recommend discussing this issue also on mypy tracker https://github.com/python/mypy
Due to the lack of character type in Python, a function that accept
List[Text]
will need to be extra careful when generalize the accepted argument type.Iterable[Text]
andSequence[Text]
clearly doesn't work:The closest thing you can do is to do something like
Union[List[Text], Set[Text], Tuple[Text]]
but it will not work with things likeKeysView
.How about having a
typing.Char
to represent Text with length=1, and dedicateText
to a type with at least 2 characters?Some examples to illustrate the usage:
The text was updated successfully, but these errors were encountered: