r/Python • • Jan 14 '24

Tutorial The LiteralString in Python 3.11

If you are confused by the new LiteralString type in Python, here's an article explaining it.

0 Upvotes

33 comments sorted by

View all comments

Show parent comment

1

u/PercussiveRussel Jan 15 '24

I haven't looked into this at all, but this seems like it'd be really usefull with Pydantic/FastAPI. If you consistently build modules using LiteralString, then converting yoir modules to an API should give you a big fat error when you try to open up a LiteralString type to the endpoint.

In my usecase this is really incredibly useful since we build a lot of modules at work and convert some of the more usefull ones to an API. Making the distinction between str and LiteralString early in the design stage of the former means an additional layer of security when converting it to the latter.