Skip to content

Commit fddfc8d

Browse files
authored
Fix walrus interaction with empty collections (#16197)
This fixes a regression caused by #16122
1 parent 0291ec9 commit fddfc8d

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

mypy/checkexpr.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4093,7 +4093,10 @@ def visit_assignment_expr(self, e: AssignmentExpr) -> Type:
40934093
value = self.accept(e.value)
40944094
self.chk.check_assignment(e.target, e.value)
40954095
self.chk.check_final(e)
4096-
self.chk.store_type(e.target, value)
4096+
if not has_uninhabited_component(value):
4097+
# TODO: can we get rid of this extra store_type()?
4098+
# Usually, check_assignment() already stores the lvalue type correctly.
4099+
self.chk.store_type(e.target, value)
40974100
self.find_partial_type_ref_fast_path(e.target)
40984101
return value
40994102

test-data/unit/check-python38.test

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,3 +826,11 @@ main:5: error: Dict entry 0 has incompatible type "str": "str"; expected "str":
826826
main:5: error: Unpacked dict entry 1 has incompatible type "Dict[str, str]"; expected "SupportsKeysAndGetItem[str, int]"
827827
dct: Dict[str, int] = {"a": "b", **other}
828828
^~~~~
829+
830+
[case testWalrusAssignmentEmptyCollection]
831+
from typing import List
832+
833+
y: List[int]
834+
if (y := []):
835+
reveal_type(y) # N: Revealed type is "builtins.list[builtins.int]"
836+
[builtins fixtures/list.pyi]

0 commit comments

Comments
 (0)