Skip to content

Commit 73e884b

Browse files
authored
[red-knot] [minor] Improve helper methods for builtin types (#13594)
1 parent edba601 commit 73e884b

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

crates/red_knot_python_semantic/src/types.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,12 @@ impl<'db> Type<'db> {
385385
}
386386
}
387387

388-
pub fn builtin_str(db: &'db dyn Db) -> Self {
389-
builtins_symbol_ty(db, "str")
388+
pub fn builtin_str_instance(db: &'db dyn Db) -> Self {
389+
builtins_symbol_ty(db, "str").to_instance(db)
390+
}
391+
392+
pub fn builtin_int_instance(db: &'db dyn Db) -> Self {
393+
builtins_symbol_ty(db, "int").to_instance(db)
390394
}
391395

392396
pub fn is_stdlib_symbol(&self, db: &'db dyn Db, module_name: &str, name: &str) -> bool {
@@ -777,7 +781,7 @@ impl<'db> Type<'db> {
777781
Type::IntLiteral(_) | Type::BooleanLiteral(_) => self.repr(db),
778782
Type::StringLiteral(_) | Type::LiteralString => *self,
779783
// TODO: handle more complex types
780-
_ => Type::builtin_str(db).to_instance(db),
784+
_ => Type::builtin_str_instance(db),
781785
}
782786
}
783787

@@ -800,7 +804,7 @@ impl<'db> Type<'db> {
800804
})),
801805
Type::LiteralString => Type::LiteralString,
802806
// TODO: handle more complex types
803-
_ => Type::builtin_str(db).to_instance(db),
807+
_ => Type::builtin_str_instance(db),
804808
}
805809
}
806810
}

crates/red_knot_python_semantic/src/types/builder.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ mod tests {
389389
#[test]
390390
fn build_union_simplify_subtype() {
391391
let db = setup_db();
392-
let t0 = builtins_symbol_ty(&db, "str").to_instance(&db);
392+
let t0 = Type::builtin_str_instance(&db);
393393
let t1 = Type::LiteralString;
394394
let u0 = UnionType::from_elements(&db, [t0, t1]);
395395
let u1 = UnionType::from_elements(&db, [t1, t0]);
@@ -401,7 +401,7 @@ mod tests {
401401
#[test]
402402
fn build_union_no_simplify_unknown() {
403403
let db = setup_db();
404-
let t0 = builtins_symbol_ty(&db, "str").to_instance(&db);
404+
let t0 = Type::builtin_str_instance(&db);
405405
let t1 = Type::Unknown;
406406
let u0 = UnionType::from_elements(&db, [t0, t1]);
407407
let u1 = UnionType::from_elements(&db, [t1, t0]);
@@ -413,8 +413,8 @@ mod tests {
413413
#[test]
414414
fn build_union_subsume_multiple() {
415415
let db = setup_db();
416-
let str_ty = builtins_symbol_ty(&db, "str").to_instance(&db);
417-
let int_ty = builtins_symbol_ty(&db, "int").to_instance(&db);
416+
let str_ty = Type::builtin_str_instance(&db);
417+
let int_ty = Type::builtin_int_instance(&db);
418418
let object_ty = builtins_symbol_ty(&db, "object").to_instance(&db);
419419
let unknown_ty = Type::Unknown;
420420

crates/red_knot_python_semantic/src/types/infer.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1688,7 +1688,7 @@ impl<'db> TypeInferenceBuilder<'db> {
16881688
ast::Number::Int(n) => n
16891689
.as_i64()
16901690
.map(Type::IntLiteral)
1691-
.unwrap_or_else(|| builtins_symbol_ty(self.db, "int").to_instance(self.db)),
1691+
.unwrap_or_else(|| Type::builtin_int_instance(self.db)),
16921692
ast::Number::Float(_) => builtins_symbol_ty(self.db, "float").to_instance(self.db),
16931693
ast::Number::Complex { .. } => {
16941694
builtins_symbol_ty(self.db, "complex").to_instance(self.db)
@@ -2327,17 +2327,17 @@ impl<'db> TypeInferenceBuilder<'db> {
23272327
(Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Add) => n
23282328
.checked_add(m)
23292329
.map(Type::IntLiteral)
2330-
.unwrap_or_else(|| builtins_symbol_ty(self.db, "int").to_instance(self.db)),
2330+
.unwrap_or_else(|| Type::builtin_int_instance(self.db)),
23312331

23322332
(Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Sub) => n
23332333
.checked_sub(m)
23342334
.map(Type::IntLiteral)
2335-
.unwrap_or_else(|| builtins_symbol_ty(self.db, "int").to_instance(self.db)),
2335+
.unwrap_or_else(|| Type::builtin_int_instance(self.db)),
23362336

23372337
(Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Mult) => n
23382338
.checked_mul(m)
23392339
.map(Type::IntLiteral)
2340-
.unwrap_or_else(|| builtins_symbol_ty(self.db, "int").to_instance(self.db)),
2340+
.unwrap_or_else(|| Type::builtin_int_instance(self.db)),
23412341

23422342
(Type::IntLiteral(_), Type::IntLiteral(_), ast::Operator::Div) => {
23432343
builtins_symbol_ty(self.db, "float").to_instance(self.db)
@@ -2346,12 +2346,12 @@ impl<'db> TypeInferenceBuilder<'db> {
23462346
(Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::FloorDiv) => n
23472347
.checked_div(m)
23482348
.map(Type::IntLiteral)
2349-
.unwrap_or_else(|| builtins_symbol_ty(self.db, "int").to_instance(self.db)),
2349+
.unwrap_or_else(|| Type::builtin_int_instance(self.db)),
23502350

23512351
(Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Mod) => n
23522352
.checked_rem(m)
23532353
.map(Type::IntLiteral)
2354-
.unwrap_or_else(|| builtins_symbol_ty(self.db, "int").to_instance(self.db)),
2354+
.unwrap_or_else(|| Type::builtin_int_instance(self.db)),
23552355

23562356
(Type::BytesLiteral(lhs), Type::BytesLiteral(rhs), ast::Operator::Add) => {
23572357
Type::BytesLiteral(BytesLiteralType::new(
@@ -2911,7 +2911,7 @@ impl StringPartsCollector {
29112911

29122912
fn ty(self, db: &dyn Db) -> Type {
29132913
if self.expression {
2914-
Type::builtin_str(db).to_instance(db)
2914+
Type::builtin_str_instance(db)
29152915
} else if let Some(concatenated) = self.concatenated {
29162916
Type::StringLiteral(StringLiteralType::new(db, concatenated.into_boxed_str()))
29172917
} else {

0 commit comments

Comments
 (0)