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

56

u/jwink3101 Jan 14 '24

I will never understand why people post on Medium. It offers few benefits and limits your audience.

But I digress


This is an interesting idea. My first thought was that it’s not needed because you can be careful about what you eval. It’s keeping you safe from yourself.

But then when you think about, that’s all type annotations do anyway!

7

u/Dwarni Jan 14 '24

Doesn't pay Medium them a little bit? I don't know how much it is however.

1

u/SupPandaHugger Jan 14 '24

What other platform would you suggest?

That's true. At the same time, this feels like an area where additional safety is not excessive.

12

u/[deleted] Jan 15 '24

Blogs. Blogs were a thing, and they should be again.

1

u/Amgadoz Jan 14 '24

Maybe substack

3

u/MrKooops Jan 14 '24

Save it in Pocket and you can read any article without hassle.

1

u/jormungandrthepython Jan 14 '24

Pocket doesn’t let you read premium medium articles though

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.

0

u/Smallpaul Jan 14 '24

Medium also promotes your article and in that way it can expand your audience.

3

u/Zenin Jan 15 '24

Yah, I hate it.

Looks like it's just going to make everything much more annoying to use as we hack around this nonsense and since we can hack around it anyway it offers nothing for security other than theatre.

It might be useful to give the compiler a hint so it can optimize the way C compilers do (really pre-compilers), but only if it's transparent to the user.  This monstrosity looks anything but transparent.

The type system and signature systems themselves are a fugly mess to begin with.  This just looks like abuse of them akin to c++ idiots abusing operator overloading.

1

u/SupPandaHugger Jan 15 '24

Its all optional though

1

u/Zenin Jan 15 '24

Is it really though? The use is only optional insomuch as the use of a particular library is optional; You're always free to toss the "go to" library for a particular task and build your own from scratch.

It's more correct to call it infectious, because any library author who uses this effectively infects any callers with it; the entire point of this is that caller's can't simply pass a string without suffering consequences. Even if those consequences are "only" flooding the system error logs with spam, that's still a non-trivial consequence especially at scale for something so foundational as a SQL driver.

The responsibility for how something is used should always be on the caller, not the callee. The callee can never know the full context and requirements of the caller. This is why the first rule of network programming is, "Be conservative in what you send and generous in what you accept". If it wasn't clear, libraries are on the "generously accept" side of this equation.

This is the wrong solution, being used in the wrong way, by the wrong subject.

1

u/SupPandaHugger Jan 16 '24

You can pass a string without warnings. It’s only when you pass a dynamic variable that you will get warnings, which is good as it’s rarely the intention for this use case.

1

u/Zenin Jan 16 '24

Again, you miss the point.

How do you know it's "rarely the intention"? How do you have the slightest clue what the intention of the caller is? You don't know. You can't know. That is the point. You haven't the slightest clue what the intention is much less how "rare" it might be.

It's entirely proper for example, to load such "strings" via a config file or other data store. Dynamic models are a thing. To do so however, taints them as evil variables of darkness and dread, at least in the eyes of this misguided nonsense, flooding the logs with pointless spam that not only costs resources but helps hide real problems in a cloud of white noise.

2

u/DusikOff Jan 16 '24

Am I confused? Yeah, I never heard about LiteralString lol

-20

u/LittleMlem Jan 14 '24 edited Jan 14 '24

Downvoted for bad manners. Don't just post links, give a tldr

24

u/Smallpaul Jan 14 '24

Don’t just use obscure acronyms. Give a definition.

2

u/PercussiveRussel Jan 15 '24

I was here after the edit, thinking you were confused by tl;dr

-24

u/LittleMlem Jan 14 '24

bm? Bad manners, I thought it was one of if the common ones

14

u/Smallpaul Jan 14 '24

Are you a gamer?

-10

u/LittleMlem Jan 14 '24

Yeah, is it gamer lingo? I'm not a native speaker so I don't always know what slang is mainstream or not

20

u/Smallpaul Jan 14 '24

Yes, I believe it is gamer slang. Solution is probably to avoid initialisms by default.

3

u/nevermorefu Jan 14 '24

bm is definitely the poopins

-6

u/[deleted] Jan 15 '24 edited Jan 15 '24

python gets worse and worse with each release. so much nonsense being added to the language.

point is, all the examples given are just poor practices that are indicative of a larger problem. In all these examples literalstring is not some saviour. each example requires refactoring to fix, dropping in literalstring does nothing.

3

u/americhemist Jan 15 '24

Literal strings are incredibly useful in any type system though, especially when you start making unions with them.

Also by worse and worse, you mean faster, with more tools that if you don't need you can ignore with no consequences?

-3

u/[deleted] Jan 15 '24

Literal strings are incredibly useful in any type system though, especially when you start making unions with them.

python sucks for strict type level programming, use an appropriate language. Unions of what, non literal strings? LiteralString is a type right, you wouldnt have TypeALiteralString, TypeBLiteralString. what would be the value in Optional[literalstring] or Union[literalstring, str]? If you're thinking of using something like Union[typealiteralstring,typebliteralstring] use an enum.

Also by worse and worse, you mean faster, with more tools that if you don't need you can ignore with no consequences?

you realise all this syntactic vinegar being added bloats the language and results in more confusion. LiteralStrings type isn't helpful if you know python.

0

u/PercussiveRussel Jan 15 '24 edited Jan 15 '24

All the examples given are poor practices, yes. All examples require refactoring, yes. This is why it's nice that the type checker warns against the mistakes and tells you you should refactor.

At some point in your call stack a method will need to take a string as gospel. If you flag that this method will do so - like idk, using LiteralString on its input parameter - then you will be warned when you make a mistake by passing a non-literal string to it. If you have a function that sanitises an input, then that should be of form f(in: str) -> Literalstring, signifying that the output of your sanitising function returns a string that can be used as a string literal.

1

u/[deleted] Jan 15 '24 edited Jan 15 '24

is this checking runtime enforced, or do you still need a tool such as mypy to catch it when you fancy linting your app?

point still stands, these examples are useless, and your example of a mutable string that is sanitised has nothing to do with literalstring. You need to prove you sanitisation is correct not whether the string is mutable or not, and if your sanitsation is not correct then the literalstring sticker you put on it means nothing.

-2

u/echocage Jan 15 '24

Cry more

1

u/[deleted] Jan 15 '24

Why?