Skip to content

Commit d213dcc

Browse files
committed
Method 2: alter in signatures method
1 parent 419d085 commit d213dcc

File tree

2 files changed

+36
-27
lines changed

2 files changed

+36
-27
lines changed

crates/ty_python_semantic/src/types.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3456,7 +3456,14 @@ impl<'db> Type<'db> {
34563456
let signature = bound_method.function(db).signature(db);
34573457
Signatures::single(match signature {
34583458
FunctionSignature::Single(signature) => {
3459-
CallableSignature::single(self, signature.clone())
3459+
let mut params = signature.parameters().clone();
3460+
params = params.set_first_type(
3461+
Self::KnownInstance(KnownInstanceType::TypingSelf)
3462+
.in_type_expression(db, bound_method.function(db).body_scope(db))
3463+
.unwrap(),
3464+
);
3465+
let new_sig = Signature::new(params, signature.return_ty.clone());
3466+
CallableSignature::single(self, new_sig)
34603467
.with_bound_type(bound_method.self_instance(db))
34613468
}
34623469
FunctionSignature::Overloaded(signatures, _) => {

crates/ty_python_semantic/src/types/signatures.rs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,24 @@ impl<'db> Parameters<'db> {
935935
}
936936
}
937937

938+
pub(crate) fn set_first_type(&mut self, ty: Type<'db>) -> Self {
939+
Self {
940+
value: self
941+
.value
942+
.iter()
943+
.enumerate()
944+
.map(|(i, param)| {
945+
if i == 0 {
946+
param.clone().with_annotated_type(ty)
947+
} else {
948+
param.clone()
949+
}
950+
})
951+
.collect(),
952+
is_gradual: self.is_gradual,
953+
}
954+
}
955+
938956
pub(crate) fn as_slice(&self) -> &[Parameter<'db>] {
939957
self.value.as_slice()
940958
}
@@ -1032,32 +1050,16 @@ impl<'db> Parameters<'db> {
10321050
},
10331051
)
10341052
});
1035-
let positional_or_keyword = args.iter().enumerate().map(|(index, arg)| {
1036-
// TODO: Check for classmethod decorator
1037-
if index == 0 && arg.name() == "self" {
1038-
Parameter {
1039-
annotated_type: Some(
1040-
Type::KnownInstance(KnownInstanceType::TypingSelf)
1041-
.in_type_expression(db, definition.scope(db))
1042-
.unwrap(),
1043-
),
1044-
kind: ParameterKind::PositionalOrKeyword {
1045-
name: arg.parameter.name.id.clone(),
1046-
default_type: default_type(arg),
1047-
},
1048-
form: ParameterForm::Value,
1049-
}
1050-
} else {
1051-
Parameter::from_node_and_kind(
1052-
db,
1053-
definition,
1054-
&arg.parameter,
1055-
ParameterKind::PositionalOrKeyword {
1056-
name: arg.parameter.name.id.clone(),
1057-
default_type: default_type(arg),
1058-
},
1059-
)
1060-
}
1053+
let positional_or_keyword = args.iter().enumerate().map(|(_, arg)| {
1054+
Parameter::from_node_and_kind(
1055+
db,
1056+
definition,
1057+
&arg.parameter,
1058+
ParameterKind::PositionalOrKeyword {
1059+
name: arg.parameter.name.id.clone(),
1060+
default_type: default_type(arg),
1061+
},
1062+
)
10611063
});
10621064
let variadic = vararg.as_ref().map(|arg| {
10631065
Parameter::from_node_and_kind(

0 commit comments

Comments
 (0)