Skip to content
Open
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
43 changes: 42 additions & 1 deletion pyrefly/lib/alt/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3638,7 +3638,7 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
) -> Vec<AttrInfo> {
let annotated_fields = self.django_annotated_fields(&base);
let mut res = Vec::new();
if let Some(base) = self.as_attribute_base(base) {
if let Some(base) = self.as_attribute_base(base.clone()) {
self.completions_inner(
base,
expected_attribute_name,
Expand All @@ -3647,6 +3647,47 @@ impl<'ctx, 'answer, Ans: LookupAnswer> AnswersSolver<'ctx, 'answer, Ans> {
&mut res,
);
}
if expected_attribute_name.is_none()
&& let Some(getattr) = self.type_of_magic_dunder_attr(
&base,
&dunder::GETATTR,
TextRange::default(),
&self.error_swallower(),
None,
"completion_getattr",
false,
)
{
let param_index = usize::from(matches!(&getattr, Type::BoundMethod(_)));
for (callable, _) in getattr.toplevel_callable_signatures() {
let Some(param) = callable.get_positional_param(param_index) else {
continue;
};
let members = match param {
Type::Union(union) => union.members.as_slice(),
_ => std::slice::from_ref(param),
};
for member in members {
let Type::Literal(lit) = member else {
continue;
};
let Lit::Str(value) = &lit.value else {
continue;
};
let name = Name::new(value);
if !res.iter().any(|info| info.name == name) {
res.push(AttrInfo {
name,
ty: include_types.then(|| callable.ret.clone()),
is_deprecated: false,
definition: AttrDefinition::Synthetic,
is_reexport: false,
is_from_object: false,
});
}
}
}
}
for (name, ty) in annotated_fields {
if expected_attribute_name.is_none_or(|expected| expected == &name)
&& !res.iter().any(|info| info.name == name)
Expand Down
17 changes: 17 additions & 0 deletions pyrefly/lib/test/lsp/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,23 @@ Completion Results:
);
}

#[test]
fn dot_complete_from_getattr_literal_parameter() {
let code = r#"
from typing import Literal

class A:
def __getattr__(self, item: Literal["A", "B"]) -> object: ...

A().
# ^
"#;
let report =
get_batched_lsp_operations_report_allow_error(&[("main", code)], get_default_test_report());
assert!(report.contains("- (Field) A"), "{report}");
assert!(report.contains("- (Field) B"), "{report}");
}

#[test]
fn dict_key_completion_from_literal() {
let code = r#"
Expand Down
Loading