diff --git "a/assignment inference test\357\200\242" "b/assignment inference test\357\200\242" new file mode 100644 index 000000000000..d9842d517d38 --- /dev/null +++ "b/assignment inference test\357\200\242" @@ -0,0 +1,15 @@ +diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test +index 4d99325d9..0e01e6bbc 100644 +--- a/test-data/unit/check-expressions.test ++++ b/test-data/unit/check-expressions.test +@@ -2540,6 +2540,10 @@ def last_known_value() -> None: + [builtins fixtures/primitives.pyi] +  + [case walrus_operator_in_comprehension_infers_type] ++# Ensure that assignment expressions (:=) inside comprehensions ++# correctly infer and propagate the assigned variable's type ++# outside the comprehension scope. ++ + from dataclasses import dataclass +  + @dataclass diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index 447afc16c464..0fe6db9b4a5c 100644 --- a/mypy/checkexpr.py +++ b/mypy/checkexpr.py @@ -585,9 +585,10 @@ def visit_call_expr_inner(self, e: CallExpr, allow_none_return: bool = False) -> e.callee.arg_names, lambda i: self.accept(e.args[i]), ) + checked_args = [self.accept(arg) for arg in e.args] arg_types = [ - join.join_type_list([self.accept(e.args[j]) for j in formal_to_actual[i]]) + join.join_type_list([checked_args[j] for j in formal_to_actual[i]]) for i in range(len(e.callee.arg_kinds)) ] type_context = CallableType( diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test index 8a969fc54428..55e643f74cbe 100644 --- a/test-data/unit/check-expressions.test +++ b/test-data/unit/check-expressions.test @@ -2538,3 +2538,40 @@ def last_known_value() -> None: x, y, z = xy # E: Unpacking a string is disallowed reveal_type(z) # N: Revealed type is "builtins.str" [builtins fixtures/primitives.pyi] + +[case testWalrusAssignmentInCallArguments] +[builtins fixtures/tuple.pyi] + + + +# Ensure that assignment expressions (:=) in earlier call arguments +# propagate type information to later arguments in the same call. + +from dataclasses import dataclass + +@dataclass +class Notification: + request_reference: str + +@dataclass +class Case: + title: str + notifications: list[Notification] + initial_reference: str = "" + +cases = [ + *[ + Case( + title="example", + initial_reference=(request_reference := "abc"), + notifications=[ + Notification( + request_reference=request_reference, + ), + ], + ) + for _ in (1, 2) + ], +] + +reveal_type(request_reference) # N: Revealed type is "builtins.str"