Oh neat! And what happens if you say Literal[3]? Please tell me it requires a static proof that that argument is 3 (-: [assuming the actual parameter is not `3`].
Oh neat! And what happens if you say Literal[3]? Please tell me it requires a static proof that that argument is 3 (-: [assuming the actual parameter is not `3`].
Oh! I’m lost :(
We’re working on it!! (Or at least, @sharkdp.bsky.social is…) playknot.ruff.rs/122d0dfe-b76...
Wow, that's cute, and fast!
For a second I thought this was a release announcement.
Yes! I try to use ruff on every new Python project I start these days!
uv + ruff is a winning combo
well, seems like it only accepts literally the syntactic literal expression '3'
For re-assignable names, instead of inferring the type from the union of types of *all* assignments to it, some inferencers only consider assignments that aren't preceded by another assignments and then broaden to a single named type. Maybe that's what's happening here.
yes but that would still infer type `Literal[3]` for `x` in `x = 3` right? (which mypy infers as `int`)
Not if the inferencer always broadens to a *named* type. Without an explicit __all__, x is exported from the module, so inferring tight type bounds entails a backwards-compatibility commitment. For VERSION = "1.0" you wouldn't want to infer Literal("1.0") for VERSION.
Line 10 is accepted by Pyright. Unfortunately, this specific case is not handled by the specification:
Thanks for all the experiments!
does it work with x: Literal[3] = 3? (I'm too lazy to spin up a Python env to test that myself :P)
yes! not sure how you would narrow a generic int though (if x == 3 doesn't seem to narrow anything)
oh i guess it makes sense that it doesn't infer it to Literal[3] without a type signature since variables are mutable
haha, yeah, I tend to forget about that too :P
I've already completely solved the actual problem at hand and moved on in the time it's taken to discuss this type issue.
That's only because you have completely misunderstood the problem, which is that, once a type system is present, the goal is to understand the type system, not to actually write and run code. Don't feel bad, it's a common rookie error. You should take a PL course sometime.
Bloody hell. I already know regex so what else could it possibly involve?
regexes can't help us make sense of irregular expressions like `def foo(x: 3):`
Sure it does. If a regexp can handle HTML it can handle anything.
Can confirm.
Why do I feel like a complete pumpkin everytime I talk to you, @shriram.bsky.social??
Because you're secretly Cinderella?