Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions crates/ty_python_semantic/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7281,29 +7281,12 @@ impl<'db> Type<'db> {
// https://typing.python.org/en/latest/spec/special-types.html#special-cases-for-float-and-complex
Type::ClassLiteral(class) => {
let ty = match class.known(db) {
Some(KnownClass::Complex) => UnionType::from_elements(
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))
}
Some(KnownClass::Complex) => KnownUnion::Complex.to_type(db),
Some(KnownClass::Float) => KnownUnion::Float.to_type(db),
_ => Type::instance(db, class.default_specialization(db)),
};
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))),
Comment on lines -7299 to 7290
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whether or not the class is a TypedDict is also checked in the Type::instance() call. Checking whether the class-literal/generic-alias is a TypedDict before calling Type::instance() is redundant.


Type::SubclassOf(_)
Expand Down Expand Up @@ -14008,6 +13991,28 @@ pub(crate) enum KnownUnion {
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)]
pub struct IntersectionType<'db> {
/// The intersection type includes only values in all of these types.
Expand Down
22 changes: 5 additions & 17 deletions crates/ty_python_semantic/src/types/ide_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::types::call::{CallArguments, MatchedArgument};
use crate::types::signatures::{ParameterKind, Signature};
use crate::types::{CallDunderError, UnionType};
use crate::types::{CallableTypes, ClassBase, KnownClass, Type, TypeContext};
use crate::types::{
CallDunderError, CallableTypes, ClassBase, KnownUnion, Type, TypeContext, UnionType,
};
use crate::{Db, DisplaySettings, HasType, SemanticModel};
use ruff_db::files::FileRange;
use ruff_db::parsed::parsed_module;
Expand Down Expand Up @@ -193,21 +194,8 @@ pub fn definitions_for_name<'db>(

fn is_float_or_complex_annotation(db: &dyn Db, ty: UnionType, name: &str) -> bool {
let float_or_complex_ty = match name {
"float" => UnionType::from_elements(
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),
],
),
"float" => KnownUnion::Float.to_type(db),
"complex" => KnownUnion::Complex.to_type(db),
_ => return false,
}
.expect_union();
Expand Down
21 changes: 3 additions & 18 deletions crates/ty_python_semantic/src/types/newtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ use std::collections::BTreeSet;
use crate::Db;
use crate::semantic_index::definition::{Definition, DefinitionKind};
use crate::types::constraints::ConstraintSet;
use crate::types::{
ClassType, KnownClass, KnownUnion, Type, UnionType, definition_expression_type, visitor,
};
use crate::types::{ClassType, KnownUnion, Type, definition_expression_type, visitor};
use ruff_db::parsed::parsed_module;
use ruff_python_ast as ast;

Expand Down Expand Up @@ -236,21 +234,8 @@ impl<'db> NewTypeBase<'db> {
match self {
NewTypeBase::ClassType(class_type) => Type::instance(db, class_type),
NewTypeBase::NewType(newtype) => Type::NewTypeInstance(newtype),
NewTypeBase::Float => UnionType::from_elements(
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),
],
),
NewTypeBase::Float => KnownUnion::Float.to_type(db),
NewTypeBase::Complex => KnownUnion::Complex.to_type(db),
}
}
}
Expand Down
Loading