-
-
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
Make narrow_declared_type
correctly handle Literal types
#6961
Make narrow_declared_type
correctly handle Literal types
#6961
Conversation
This diff fixes the `narrowed_declared_type` function to correctly handle cases where the declared type is actually a `Literal[...]`. This fixes python#6952. In short, it seems like using the new semantic analyzer somehow made mypy call [the `narrow_type_from_binder(...)` function][0] where it previously did not. Specifically, mypy passed in the `var` expression and a `known_type` of `Literal[42]`, we got a restriction of `builtins.int` (with a `last_known_value` of 42), and attempted to narrow the `Literal[42]` on line 3512. But due to the missing case in `narrow_declared_type`, we ended up returning just the int. [0]: https://github.com/python/mypy/blob/master/mypy/checkexpr.py#L3504
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The difference in behavior is likely related to #6458, and the fact that Final
gets "stripped" in the process. Although this fix works for the issue, I would like to actually understand why it happens before deciding whether we should use this as a fix.
Note there is a little merge conflict.
I'm not too familiar with the behavior of the new semantic analyzer, but fwiw it didn't seem as if this were the case. More precisely, it seemed we did correctly remember that the type of the variable was
I do think this fix would be useful independently from the linked issue. IIUC doing The fact that change fixes #6458 is almost a side-effect IMO. |
I mean this #6988. I still think this PR has value, but it might also mask an actual flaw in the logic in new semantic analyzer. |
This diff fixes the `narrowed_declared_type` function to correctly handle cases where the declared type is actually a `Literal[...]`. Specifically, doing something like the below will return `int` instead of `Literal[42]`: narrowed_declared_type(Literal[42], int) This is inconsistent with the behavior of doing something like the below, which will return `ChildType`: narrowed_declared_type(ChildType, ParentType) This diff modifies `narrowed_declared_type` to instead return `Literal[42]` in the first example.
This diff fixes the
narrowed_declared_type
function to correctly handle cases where the declared type is actually aLiteral[...]
.This fixes #6952.
In short, it seems like using the new semantic analyzer somehow made mypy call the
narrow_type_from_binder(...)
function where it previously did not.Specifically, mypy passed in the
var
expression and aknown_type
ofLiteral[42]
, we got a restriction ofbuiltins.int
(with alast_known_value
of 42), and attempted to narrow theLiteral[42]
on line 3512.But due to the missing case in
narrow_declared_type
, we ended up returning just the int.