-
-
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
Literal + Final don't work well on new semantic analyzer #6952
Comments
|
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
#6988) This is (kind of) an actual fix for #6952 The problem is that previously `unwrap_final()` unconditionally set `s.type` to `None` for bare `Final`, even if the type was inferred from a simple literal r.h.s. on a previous iteration. At the same time `lvalue.is_inferred_def` remained `False`, so that the type was not inferred on the second iteration. This way `s.type` and `lvalue.is_inferred_def` may get out of sync depending on the number of iterations, thus causing weird behavior in type checker. I generally can say the current way of storing the inferred status is error-prone, but we already have #6458 to track this.
@ilevkivskyi -- actually, should we keep this one open? Both of our PRs are only kind of fixes this. Or maybe it's sufficient to just keep this closed and track just #6458 instead. |
#6458 is sufficient. |
python#6988) This is (kind of) an actual fix for python#6952 The problem is that previously `unwrap_final()` unconditionally set `s.type` to `None` for bare `Final`, even if the type was inferred from a simple literal r.h.s. on a previous iteration. At the same time `lvalue.is_inferred_def` remained `False`, so that the type was not inferred on the second iteration. This way `s.type` and `lvalue.is_inferred_def` may get out of sync depending on the number of iterations, thus causing weird behavior in type checker. I generally can say the current way of storing the inferred status is error-prone, but we already have python#6458 to track this.
This test case fails
with
on new semantic analyzer
cc @Michael0x2a
The text was updated successfully, but these errors were encountered: