Extending LiteralString or Literal with structural validation

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.

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:

Ricky Teachey writes:
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.
IIUC, Mathew's idea doesn't need to be added to *the* language (the one defined by the Language Reference). It needs to be added to the languages used by type checkers. So if you want this and know a type checker author, send them pizza! :-) Steve

On Tue, Dec 20, 2022 at 11:17 PM Stephen J. Turnbull < stephenjturnbull@gmail.com> wrote:
Unfortunately subclassing LiteralString is a TypeError at this time. Otoh something like this doesn't necessarily have to be a LiteralString child class, but it does mean someone who is a little more knowledgeable than I would have to write such a CustomString parent class, or custom_str() factory. Anyway I guess probably this is a discussion for the typing mailing list. Maybe the OP should consider taking it there? --- Ricky. "I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler

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:

Ricky Teachey writes:
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.
IIUC, Mathew's idea doesn't need to be added to *the* language (the one defined by the Language Reference). It needs to be added to the languages used by type checkers. So if you want this and know a type checker author, send them pizza! :-) Steve

On Tue, Dec 20, 2022 at 11:17 PM Stephen J. Turnbull < stephenjturnbull@gmail.com> wrote:
Unfortunately subclassing LiteralString is a TypeError at this time. Otoh something like this doesn't necessarily have to be a LiteralString child class, but it does mean someone who is a little more knowledgeable than I would have to write such a CustomString parent class, or custom_str() factory. Anyway I guess probably this is a discussion for the typing mailing list. Maybe the OP should consider taking it there? --- Ricky. "I've never met a Kentucky man who wasn't either thinking about going home or actually going home." - Happy Chandler
participants (3)
-
Mathew Elman
-
Ricky Teachey
-
Stephen J. Turnbull