mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-30 08:23:53 +00:00
[red-knot] [minor] Improve helper methods for builtin types (#13594)
This commit is contained in:
parent
edba60106b
commit
73e884b232
3 changed files with 19 additions and 15 deletions
|
@ -385,8 +385,12 @@ impl<'db> Type<'db> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn builtin_str(db: &'db dyn Db) -> Self {
|
pub fn builtin_str_instance(db: &'db dyn Db) -> Self {
|
||||||
builtins_symbol_ty(db, "str")
|
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 {
|
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::IntLiteral(_) | Type::BooleanLiteral(_) => self.repr(db),
|
||||||
Type::StringLiteral(_) | Type::LiteralString => *self,
|
Type::StringLiteral(_) | Type::LiteralString => *self,
|
||||||
// TODO: handle more complex types
|
// 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,
|
Type::LiteralString => Type::LiteralString,
|
||||||
// TODO: handle more complex types
|
// TODO: handle more complex types
|
||||||
_ => Type::builtin_str(db).to_instance(db),
|
_ => Type::builtin_str_instance(db),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -389,7 +389,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn build_union_simplify_subtype() {
|
fn build_union_simplify_subtype() {
|
||||||
let db = setup_db();
|
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 t1 = Type::LiteralString;
|
||||||
let u0 = UnionType::from_elements(&db, [t0, t1]);
|
let u0 = UnionType::from_elements(&db, [t0, t1]);
|
||||||
let u1 = UnionType::from_elements(&db, [t1, t0]);
|
let u1 = UnionType::from_elements(&db, [t1, t0]);
|
||||||
|
@ -401,7 +401,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn build_union_no_simplify_unknown() {
|
fn build_union_no_simplify_unknown() {
|
||||||
let db = setup_db();
|
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 t1 = Type::Unknown;
|
||||||
let u0 = UnionType::from_elements(&db, [t0, t1]);
|
let u0 = UnionType::from_elements(&db, [t0, t1]);
|
||||||
let u1 = UnionType::from_elements(&db, [t1, t0]);
|
let u1 = UnionType::from_elements(&db, [t1, t0]);
|
||||||
|
@ -413,8 +413,8 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn build_union_subsume_multiple() {
|
fn build_union_subsume_multiple() {
|
||||||
let db = setup_db();
|
let db = setup_db();
|
||||||
let str_ty = builtins_symbol_ty(&db, "str").to_instance(&db);
|
let str_ty = Type::builtin_str_instance(&db);
|
||||||
let int_ty = builtins_symbol_ty(&db, "int").to_instance(&db);
|
let int_ty = Type::builtin_int_instance(&db);
|
||||||
let object_ty = builtins_symbol_ty(&db, "object").to_instance(&db);
|
let object_ty = builtins_symbol_ty(&db, "object").to_instance(&db);
|
||||||
let unknown_ty = Type::Unknown;
|
let unknown_ty = Type::Unknown;
|
||||||
|
|
||||||
|
|
|
@ -1688,7 +1688,7 @@ impl<'db> TypeInferenceBuilder<'db> {
|
||||||
ast::Number::Int(n) => n
|
ast::Number::Int(n) => n
|
||||||
.as_i64()
|
.as_i64()
|
||||||
.map(Type::IntLiteral)
|
.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::Float(_) => builtins_symbol_ty(self.db, "float").to_instance(self.db),
|
||||||
ast::Number::Complex { .. } => {
|
ast::Number::Complex { .. } => {
|
||||||
builtins_symbol_ty(self.db, "complex").to_instance(self.db)
|
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
|
(Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Add) => n
|
||||||
.checked_add(m)
|
.checked_add(m)
|
||||||
.map(Type::IntLiteral)
|
.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
|
(Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Sub) => n
|
||||||
.checked_sub(m)
|
.checked_sub(m)
|
||||||
.map(Type::IntLiteral)
|
.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
|
(Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Mult) => n
|
||||||
.checked_mul(m)
|
.checked_mul(m)
|
||||||
.map(Type::IntLiteral)
|
.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) => {
|
(Type::IntLiteral(_), Type::IntLiteral(_), ast::Operator::Div) => {
|
||||||
builtins_symbol_ty(self.db, "float").to_instance(self.db)
|
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
|
(Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::FloorDiv) => n
|
||||||
.checked_div(m)
|
.checked_div(m)
|
||||||
.map(Type::IntLiteral)
|
.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
|
(Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Mod) => n
|
||||||
.checked_rem(m)
|
.checked_rem(m)
|
||||||
.map(Type::IntLiteral)
|
.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(lhs), Type::BytesLiteral(rhs), ast::Operator::Add) => {
|
||||||
Type::BytesLiteral(BytesLiteralType::new(
|
Type::BytesLiteral(BytesLiteralType::new(
|
||||||
|
@ -2911,7 +2911,7 @@ impl StringPartsCollector {
|
||||||
|
|
||||||
fn ty(self, db: &dyn Db) -> Type {
|
fn ty(self, db: &dyn Db) -> Type {
|
||||||
if self.expression {
|
if self.expression {
|
||||||
Type::builtin_str(db).to_instance(db)
|
Type::builtin_str_instance(db)
|
||||||
} else if let Some(concatenated) = self.concatenated {
|
} else if let Some(concatenated) = self.concatenated {
|
||||||
Type::StringLiteral(StringLiteralType::new(db, concatenated.into_boxed_str()))
|
Type::StringLiteral(StringLiteralType::new(db, concatenated.into_boxed_str()))
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue