From 260c2ecd15f09578b7d0027c2f34e77cd212c6f8 Mon Sep 17 00:00:00 2001 From: Carl Meyer Date: Thu, 19 Sep 2024 10:37:49 -0700 Subject: [PATCH] [red-knot] visit with-item vars even if not a Name (#13409) This fixes the last panic on checking pandas. (Match statement became an `if let` because clippy decided it wanted that once I added the additional line in the else case?) --------- Co-authored-by: Alex Waygood --- .../red_knot_python_semantic/src/types/infer.rs | 15 +++++++-------- .../test/corpus/67_with_non_name_target.py | 2 ++ 2 files changed, 9 insertions(+), 8 deletions(-) create mode 100644 crates/red_knot_workspace/resources/test/corpus/67_with_non_name_target.py 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