I just had this idea and wanted to hear your feedback.
The 3 current possibilities (that I know of) for “solving” the issue of not being able to represent a sequence of strings, that is not just a string itself, are the following:
- Do nothing.
- Add SequenceNotStr, which is a Special Form that specifically does not count
stras a valid sequence in its place. - Wait for negated types.
My opinion on the 3 solutions (skip this if you want)
In short:
- I and many others have ran into this issue multiple times and I think it needs a solution, so doing nothing is not a valid solution in my opinion.
- In my opinion the best solution. I think this case is easily common enough to be worth adding a Special Form and if another solution may ever be introduced, it could always still be (soft) deprecated in the future.
- Negated types seem like they will either never come or come in the far future. Waiting for those to fix a current issue is a bad decision and harmful in my opinion.
Somewhat of a workaround workaround is SequenceNotStr from the useful_types 3rd party library, but this also has some problems.
My new idea
So I have another idea to propose. This is just an idea I had and I want to hear your feedback!
We could introduce a new Special Form: Char
Char would simply represent a single character and just exists for type hinting, not as an actual class that replaces any functionality of strings.
Benefits
- It would solve the aforementioned problems with
SequenceNotStr:
strwould be valid for the expected typeSequence[Char], but forSequence[Sequence[Char]]strwould not be valid. Obviously for places where just a string is expectedstrwould be simpler and preferred over usingSequence[Char]. - It would bring type safety in other places:
Functions that take or return a single character could be type hinted more specifically. For example the builtin functionchrwould have the return typeCharand the builtin functionordwould take in aChar | bytes | bytearraycodepoint (instead ofstr | bytes | bytearray). - Bonus: Combined with tuples, specific length strings could be verified:
Technically a function could take any specifically wanted string length if converted to tuples like this:
Length 2:tuple[Char, Char]
Length 1 or more:tuple[Char, *tuple[Char, ...]]
Length 2 or 6:tuple[Char, Char] | tuple[Char, Char, Char, Char, Char, Char]
(Honestly I hope in the future not just tuples but Sequences can be typed heterogenously, then the 3rd point would be possible more natively with Sequence, but that’s another topic)
I also think there are more places this could be used for, but these are 3 things I could come up with. Let me know if you find some other places! ![]()
So what do you think? I’m curious for your feedback!
Edit: Fix phrasing and remove emoji.