-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Regression: false positive avoid redundant cast with literal union #19055
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
I'd say this is indeed a redundant cast as I suspect the logic goes as follows: during the first pass we have an unknown type and infer |
The reason this looked like a regression is that I've only seen this happen with literal types. This doesn't generate an error: # mypy: warn-redundant-casts
from typing import cast
Foo = int | str
class C:
def __init__(self) -> None:
self.x = cast(Foo, "a") # No error Is there an inconsistency? |
Indeed, that's a bit inconsistent: we only detect unnecessary casting to Literal types in this scenario (same if you annotate I'd argue that this aspect of new mypy behavior is correct: the cast is redundant. So the actual bug is that we fail to report |
Hm, no, my bad, this still looks weird. It's a positive change, but I don't understand why it actually happens. Why do we infer a union of literals somewhere when its subset would suffice? |
This example started generating an error after #18588, which looks like a regression:
I haven't looked into this in detail, but it seems possible that we infer a union type for
"a"
in a literal union context, which doesn't look right. If this is a correct hypothesis, #18588 wouldn't actually be the root cause, and it would just be exposing a pre-existing issue.cc @asottile as the author of the PR
The text was updated successfully, but these errors were encountered: