mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:24:57 +00:00
Avoid type mapping
This commit is contained in:
parent
0fc7ddb5c9
commit
4b0d423b57
4 changed files with 18 additions and 16 deletions
|
@ -5359,7 +5359,7 @@ impl<'db> Type<'db> {
|
|||
// It is important that identity_specialization specializes the class with
|
||||
// _inferable_ typevars, so that our specialization inference logic will
|
||||
// try to find a specialization for them.
|
||||
Type::from(class.identity_specialization(db, Type::TypeVar)),
|
||||
Type::from(class.identity_specialization(db, &Type::TypeVar)),
|
||||
),
|
||||
_ => (None, None, self),
|
||||
},
|
||||
|
@ -5717,8 +5717,15 @@ impl<'db> Type<'db> {
|
|||
],
|
||||
});
|
||||
};
|
||||
let self_type = typing_self(db, scope_id, typevar_binding_context, class);
|
||||
Ok(self_type.map(Type::NonInferableTypeVar).unwrap_or(*self))
|
||||
|
||||
Ok(typing_self(
|
||||
db,
|
||||
scope_id,
|
||||
typevar_binding_context,
|
||||
class,
|
||||
&Type::NonInferableTypeVar,
|
||||
)
|
||||
.unwrap_or(*self))
|
||||
}
|
||||
SpecialFormType::TypeAlias => Ok(Type::Dynamic(DynamicType::TodoTypeAlias)),
|
||||
SpecialFormType::TypedDict => Err(InvalidTypeExpressionError {
|
||||
|
|
|
@ -1532,7 +1532,7 @@ impl<'db> ClassLiteral<'db> {
|
|||
pub(crate) fn identity_specialization(
|
||||
self,
|
||||
db: &'db dyn Db,
|
||||
typevar_to_type: impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>,
|
||||
typevar_to_type: &impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>,
|
||||
) -> ClassType<'db> {
|
||||
self.apply_specialization(db, |generic_context| {
|
||||
generic_context.identity_specialization(db, typevar_to_type)
|
||||
|
|
|
@ -107,7 +107,8 @@ pub(crate) fn typing_self<'db>(
|
|||
scope_id: ScopeId,
|
||||
typevar_binding_context: Option<Definition<'db>>,
|
||||
class: ClassLiteral<'db>,
|
||||
) -> Option<BoundTypeVarInstance<'db>> {
|
||||
typevar_to_type: &impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>,
|
||||
) -> Option<Type<'db>> {
|
||||
let module = parsed_module(db, scope_id.file(db)).load(db);
|
||||
let index = semantic_index(db, scope_id.file(db));
|
||||
|
||||
|
@ -118,7 +119,7 @@ pub(crate) fn typing_self<'db>(
|
|||
Some(
|
||||
TypeVarBoundOrConstraints::UpperBound(Type::instance(
|
||||
db,
|
||||
class.identity_specialization(db, Type::NonInferableTypeVar),
|
||||
class.identity_specialization(db, typevar_to_type),
|
||||
))
|
||||
.into(),
|
||||
),
|
||||
|
@ -138,6 +139,7 @@ pub(crate) fn typing_self<'db>(
|
|||
typevar_binding_context,
|
||||
typevar,
|
||||
)
|
||||
.map(typevar_to_type)
|
||||
}
|
||||
|
||||
/// A list of formal type variables for a generic function, class, or type alias.
|
||||
|
@ -333,7 +335,7 @@ impl<'db> GenericContext<'db> {
|
|||
pub(crate) fn identity_specialization(
|
||||
self,
|
||||
db: &'db dyn Db,
|
||||
typevar_to_type: impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>,
|
||||
typevar_to_type: &impl Fn(BoundTypeVarInstance<'db>) -> Type<'db>,
|
||||
) -> Specialization<'db> {
|
||||
let types = self
|
||||
.variables(db)
|
||||
|
|
|
@ -1255,15 +1255,8 @@ impl<'db> Parameters<'db> {
|
|||
let class = nearest_enclosing_class(db, index, scope_id).unwrap();
|
||||
|
||||
Some(
|
||||
Type::NonInferableTypeVar(
|
||||
typing_self(db, scope_id, typevar_binding_context, class).unwrap(),
|
||||
)
|
||||
.apply_type_mapping(
|
||||
db,
|
||||
&TypeMapping::MarkTypeVarsInferable(Some(BindingContext::Definition(
|
||||
definition,
|
||||
))),
|
||||
),
|
||||
typing_self(db, scope_id, typevar_binding_context, class, &Type::TypeVar)
|
||||
.unwrap(),
|
||||
)
|
||||
} else {
|
||||
// For methods of non-generic classes that are not otherwise generic (e.g. return `Self` or
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue