Open yegle opened 6 years ago
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
Related: #256
You can use phantom-types
in this case: https://github.com/antonagestam/phantom-types/
from phantom.sized import PhantomSized
class Char(PhantomSized[str], str, len=1):
...
assert isinstance('a', Char) is True
assert isinstance('123', Char) is False
def works_with_char(c: Char): ...
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: