diff --git a/crates/red_knot_python_semantic/src/semantic_index/builder.rs b/crates/red_knot_python_semantic/src/semantic_index/builder.rs index 049712feaf..5db6aca1d8 100644 --- a/crates/red_knot_python_semantic/src/semantic_index/builder.rs +++ b/crates/red_knot_python_semantic/src/semantic_index/builder.rs @@ -658,11 +658,11 @@ where } ast::Expr::Named(node) => { debug_assert!(self.current_assignment.is_none()); - self.current_assignment = Some(node.into()); // TODO walrus in comprehensions is implicitly nonlocal + self.visit_expr(&node.value); + self.current_assignment = Some(node.into()); self.visit_expr(&node.target); self.current_assignment = None; - self.visit_expr(&node.value); } ast::Expr::Lambda(lambda) => { if let Some(parameters) = &lambda.parameters { diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index 28fb0a002c..6f00e36b63 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -2290,6 +2290,23 @@ mod tests { Ok(()) } + #[test] + fn walrus_self_plus_one() -> anyhow::Result<()> { + let mut db = setup_db(); + + db.write_dedented( + "src/a.py", + " + x = 0 + (x := x + 1) + ", + )?; + + assert_public_ty(&db, "src/a.py", "x", "Literal[1]"); + + Ok(()) + } + #[test] fn ifexpr() -> anyhow::Result<()> { let mut db = setup_db(); diff --git a/crates/red_knot_workspace/resources/test/corpus/04_assign_named_expr.py b/crates/red_knot_workspace/resources/test/corpus/04_assign_named_expr.py new file mode 100644 index 0000000000..9147747776 --- /dev/null +++ b/crates/red_knot_workspace/resources/test/corpus/04_assign_named_expr.py @@ -0,0 +1,2 @@ +x = 0 +(x := x + 1) diff --git a/crates/red_knot_workspace/resources/test/corpus/10_if_with_named_expr.py b/crates/red_knot_workspace/resources/test/corpus/10_if_with_named_expr.py new file mode 100644 index 0000000000..7b602e79c2 --- /dev/null +++ b/crates/red_knot_workspace/resources/test/corpus/10_if_with_named_expr.py @@ -0,0 +1,3 @@ +x = 0 +if x := x + 1: + pass diff --git a/crates/red_knot_workspace/resources/test/corpus/85_match_guard_with_named_expr.py b/crates/red_knot_workspace/resources/test/corpus/85_match_guard_with_named_expr.py new file mode 100644 index 0000000000..6393f73a36 --- /dev/null +++ b/crates/red_knot_workspace/resources/test/corpus/85_match_guard_with_named_expr.py @@ -0,0 +1,3 @@ +match x: + case [1, 0] if x := x[:0]: + y = 1