mirror of https://github.com/astral-sh/ruff
[ty] Add `KnownUnion::to_type()` (#21948)
This commit is contained in:
parent
bc8efa2fd8
commit
ff0ed4e752
|
|
@ -7281,29 +7281,12 @@ impl<'db> Type<'db> {
|
||||||
// https://typing.python.org/en/latest/spec/special-types.html#special-cases-for-float-and-complex
|
// https://typing.python.org/en/latest/spec/special-types.html#special-cases-for-float-and-complex
|
||||||
Type::ClassLiteral(class) => {
|
Type::ClassLiteral(class) => {
|
||||||
let ty = match class.known(db) {
|
let ty = match class.known(db) {
|
||||||
Some(KnownClass::Complex) => UnionType::from_elements(
|
Some(KnownClass::Complex) => KnownUnion::Complex.to_type(db),
|
||||||
db,
|
Some(KnownClass::Float) => KnownUnion::Float.to_type(db),
|
||||||
[
|
|
||||||
KnownClass::Int.to_instance(db),
|
|
||||||
KnownClass::Float.to_instance(db),
|
|
||||||
KnownClass::Complex.to_instance(db),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
Some(KnownClass::Float) => UnionType::from_elements(
|
|
||||||
db,
|
|
||||||
[
|
|
||||||
KnownClass::Int.to_instance(db),
|
|
||||||
KnownClass::Float.to_instance(db),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
_ if class.is_typed_dict(db) => {
|
|
||||||
Type::typed_dict(class.default_specialization(db))
|
|
||||||
}
|
|
||||||
_ => Type::instance(db, class.default_specialization(db)),
|
_ => Type::instance(db, class.default_specialization(db)),
|
||||||
};
|
};
|
||||||
Ok(ty)
|
Ok(ty)
|
||||||
}
|
}
|
||||||
Type::GenericAlias(alias) if alias.is_typed_dict(db) => Ok(Type::typed_dict(*alias)),
|
|
||||||
Type::GenericAlias(alias) => Ok(Type::instance(db, ClassType::from(*alias))),
|
Type::GenericAlias(alias) => Ok(Type::instance(db, ClassType::from(*alias))),
|
||||||
|
|
||||||
Type::SubclassOf(_)
|
Type::SubclassOf(_)
|
||||||
|
|
@ -14008,6 +13991,28 @@ pub(crate) enum KnownUnion {
|
||||||
Complex, // `int | float | complex`
|
Complex, // `int | float | complex`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl KnownUnion {
|
||||||
|
pub(crate) fn to_type(self, db: &dyn Db) -> Type<'_> {
|
||||||
|
match self {
|
||||||
|
KnownUnion::Float => UnionType::from_elements(
|
||||||
|
db,
|
||||||
|
[
|
||||||
|
KnownClass::Int.to_instance(db),
|
||||||
|
KnownClass::Float.to_instance(db),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
KnownUnion::Complex => UnionType::from_elements(
|
||||||
|
db,
|
||||||
|
[
|
||||||
|
KnownClass::Int.to_instance(db),
|
||||||
|
KnownClass::Float.to_instance(db),
|
||||||
|
KnownClass::Complex.to_instance(db),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[salsa::interned(debug, heap_size=IntersectionType::heap_size)]
|
#[salsa::interned(debug, heap_size=IntersectionType::heap_size)]
|
||||||
pub struct IntersectionType<'db> {
|
pub struct IntersectionType<'db> {
|
||||||
/// The intersection type includes only values in all of these types.
|
/// The intersection type includes only values in all of these types.
|
||||||
|
|
|
||||||
|
|
@ -7,8 +7,9 @@ use crate::semantic_index::definition::DefinitionKind;
|
||||||
use crate::semantic_index::{attribute_scopes, global_scope, semantic_index, use_def_map};
|
use crate::semantic_index::{attribute_scopes, global_scope, semantic_index, use_def_map};
|
||||||
use crate::types::call::{CallArguments, MatchedArgument};
|
use crate::types::call::{CallArguments, MatchedArgument};
|
||||||
use crate::types::signatures::{ParameterKind, Signature};
|
use crate::types::signatures::{ParameterKind, Signature};
|
||||||
use crate::types::{CallDunderError, UnionType};
|
use crate::types::{
|
||||||
use crate::types::{CallableTypes, ClassBase, KnownClass, Type, TypeContext};
|
CallDunderError, CallableTypes, ClassBase, KnownUnion, Type, TypeContext, UnionType,
|
||||||
|
};
|
||||||
use crate::{Db, DisplaySettings, HasType, SemanticModel};
|
use crate::{Db, DisplaySettings, HasType, SemanticModel};
|
||||||
use ruff_db::files::FileRange;
|
use ruff_db::files::FileRange;
|
||||||
use ruff_db::parsed::parsed_module;
|
use ruff_db::parsed::parsed_module;
|
||||||
|
|
@ -193,21 +194,8 @@ pub fn definitions_for_name<'db>(
|
||||||
|
|
||||||
fn is_float_or_complex_annotation(db: &dyn Db, ty: UnionType, name: &str) -> bool {
|
fn is_float_or_complex_annotation(db: &dyn Db, ty: UnionType, name: &str) -> bool {
|
||||||
let float_or_complex_ty = match name {
|
let float_or_complex_ty = match name {
|
||||||
"float" => UnionType::from_elements(
|
"float" => KnownUnion::Float.to_type(db),
|
||||||
db,
|
"complex" => KnownUnion::Complex.to_type(db),
|
||||||
[
|
|
||||||
KnownClass::Int.to_instance(db),
|
|
||||||
KnownClass::Float.to_instance(db),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
"complex" => UnionType::from_elements(
|
|
||||||
db,
|
|
||||||
[
|
|
||||||
KnownClass::Int.to_instance(db),
|
|
||||||
KnownClass::Float.to_instance(db),
|
|
||||||
KnownClass::Complex.to_instance(db),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
_ => return false,
|
_ => return false,
|
||||||
}
|
}
|
||||||
.expect_union();
|
.expect_union();
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,7 @@ use std::collections::BTreeSet;
|
||||||
use crate::Db;
|
use crate::Db;
|
||||||
use crate::semantic_index::definition::{Definition, DefinitionKind};
|
use crate::semantic_index::definition::{Definition, DefinitionKind};
|
||||||
use crate::types::constraints::ConstraintSet;
|
use crate::types::constraints::ConstraintSet;
|
||||||
use crate::types::{
|
use crate::types::{ClassType, KnownUnion, Type, definition_expression_type, visitor};
|
||||||
ClassType, KnownClass, KnownUnion, Type, UnionType, definition_expression_type, visitor,
|
|
||||||
};
|
|
||||||
use ruff_db::parsed::parsed_module;
|
use ruff_db::parsed::parsed_module;
|
||||||
use ruff_python_ast as ast;
|
use ruff_python_ast as ast;
|
||||||
|
|
||||||
|
|
@ -236,21 +234,8 @@ impl<'db> NewTypeBase<'db> {
|
||||||
match self {
|
match self {
|
||||||
NewTypeBase::ClassType(class_type) => Type::instance(db, class_type),
|
NewTypeBase::ClassType(class_type) => Type::instance(db, class_type),
|
||||||
NewTypeBase::NewType(newtype) => Type::NewTypeInstance(newtype),
|
NewTypeBase::NewType(newtype) => Type::NewTypeInstance(newtype),
|
||||||
NewTypeBase::Float => UnionType::from_elements(
|
NewTypeBase::Float => KnownUnion::Float.to_type(db),
|
||||||
db,
|
NewTypeBase::Complex => KnownUnion::Complex.to_type(db),
|
||||||
[
|
|
||||||
KnownClass::Int.to_instance(db),
|
|
||||||
KnownClass::Float.to_instance(db),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
NewTypeBase::Complex => UnionType::from_elements(
|
|
||||||
db,
|
|
||||||
[
|
|
||||||
KnownClass::Int.to_instance(db),
|
|
||||||
KnownClass::Float.to_instance(db),
|
|
||||||
KnownClass::Complex.to_instance(db),
|
|
||||||
],
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue