-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 enums causes .value
to be typed as Any
#14092
Labels
Comments
This seems wrong too (another bug?) though I forgot what |
@A5rocks the question mark at the end reflects this context-sensitive nature., basically means it's inferred to be literal, and not declared with |
Another example: from enum import Enum
class E(Enum):
A = 1
B = 2
e: E
print(e.value) # no error
if e is E.A:
print(e.value) # Expression has type "Any" [misc] |
from enum import Enum
from typing import Literal
class E(Enum):
A = 1
x: Literal[E.A]
reveal_type(x.value) # N: Revealed type is "Any" Turns out there's a bigger issue here :/ |
Thanks for making this pop back up in my notifications @KotlinIsland! Turns out it was simpler than I expected :P |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Bug Report
Apologies if this has already been reported, but I couldn't find an existing issue for it.
The issue I'm seeing is related to accessing the
.value
of anEnum
after "narrowing" it with theis
operator. It seems like the narrowing (toUnion[Literal[...]]
) works as expected, but then it fails to realize that the.value
of all the variants have the same type (and so falls back toAny
).This only affects runs with
--warn-return-any
enabled:To Reproduce
It's probably easiest to see in this example:
(playground URL: https://mypy-play.net/?mypy=latest&python=3.8&flags=warn-return-any&gist=b2a6df92fb37717e5c9d7542b6e88fec)
Expected Behavior
This should deduce that
self.value
has typeRGB
and succeed.Actual Behavior
If I add some
reveal_type(self)
andreveal_type(self.value)
s before/after the narrowing, I get this output:Your Environment
The text was updated successfully, but these errors were encountered: