Skip to content
Closed
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
12 changes: 12 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3539,6 +3539,18 @@ def check_assignment(
var.type = None
del partial_types[var]
lvalue_type = var.type
elif var.type is not None:
# 右值中的 walrus 可能已解析该变量的 partial type,
# 因此仍需检查外层赋值。
lvalue_type = var.type
self.check_subtype(
rvalue_type,
lvalue_type,
rvalue,
message_registry.INCOMPATIBLE_TYPES_IN_ASSIGNMENT,
"expression has type",
"variable has type",
)
else:
# Try to infer a partial type.
if not self.infer_partial_type(var, lvalue, rvalue_type):
Expand Down
7 changes: 7 additions & 0 deletions test-data/unit/check-python38.test
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,13 @@ i: Optional[int] = 0 if condition() else None
x: Optional[tuple[int, int]] = (i, (i := 1)) # E: Incompatible types in assignment (expression has type "tuple[int | None, int]", variable has type "tuple[int, int] | None")
[builtins fixtures/tuple.pyi]

[case testPartialTypeWalrusAssignment]
def fn() -> int:
x = None
x = (x, (x := 1)) # E: Incompatible types in assignment (expression has type "tuple[None, int]", variable has type "int | None")
return x
[builtins fixtures/tuple.pyi]

[case testReturnTupleOptionalWalrus]
from typing import Optional

Expand Down
Loading