mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:24:57 +00:00
Code simplification
This commit is contained in:
parent
ec3520bbe4
commit
40ffa0c6da
1 changed files with 41 additions and 53 deletions
|
@ -1268,63 +1268,51 @@ impl<'db> Parameters<'db> {
|
||||||
method_type: method,
|
method_type: method,
|
||||||
class_type: class,
|
class_type: class,
|
||||||
}) = method_info
|
}) = method_info
|
||||||
|
&& !is_staticmethod
|
||||||
|
&& !is_classmethod
|
||||||
|
&& arg.parameter.annotation().is_none()
|
||||||
|
&& parameters.index(arg.name().id()) == Some(0)
|
||||||
{
|
{
|
||||||
if !is_staticmethod
|
let method_has_self_in_generic_context =
|
||||||
&& !is_classmethod
|
method.signature(db).overloads.iter().any(|s| {
|
||||||
&& arg.parameter.annotation().is_none()
|
if let Some(context) = s.generic_context {
|
||||||
&& parameters.index(arg.name().id()) == Some(0)
|
context
|
||||||
|
.variables(db)
|
||||||
|
.iter()
|
||||||
|
.any(|v| v.typevar(db).kind(db) == TypeVarKind::TypingSelf);
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let implicit_annotation = if !method_has_self_in_generic_context
|
||||||
|
&& class.is_not_generic()
|
||||||
|
&& !class.known(db).is_some_and(KnownClass::is_fallback_class)
|
||||||
{
|
{
|
||||||
let method_has_self_in_generic_context =
|
Type::instance(db, class)
|
||||||
method.signature(db).overloads.iter().any(|s| {
|
|
||||||
if let Some(context) = s.generic_context {
|
|
||||||
context
|
|
||||||
.variables(db)
|
|
||||||
.iter()
|
|
||||||
.any(|v| v.typevar(db).kind(db) == TypeVarKind::TypingSelf);
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
});
|
|
||||||
let implicit_annotation = if !method_has_self_in_generic_context
|
|
||||||
&& class.is_not_generic()
|
|
||||||
&& !class.known(db).is_some_and(KnownClass::is_fallback_class)
|
|
||||||
{
|
|
||||||
Type::instance(db, class)
|
|
||||||
} else {
|
|
||||||
let scope_id = definition.scope(db);
|
|
||||||
let typevar_binding_context = Some(definition);
|
|
||||||
let index = semantic_index(db, scope_id.file(db));
|
|
||||||
let class = nearest_enclosing_class(db, index, scope_id).unwrap();
|
|
||||||
Type::NonInferableTypeVar(
|
|
||||||
get_self_type(db, scope_id, typevar_binding_context, class).unwrap(),
|
|
||||||
)
|
|
||||||
.apply_type_mapping(
|
|
||||||
db,
|
|
||||||
&TypeMapping::MarkTypeVarsInferable(Some(BindingContext::Definition(
|
|
||||||
definition,
|
|
||||||
))),
|
|
||||||
)
|
|
||||||
};
|
|
||||||
Parameter {
|
|
||||||
annotated_type: Some(implicit_annotation),
|
|
||||||
inferred_annotation: true,
|
|
||||||
kind: ParameterKind::PositionalOrKeyword {
|
|
||||||
name: arg.parameter.name.id.clone(),
|
|
||||||
default_type: default_type(arg),
|
|
||||||
},
|
|
||||||
form: ParameterForm::Value,
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
Parameter::from_node_and_kind(
|
let scope_id = definition.scope(db);
|
||||||
db,
|
let typevar_binding_context = Some(definition);
|
||||||
definition,
|
let index = semantic_index(db, scope_id.file(db));
|
||||||
&arg.parameter,
|
let class = nearest_enclosing_class(db, index, scope_id).unwrap();
|
||||||
ParameterKind::PositionalOrKeyword {
|
Type::NonInferableTypeVar(
|
||||||
name: arg.parameter.name.id.clone(),
|
get_self_type(db, scope_id, typevar_binding_context, class).unwrap(),
|
||||||
default_type: default_type(arg),
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
|
.apply_type_mapping(
|
||||||
|
db,
|
||||||
|
&TypeMapping::MarkTypeVarsInferable(Some(BindingContext::Definition(
|
||||||
|
definition,
|
||||||
|
))),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
Parameter {
|
||||||
|
annotated_type: Some(implicit_annotation),
|
||||||
|
inferred_annotation: true,
|
||||||
|
kind: ParameterKind::PositionalOrKeyword {
|
||||||
|
name: arg.parameter.name.id.clone(),
|
||||||
|
default_type: default_type(arg),
|
||||||
|
},
|
||||||
|
form: ParameterForm::Value,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Parameter::from_node_and_kind(
|
Parameter::from_node_and_kind(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue