From a35cdbb27519d91de6d35e76995d1c532e0cd60c Mon Sep 17 00:00:00 2001 From: Micha Reiser Date: Wed, 21 Aug 2024 14:35:29 +0200 Subject: [PATCH] Fix various panicks when linting black/src (#13033) --- crates/red_knot_python_semantic/src/types.rs | 19 ++++++--- .../src/types/infer.rs | 40 ++++++++++++------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index 07dd1c6e1d48e..272d0bfb0cdcb 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -226,16 +226,25 @@ impl<'db> Type<'db> { pub fn member(&self, db: &'db dyn Db, name: &Name) -> Type<'db> { match self { Type::Any => Type::Any, - Type::Never => todo!("attribute lookup on Never type"), + Type::Never => { + // TODO: attribute lookup on Never type + Type::Unknown + } Type::Unknown => Type::Unknown, Type::Unbound => Type::Unbound, - Type::None => todo!("attribute lookup on None type"), - Type::Function(_) => todo!("attribute lookup on Function type"), + Type::None => { + // TODO: attribute lookup on None type + Type::Unknown + } + Type::Function(_) => { + // TODO: attribute lookup on function type + Type::Unknown + } Type::Module(file) => global_symbol_ty_by_name(db, *file, name), Type::Class(class) => class.class_member(db, name), Type::Instance(_) => { // TODO MRO? get_own_instance_member, get_instance_member - todo!("attribute lookup on Instance type") + Type::Unknown } Type::Union(union) => union .elements(db) @@ -247,7 +256,7 @@ impl<'db> Type<'db> { Type::Intersection(_) => { // TODO perform the get_member on each type in the intersection // TODO return the intersection of those results - todo!("attribute lookup on Intersection type") + Type::Unknown } Type::IntLiteral(_) => { // TODO raise error diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index 01dd540c9c7e8..93c15bc015ab0 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -1159,21 +1159,7 @@ impl<'db> TypeInferenceBuilder<'db> { flags: _, } = fstring; for element in elements { - match element { - ast::FStringElement::Literal(_) => { - // TODO string literal type - } - ast::FStringElement::Expression(expr_element) => { - let ast::FStringExpressionElement { - range: _, - expression, - debug_text: _, - conversion: _, - format_spec: _, - } = expr_element; - self.infer_expression(expression); - } - } + self.infer_fstring_element(element); } } } @@ -1183,6 +1169,30 @@ impl<'db> TypeInferenceBuilder<'db> { Type::Unknown } + fn infer_fstring_element(&mut self, element: &ast::FStringElement) { + match element { + ast::FStringElement::Literal(_) => { + // TODO string literal type + } + ast::FStringElement::Expression(expr_element) => { + let ast::FStringExpressionElement { + range: _, + expression, + debug_text: _, + conversion: _, + format_spec, + } = expr_element; + self.infer_expression(expression); + + if let Some(format_spec) = format_spec { + for spec_element in &format_spec.elements { + self.infer_fstring_element(spec_element); + } + } + } + } + } + #[allow(clippy::unused_self)] fn infer_ellipsis_literal_expression( &mut self,