Skip to content
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
4 changes: 4 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3722,6 +3722,9 @@ def try_infer_partial_generic_type_from_assignment(
if partial_types is None:
return
rvalue_type = self.expr_checker.accept(rvalue)
if var not in partial_types:
# Partial type may have been inferred while visiting rvalue (e.g. by walrus).
return
rvalue_type = get_proper_type(rvalue_type)
if isinstance(rvalue_type, Instance):
if rvalue_type.type == typ.type and is_valid_inferred_type(
Expand Down Expand Up @@ -5230,6 +5233,7 @@ def try_infer_partial_type_from_indexed_assignment(
if (
is_valid_inferred_type(key_type, self.options)
and is_valid_inferred_type(value_type, self.options)
and var in partial_types
and not self.current_node_deferred
and not (
typename == "collections.defaultdict"
Expand Down
5 changes: 4 additions & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -1177,7 +1177,10 @@ def try_infer_partial_type(self, e: CallExpr) -> None:
if value_type is not None:
# Infer key type.
key_type = self.accept(index)
if mypy.checker.is_valid_inferred_type(key_type, self.chk.options):
if (
mypy.checker.is_valid_inferred_type(key_type, self.chk.options)
and var in partial_types
):
# Store inferred partial type.
assert partial_type.type is not None
typename = partial_type.type.fullname
Expand Down
15 changes: 15 additions & 0 deletions test-data/unit/check-python38.test
Original file line number Diff line number Diff line change
Expand Up @@ -831,3 +831,18 @@ def fn() -> tuple[int, int]:
# E: Unsupported operand types for + ("None" and "int") \
# N: Left operand is of type "int | None"
[builtins fixtures/dict.pyi]

[case testPartialTypeResolvedByWalrusInAssignmentRvalueList]
errors = []
errors = [
i for i in [1, 2, 3] if (errors := [1])
]
reveal_type(errors) # N: Revealed type is "builtins.list[builtins.int]"
[builtins fixtures/list.pyi]

[case testPartialTypeResolvedByCallInAssignmentRvalueDict]
errors = {}
errors["42"] = 1 if int() else errors.update({"43": 1}) # E: Incompatible types in assignment (expression has type "int | None", target has type "int") \
# E: "update" of "dict" does not return a value (it only ever returns None)
reveal_type(errors) # N: Revealed type is "builtins.dict[builtins.str, builtins.int]"
[builtins fixtures/dict.pyi]
Loading