-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Narrowing types to Literal
using in
syntax
#12535
Comments
Related to (but maybe not quite a duplicate of) #9718. |
It's a superset of #9718. I'll be looking into implementing this then, starting with the literal narrowing case. |
This issue seems to address narrowing both for I couldn't find an issue specifically for narrowing literals in from typing import Literal
def takes_foo(s: Literal["foo"]) -> None:
...
def f(s: str) -> None:
if s == "foo":
takes_foo(s)
Pyright seems to understand this kind of narrowing well. |
I opted for a clunky workaround using MyLiteral = Literal["a", "b"]
def is_my_literal(s: str) -> TypeIs[MyLiteral]:
return s in ("a", "b") |
This is similar to #3229 though that one doesn't ask for narrowing to Literal, possibly because the issue predates literal types. |
Take this example code:
The current output of this is:
If mypy were to support type narrowing using chained
or
statements andin
statements, the output would look like:I think doing simple narrowing here would be relatively easy and very useful.
The text was updated successfully, but these errors were encountered: