Type hint for a "char" / "character" type
Solution 1:
Your first approach is simply wrong. chr
is a built-in function, it's not a type ! So you can't use it in type hints.
In my opinion it is enough to say it's a str
.Type hint is for third-parties, type-checkers... When Python doesn't differentiate between char and string, why do you want to do that? You can then implement a logic to raise error in your function or methods or whatever if that string has more than one item.
If you insist, you can have your custom type although I don't think it is super useful:
class Onechar(str):
def __new__(cls, s):
if len(s) > 1:
raise ValueError("Only one character")
return super(Onechar, cls).__new__(cls, s)
obj: Onechar
obj = "hiii" # mypy complains
obj = Onechar("h") # fine