From 73e884b2326ed44f1937a33f00944c110d73ee2f Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Tue, 1 Oct 2024 18:38:33 +0100 Subject: [PATCH] [red-knot] [minor] Improve helper methods for builtin types (#13594) --- crates/red_knot_python_semantic/src/types.rs | 12 ++++++++---- .../red_knot_python_semantic/src/types/builder.rs | 8 ++++---- crates/red_knot_python_semantic/src/types/infer.rs | 14 +++++++------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/crates/red_knot_python_semantic/src/types.rs b/crates/red_knot_python_semantic/src/types.rs index 50bab3d555..2268183d1d 100644 --- a/crates/red_knot_python_semantic/src/types.rs +++ b/crates/red_knot_python_semantic/src/types.rs @@ -385,8 +385,12 @@ impl<'db> Type<'db> { } } - pub fn builtin_str(db: &'db dyn Db) -> Self { - builtins_symbol_ty(db, "str") + pub fn builtin_str_instance(db: &'db dyn Db) -> Self { + builtins_symbol_ty(db, "str").to_instance(db) + } + + pub fn builtin_int_instance(db: &'db dyn Db) -> Self { + builtins_symbol_ty(db, "int").to_instance(db) } pub fn is_stdlib_symbol(&self, db: &'db dyn Db, module_name: &str, name: &str) -> bool { @@ -777,7 +781,7 @@ impl<'db> Type<'db> { Type::IntLiteral(_) | Type::BooleanLiteral(_) => self.repr(db), Type::StringLiteral(_) | Type::LiteralString => *self, // TODO: handle more complex types - _ => Type::builtin_str(db).to_instance(db), + _ => Type::builtin_str_instance(db), } } @@ -800,7 +804,7 @@ impl<'db> Type<'db> { })), Type::LiteralString => Type::LiteralString, // TODO: handle more complex types - _ => Type::builtin_str(db).to_instance(db), + _ => Type::builtin_str_instance(db), } } } diff --git a/crates/red_knot_python_semantic/src/types/builder.rs b/crates/red_knot_python_semantic/src/types/builder.rs index f264dc4f8f..1ba6bc72c4 100644 --- a/crates/red_knot_python_semantic/src/types/builder.rs +++ b/crates/red_knot_python_semantic/src/types/builder.rs @@ -389,7 +389,7 @@ mod tests { #[test] fn build_union_simplify_subtype() { let db = setup_db(); - let t0 = builtins_symbol_ty(&db, "str").to_instance(&db); + let t0 = Type::builtin_str_instance(&db); let t1 = Type::LiteralString; let u0 = UnionType::from_elements(&db, [t0, t1]); let u1 = UnionType::from_elements(&db, [t1, t0]); @@ -401,7 +401,7 @@ mod tests { #[test] fn build_union_no_simplify_unknown() { let db = setup_db(); - let t0 = builtins_symbol_ty(&db, "str").to_instance(&db); + let t0 = Type::builtin_str_instance(&db); let t1 = Type::Unknown; let u0 = UnionType::from_elements(&db, [t0, t1]); let u1 = UnionType::from_elements(&db, [t1, t0]); @@ -413,8 +413,8 @@ mod tests { #[test] fn build_union_subsume_multiple() { let db = setup_db(); - let str_ty = builtins_symbol_ty(&db, "str").to_instance(&db); - let int_ty = builtins_symbol_ty(&db, "int").to_instance(&db); + let str_ty = Type::builtin_str_instance(&db); + let int_ty = Type::builtin_int_instance(&db); let object_ty = builtins_symbol_ty(&db, "object").to_instance(&db); let unknown_ty = Type::Unknown; diff --git a/crates/red_knot_python_semantic/src/types/infer.rs b/crates/red_knot_python_semantic/src/types/infer.rs index f1444663cd..33155ad6cb 100644 --- a/crates/red_knot_python_semantic/src/types/infer.rs +++ b/crates/red_knot_python_semantic/src/types/infer.rs @@ -1688,7 +1688,7 @@ impl<'db> TypeInferenceBuilder<'db> { ast::Number::Int(n) => n .as_i64() .map(Type::IntLiteral) - .unwrap_or_else(|| builtins_symbol_ty(self.db, "int").to_instance(self.db)), + .unwrap_or_else(|| Type::builtin_int_instance(self.db)), ast::Number::Float(_) => builtins_symbol_ty(self.db, "float").to_instance(self.db), ast::Number::Complex { .. } => { builtins_symbol_ty(self.db, "complex").to_instance(self.db) @@ -2327,17 +2327,17 @@ impl<'db> TypeInferenceBuilder<'db> { (Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Add) => n .checked_add(m) .map(Type::IntLiteral) - .unwrap_or_else(|| builtins_symbol_ty(self.db, "int").to_instance(self.db)), + .unwrap_or_else(|| Type::builtin_int_instance(self.db)), (Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Sub) => n .checked_sub(m) .map(Type::IntLiteral) - .unwrap_or_else(|| builtins_symbol_ty(self.db, "int").to_instance(self.db)), + .unwrap_or_else(|| Type::builtin_int_instance(self.db)), (Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Mult) => n .checked_mul(m) .map(Type::IntLiteral) - .unwrap_or_else(|| builtins_symbol_ty(self.db, "int").to_instance(self.db)), + .unwrap_or_else(|| Type::builtin_int_instance(self.db)), (Type::IntLiteral(_), Type::IntLiteral(_), ast::Operator::Div) => { builtins_symbol_ty(self.db, "float").to_instance(self.db) @@ -2346,12 +2346,12 @@ impl<'db> TypeInferenceBuilder<'db> { (Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::FloorDiv) => n .checked_div(m) .map(Type::IntLiteral) - .unwrap_or_else(|| builtins_symbol_ty(self.db, "int").to_instance(self.db)), + .unwrap_or_else(|| Type::builtin_int_instance(self.db)), (Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Mod) => n .checked_rem(m) .map(Type::IntLiteral) - .unwrap_or_else(|| builtins_symbol_ty(self.db, "int").to_instance(self.db)), + .unwrap_or_else(|| Type::builtin_int_instance(self.db)), (Type::BytesLiteral(lhs), Type::BytesLiteral(rhs), ast::Operator::Add) => { Type::BytesLiteral(BytesLiteralType::new( @@ -2911,7 +2911,7 @@ impl StringPartsCollector { fn ty(self, db: &dyn Db) -> Type { if self.expression { - Type::builtin_str(db).to_instance(db) + Type::builtin_str_instance(db) } else if let Some(concatenated) = self.concatenated { Type::StringLiteral(StringLiteralType::new(db, concatenated.into_boxed_str())) } else {