[ty] Add support for inlay hints on attribute assignment (#20485)

This commit is contained in:
Matthew Mckee 2025-09-23 12:14:46 +01:00 committed by GitHub
parent ef4df34652
commit fd5c48c539
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -264,6 +264,15 @@ impl SourceOrderVisitor<'_> for InlayHintVisitor<'_, '_> {
} }
source_order::walk_expr(self, expr); source_order::walk_expr(self, expr);
} }
Expr::Attribute(attribute) => {
if self.in_assignment {
if attribute.ctx.is_store() {
let ty = expr.inferred_type(&self.model);
self.add_type_hint(expr.range().end(), ty);
}
}
source_order::walk_expr(self, expr);
}
Expr::Call(call) => { Expr::Call(call) => {
let argument_names = let argument_names =
inlay_hint_function_argument_details(self.db, &self.model, call) inlay_hint_function_argument_details(self.db, &self.model, call)
@ -436,6 +445,31 @@ mod tests {
"); ");
} }
#[test]
fn test_assign_attribute_of_instance() {
let test = inlay_hint_test(
"
class A:
def __init__(self, y):
self.x = 1
self.y = y
a = A(2)
a.y = 3
",
);
assert_snapshot!(test.inlay_hints(), @r"
class A:
def __init__(self, y):
self.x[: Literal[1]] = 1
self.y[: Unknown] = y
a[: A] = A([y=]2)
a.y[: Literal[3]] = 3
");
}
#[test] #[test]
fn test_disabled_variable_types() { fn test_disabled_variable_types() {
let test = inlay_hint_test("x = 1"); let test = inlay_hint_test("x = 1");