Skip to content

Commit 099d990

Browse files
samwgoldmanfacebook-github-bot
authored andcommitted
Fix completions for super with multiple inheritance
Summary: Before this diff, we were not giving all the completions from ancestors with multiple inheritance. The fix is the same as D75792048. Reviewed By: stroxler Differential Revision: D80887987 fbshipit-source-id: 77e7f4d4cbc7fe6db5f835c39b6c86331f2e6a10
1 parent 39f5f24 commit 099d990

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

pyrefly/lib/alt/attr.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2135,6 +2135,22 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
21352135
self.completions_mro(mro, expected_attribute_name, res)
21362136
}
21372137

2138+
fn completions_super(
2139+
&self,
2140+
cls: &Class,
2141+
start_lookup_cls: &ClassType,
2142+
expected_attribute_name: Option<&Name>,
2143+
res: &mut Vec<AttrInfo>,
2144+
) {
2145+
let mro = self.get_mro_for_class(cls);
2146+
let mro = mro
2147+
.ancestors_no_object()
2148+
.iter()
2149+
.skip_while(|ancestor| *ancestor != start_lookup_cls)
2150+
.map(|x| x.class_object());
2151+
self.completions_mro(mro, expected_attribute_name, res)
2152+
}
2153+
21382154
fn completions_class_type(
21392155
&self,
21402156
cls: &ClassType,
@@ -2203,8 +2219,11 @@ impl<'a, Ans: LookupAnswer> AnswersSolver<'a, Ans> {
22032219
expected_attribute_name,
22042220
res,
22052221
),
2206-
AttributeBase::SuperInstance(class, _) => {
2207-
self.completions_class_type(class, expected_attribute_name, res)
2222+
AttributeBase::SuperInstance(start_lookup_cls, obj) => {
2223+
let cls = match obj {
2224+
SuperObj::Instance(c) | SuperObj::Class(c) => c.class_object(),
2225+
};
2226+
self.completions_super(cls, start_lookup_cls, expected_attribute_name, res);
22082227
}
22092228
AttributeBase::ClassObject(class) => {
22102229
self.completions_class(class.class_object(), expected_attribute_name, res)

pyrefly/lib/test/lsp/completion.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,36 @@ Completion Results:
10111011
);
10121012
}
10131013

1014+
#[test]
1015+
fn dot_complete_super() {
1016+
let code = r#"
1017+
class A:
1018+
x: int
1019+
1020+
class B:
1021+
y: str
1022+
1023+
class C(A, B):
1024+
def foo(self):
1025+
super().
1026+
# ^
1027+
"#;
1028+
let report =
1029+
get_batched_lsp_operations_report_allow_error(&[("main", code)], get_default_test_report());
1030+
assert_eq!(
1031+
r#"
1032+
# main.py
1033+
10 | super().
1034+
^
1035+
Completion Results:
1036+
- (Field) x: int
1037+
- (Field) y: str
1038+
"#
1039+
.trim(),
1040+
report.trim(),
1041+
);
1042+
}
1043+
10141044
#[test]
10151045
fn import_completions_on_builtins() {
10161046
let code = r#"

0 commit comments

Comments
 (0)