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
108 changes: 54 additions & 54 deletions crates/ty/docs/rules.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/ty_python_semantic/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ fn widen_type_for_undeclared_public_symbol<'db>(
// such.
let is_known_instance = inferred
.ignore_possibly_unbound()
.is_some_and(|ty| matches!(ty, Type::KnownInstance(_)));
.is_some_and(|ty| matches!(ty, Type::SpecialForm(_) | Type::KnownInstance(_)));

if is_considered_non_modifiable || is_known_instance {
inferred
Expand Down
319 changes: 231 additions & 88 deletions crates/ty_python_semantic/src/types.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions crates/ty_python_semantic/src/types/call/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::types::signatures::{Parameter, ParameterForm};
use crate::types::{
BoundMethodType, DataclassParams, DataclassTransformerParams, FunctionDecorators, FunctionType,
KnownClass, KnownFunction, KnownInstanceType, MethodWrapperKind, PropertyInstanceType,
TupleType, TypeMapping, UnionType, WrapperDescriptorKind, todo_type,
SpecialFormType, TupleType, TypeMapping, UnionType, WrapperDescriptorKind, todo_type,
};
use ruff_db::diagnostic::{Annotation, Diagnostic, Severity, SubDiagnostic};
use ruff_python_ast as ast;
Expand Down Expand Up @@ -933,7 +933,7 @@ impl<'db> Bindings<'db> {
_ => {}
},

Type::KnownInstance(KnownInstanceType::TypedDict) => {
Type::SpecialForm(SpecialFormType::TypedDict) => {
overload.set_return_type(todo_type!("TypedDict"));
}

Expand Down
22 changes: 12 additions & 10 deletions crates/ty_python_semantic/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::sync::{LazyLock, Mutex};

use super::{
IntersectionBuilder, KnownFunction, MemberLookupPolicy, Mro, MroError, MroIterator,
SubclassOfType, Truthiness, Type, TypeQualifiers, class_base::ClassBase, infer_expression_type,
infer_unpack_types,
SpecialFormType, SubclassOfType, Truthiness, Type, TypeQualifiers, class_base::ClassBase,
infer_expression_type, infer_unpack_types,
};
use crate::semantic_index::DeclarationWithConstraint;
use crate::semantic_index::definition::Definition;
Expand Down Expand Up @@ -729,9 +729,9 @@ impl<'db> ClassLiteral<'db> {
pub(crate) fn legacy_generic_context(self, db: &'db dyn Db) -> Option<GenericContext<'db>> {
self.explicit_bases(db).iter().find_map(|base| match base {
Type::KnownInstance(
KnownInstanceType::Generic(generic_context)
| KnownInstanceType::Protocol(generic_context),
) => *generic_context,
KnownInstanceType::SubscriptedGeneric(generic_context)
| KnownInstanceType::SubscriptedProtocol(generic_context),
) => Some(*generic_context),
_ => None,
})
}
Expand Down Expand Up @@ -879,11 +879,13 @@ impl<'db> ClassLiteral<'db> {
// - OR be the last-but-one base (with the final base being `Generic[]` or `object`)
// - OR be the last-but-two base (with the penultimate base being `Generic[]`
// and the final base being `object`)
self.explicit_bases(db)
.iter()
.rev()
.take(3)
.any(|base| matches!(base, Type::KnownInstance(KnownInstanceType::Protocol(_))))
self.explicit_bases(db).iter().rev().take(3).any(|base| {
matches!(
base,
Type::SpecialForm(SpecialFormType::Protocol)
| Type::KnownInstance(KnownInstanceType::SubscriptedProtocol(_))
)
})
})
}

Expand Down
100 changes: 54 additions & 46 deletions crates/ty_python_semantic/src/types/class_base.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::Db;
use crate::types::generics::Specialization;
use crate::types::{
ClassType, DynamicType, KnownClass, KnownInstanceType, MroError, MroIterator, Type,
TypeMapping, todo_type,
ClassType, DynamicType, KnownClass, KnownInstanceType, MroError, MroIterator, SpecialFormType,
Type, TypeMapping, todo_type,
};

/// Enumeration of the possible kinds of types we allow in class bases.
Expand Down Expand Up @@ -147,74 +147,82 @@ impl<'db> ClassBase<'db> {
| Type::ProtocolInstance(_)
| Type::AlwaysFalsy
| Type::AlwaysTruthy => None,

Type::KnownInstance(known_instance) => match known_instance {
KnownInstanceType::TypeVar(_)
| KnownInstanceType::TypeAliasType(_)
| KnownInstanceType::Annotated
| KnownInstanceType::Literal
| KnownInstanceType::LiteralString
| KnownInstanceType::Union
| KnownInstanceType::NoReturn
| KnownInstanceType::Never
| KnownInstanceType::Final
| KnownInstanceType::NotRequired
| KnownInstanceType::TypeGuard
| KnownInstanceType::TypeIs
| KnownInstanceType::TypingSelf
| KnownInstanceType::Unpack
| KnownInstanceType::ClassVar
| KnownInstanceType::Concatenate
| KnownInstanceType::Required
| KnownInstanceType::TypeAlias
| KnownInstanceType::ReadOnly
| KnownInstanceType::Optional
| KnownInstanceType::Not
| KnownInstanceType::Intersection
| KnownInstanceType::TypeOf
| KnownInstanceType::CallableTypeOf
| KnownInstanceType::AlwaysTruthy
| KnownInstanceType::AlwaysFalsy => None,
KnownInstanceType::Unknown => Some(Self::unknown()),
KnownInstanceType::SubscriptedGeneric(_) => Some(Self::Generic),
KnownInstanceType::SubscriptedProtocol(_) => Some(Self::Protocol),
KnownInstanceType::TypeAliasType(_) | KnownInstanceType::TypeVar(_) => None,
},

Type::SpecialForm(special_form) => match special_form {
SpecialFormType::Annotated
| SpecialFormType::Literal
| SpecialFormType::LiteralString
| SpecialFormType::Union
| SpecialFormType::NoReturn
| SpecialFormType::Never
| SpecialFormType::Final
| SpecialFormType::NotRequired
| SpecialFormType::TypeGuard
| SpecialFormType::TypeIs
| SpecialFormType::TypingSelf
| SpecialFormType::Unpack
| SpecialFormType::ClassVar
| SpecialFormType::Concatenate
| SpecialFormType::Required
| SpecialFormType::TypeAlias
| SpecialFormType::ReadOnly
| SpecialFormType::Optional
| SpecialFormType::Not
| SpecialFormType::Intersection
| SpecialFormType::TypeOf
| SpecialFormType::CallableTypeOf
| SpecialFormType::AlwaysTruthy
| SpecialFormType::AlwaysFalsy => None,

SpecialFormType::Unknown => Some(Self::unknown()),

SpecialFormType::Protocol => Some(Self::Protocol),
SpecialFormType::Generic => Some(Self::Generic),

// TODO: Classes inheriting from `typing.Type` et al. also have `Generic` in their MRO
KnownInstanceType::Dict => {
SpecialFormType::Dict => {
Self::try_from_type(db, KnownClass::Dict.to_class_literal(db))
}
KnownInstanceType::List => {
SpecialFormType::List => {
Self::try_from_type(db, KnownClass::List.to_class_literal(db))
}
KnownInstanceType::Type => {
SpecialFormType::Type => {
Self::try_from_type(db, KnownClass::Type.to_class_literal(db))
}
KnownInstanceType::Tuple => {
SpecialFormType::Tuple => {
Self::try_from_type(db, KnownClass::Tuple.to_class_literal(db))
}
KnownInstanceType::Set => {
SpecialFormType::Set => {
Self::try_from_type(db, KnownClass::Set.to_class_literal(db))
}
KnownInstanceType::FrozenSet => {
SpecialFormType::FrozenSet => {
Self::try_from_type(db, KnownClass::FrozenSet.to_class_literal(db))
}
KnownInstanceType::ChainMap => {
SpecialFormType::ChainMap => {
Self::try_from_type(db, KnownClass::ChainMap.to_class_literal(db))
}
KnownInstanceType::Counter => {
SpecialFormType::Counter => {
Self::try_from_type(db, KnownClass::Counter.to_class_literal(db))
}
KnownInstanceType::DefaultDict => {
SpecialFormType::DefaultDict => {
Self::try_from_type(db, KnownClass::DefaultDict.to_class_literal(db))
}
KnownInstanceType::Deque => {
SpecialFormType::Deque => {
Self::try_from_type(db, KnownClass::Deque.to_class_literal(db))
}
KnownInstanceType::OrderedDict => {
SpecialFormType::OrderedDict => {
Self::try_from_type(db, KnownClass::OrderedDict.to_class_literal(db))
}
KnownInstanceType::TypedDict => Self::try_from_type(db, todo_type!("TypedDict")),
KnownInstanceType::Callable => {
SpecialFormType::TypedDict => Self::try_from_type(db, todo_type!("TypedDict")),
SpecialFormType::Callable => {
Self::try_from_type(db, todo_type!("Support for Callable as a base class"))
}
KnownInstanceType::Protocol(_) => Some(ClassBase::Protocol),
KnownInstanceType::Generic(_) => Some(ClassBase::Generic),
},
}
}
Expand Down Expand Up @@ -288,8 +296,8 @@ impl<'db> From<ClassBase<'db>> for Type<'db> {
match value {
ClassBase::Dynamic(dynamic) => Type::Dynamic(dynamic),
ClassBase::Class(class) => class.into(),
ClassBase::Protocol => Type::KnownInstance(KnownInstanceType::Protocol(None)),
ClassBase::Generic => Type::KnownInstance(KnownInstanceType::Generic(None)),
ClassBase::Protocol => Type::SpecialForm(SpecialFormType::Protocol),
ClassBase::Generic => Type::SpecialForm(SpecialFormType::Generic),
}
}
}
Expand Down
9 changes: 3 additions & 6 deletions crates/ty_python_semantic/src/types/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use super::{
CallArgumentTypes, CallDunderError, ClassBase, ClassLiteral, KnownClass,
add_inferred_python_version_hint_to_diagnostic,
};
use crate::db::Db;
use crate::declare_lint;
use crate::lint::{Level, LintRegistryBuilder, LintStatus};
use crate::suppression::FileSuppressionId;
Expand All @@ -15,7 +14,7 @@ use crate::types::string_annotation::{
IMPLICIT_CONCATENATED_STRING_TYPE_ANNOTATION, INVALID_SYNTAX_IN_FORWARD_ANNOTATION,
RAW_STRING_TYPE_ANNOTATION,
};
use crate::types::{KnownFunction, KnownInstanceType, Type, protocol_class::ProtocolClassLiteral};
use crate::types::{KnownFunction, SpecialFormType, Type, protocol_class::ProtocolClassLiteral};
use itertools::Itertools;
use ruff_db::diagnostic::{Annotation, Diagnostic, Severity, SubDiagnostic};
use ruff_python_ast::{self as ast, AnyNodeRef};
Expand Down Expand Up @@ -1823,7 +1822,6 @@ pub(crate) fn report_base_with_incompatible_slots(context: &InferContext, node:
}

pub(crate) fn report_invalid_arguments_to_annotated(
db: &dyn Db,
context: &InferContext,
subscript: &ast::ExprSubscript,
) {
Expand All @@ -1833,7 +1831,7 @@ pub(crate) fn report_invalid_arguments_to_annotated(
builder.into_diagnostic(format_args!(
"Special form `{}` expected at least 2 arguments \
(one type and at least one metadata element)",
KnownInstanceType::Annotated.repr(db)
SpecialFormType::Annotated
));
}

Expand Down Expand Up @@ -1873,7 +1871,6 @@ pub(crate) fn report_bad_argument_to_get_protocol_members(
}

pub(crate) fn report_invalid_arguments_to_callable(
db: &dyn Db,
context: &InferContext,
subscript: &ast::ExprSubscript,
) {
Expand All @@ -1882,7 +1879,7 @@ pub(crate) fn report_invalid_arguments_to_callable(
};
builder.into_diagnostic(format_args!(
"Special form `{}` expected exactly two arguments (parameter types and return type)",
KnownInstanceType::Callable.repr(db)
SpecialFormType::Callable
));
}

Expand Down
1 change: 1 addition & 0 deletions crates/ty_python_semantic/src/types/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ impl Display for DisplayRepresentation<'_> {
SubclassOfInner::Class(class) => write!(f, "type[{}]", class.name(self.db)),
SubclassOfInner::Dynamic(dynamic) => write!(f, "type[{dynamic}]"),
},
Type::SpecialForm(special_form) => special_form.fmt(f),
Type::KnownInstance(known_instance) => known_instance.repr(self.db).fmt(f),
Type::FunctionLiteral(function) => {
let signature = function.signature(self.db);
Expand Down
Loading
Loading