Skip to content
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

Fix walrus interaction with empty collections #16197

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4057,7 +4057,10 @@ def visit_assignment_expr(self, e: AssignmentExpr) -> Type:
value = self.accept(e.value)
self.chk.check_assignment(e.target, e.value)
self.chk.check_final(e)
self.chk.store_type(e.target, value)
if not has_uninhabited_component(value):
# TODO: can we get rid of this extra store_type()?
# Usually, check_assignment() already stores the lvalue type correctly.
self.chk.store_type(e.target, value)
self.find_partial_type_ref_fast_path(e.target)
return value

Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-python38.test
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,11 @@ main:5: error: Dict entry 0 has incompatible type "str": "str"; expected "str":
main:5: error: Unpacked dict entry 1 has incompatible type "Dict[str, str]"; expected "SupportsKeysAndGetItem[str, int]"
dct: Dict[str, int] = {"a": "b", **other}
^~~~~

[case testWalrusAssignmentEmptyCollection]
from typing import List

y: List[int]
if (y := []):
reveal_type(y) # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/list.pyi]