diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index f7ad760382c3f..611c6307b13b1 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -926,14 +926,13 @@ impl<'db> TypeInferenceBuilder<'db> { } = with_statement; for item in items { - match item.optional_vars.as_deref() { - Some(ast::Expr::Name(name)) => { - self.infer_definition(name); - } - _ => { - // TODO infer definitions in unpacking assignment - self.infer_expression(&item.context_expr); - } + let target = item.optional_vars.as_deref(); + if let Some(ast::Expr::Name(name)) = target { + self.infer_definition(name); + } else { + // TODO infer definitions in unpacking assignment + self.infer_expression(&item.context_expr); + self.infer_optional_expression(target); } } diff --git a/crates/red_knot_workspace/resources/test/corpus/67_with_non_name_target.py b/crates/red_knot_workspace/resources/test/corpus/67_with_non_name_target.py new file mode 100644 index 0000000000000..d70cf0c9b509c --- /dev/null +++ b/crates/red_knot_workspace/resources/test/corpus/67_with_non_name_target.py @@ -0,0 +1,2 @@ +with foo() as self.bar: + pass