
Chiming in to say this is a really neat idea and I would definitely have used this in the past, especially when doing programming where it would have been much more useful to know about errors at coding time and not at runtime. This isn't to say it should definitely be added to the language, that's a tough hurdle. But boy would I have used it. On Tue, Dec 20, 2022, 6:33 AM Mathew Elman <mathew.elman@ocado.com> wrote:
This is somewhat inspired by the "Tagged strings in python" thread, but from a different approach. Specifically, rather than messing with runtime python, using static type analysis to help the usability of specific kinds of string literals.
It would be useful to take advantage of the `LiteralString` or `Literal` type hints to add structural validation to literals.
For *example*, if these could be subclassed to add a validation method: class MyLiteral(LiteralString): def __validate__(self): ...
or extended to allow something like regex in there generic type parameter: Literal[r"\w+"] LiteralString[r"\w+"]
then specific kinds of literal string can be "enforced" by static type checkers. Then IDEs could at the very least highlight that a string literal is not valid.
I stress these are just examples, the goal is to have a way to tell a static type checker what the structure of a string literal has to be. _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-leave@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/37OZHV... Code of Conduct: http://python.org/psf/codeofconduct/