Skip to content

Commit

Permalink
Fix walrus interaction with empty collections (#16197)
Browse files Browse the repository at this point in the history
This fixes a regression caused by
#16122
  • Loading branch information
ilevkivskyi committed Sep 28, 2023
1 parent 0291ec9 commit fddfc8d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4093,7 +4093,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]

0 comments on commit fddfc8d

Please sign in to comment.