mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 06:11:21 +00:00
[ty] Add support for inlay hints on attribute assignment (#20485)
This commit is contained in:
parent
ef4df34652
commit
fd5c48c539
1 changed files with 34 additions and 0 deletions
|
@ -264,6 +264,15 @@ impl SourceOrderVisitor<'_> for InlayHintVisitor<'_, '_> {
|
|||
}
|
||||
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) => {
|
||||
let argument_names =
|
||||
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]
|
||||
fn test_disabled_variable_types() {
|
||||
let test = inlay_hint_test("x = 1");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue