-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Enhanced Literal Detection: Allow string type narrowing to literals #16813
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
Comments
related to #13767 |
This is a lot of different feature requests in one. You should probably open multiple issues. The one about |
I will create different issues an link them here. After this I will close this issue. |
Opened three different Issues that should tackle this. |
Note this already works in pyright Correction: Pyright just doesn't check this correctly |
|
Feature
It would be great if mypy could detect and type narrow strings to Literals if this is statically detectable.
I.e. if accessing TypedDict or Enum values
Pitch
MyPy Play
Currently there is no way to type this kind of expression without a
# type: ignore
.It would be nice if mypy could detect that
if k in Part.__annotations__
will narrowk
to the Literal of all key ofPart
.Also
if k == "a"
isn't working at the moment, but at least for this I am able to write aTypeGuard
.Also if Literals are explicitly defined, we loose this once we are with a loop
Even if the literal is defined explictly, it gets lost in a for loop
If explicitly given as
keys: tuple[Literal["a", "b"], ...]
it works also in the for loop but I don't want to duplicate these values...The text was updated successfully, but these errors were encountered: