Skip to content

Commit fd5c48c

Browse files
[ty] Add support for inlay hints on attribute assignment (#20485)
1 parent ef4df34 commit fd5c48c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

crates/ty_ide/src/inlay_hints.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,15 @@ impl SourceOrderVisitor<'_> for InlayHintVisitor<'_, '_> {
264264
}
265265
source_order::walk_expr(self, expr);
266266
}
267+
Expr::Attribute(attribute) => {
268+
if self.in_assignment {
269+
if attribute.ctx.is_store() {
270+
let ty = expr.inferred_type(&self.model);
271+
self.add_type_hint(expr.range().end(), ty);
272+
}
273+
}
274+
source_order::walk_expr(self, expr);
275+
}
267276
Expr::Call(call) => {
268277
let argument_names =
269278
inlay_hint_function_argument_details(self.db, &self.model, call)
@@ -436,6 +445,31 @@ mod tests {
436445
");
437446
}
438447

448+
#[test]
449+
fn test_assign_attribute_of_instance() {
450+
let test = inlay_hint_test(
451+
"
452+
class A:
453+
def __init__(self, y):
454+
self.x = 1
455+
self.y = y
456+
457+
a = A(2)
458+
a.y = 3
459+
",
460+
);
461+
462+
assert_snapshot!(test.inlay_hints(), @r"
463+
class A:
464+
def __init__(self, y):
465+
self.x[: Literal[1]] = 1
466+
self.y[: Unknown] = y
467+
468+
a[: A] = A([y=]2)
469+
a.y[: Literal[3]] = 3
470+
");
471+
}
472+
439473
#[test]
440474
fn test_disabled_variable_types() {
441475
let test = inlay_hint_test("x = 1");

0 commit comments

Comments
 (0)