[ty] Add inlay hints for call arguments#19269
Conversation
|
Will mark this as ready for review for some initial feedback |
|
crates/ty_ide/src/inlay_hints.rs
Outdated
| let call_ty = call.func.inferred_type(&self.model).into_callable(self.db); | ||
| if let Some(Type::Callable(callable)) = call_ty { | ||
| let first_signature = callable.signatures(self.db).into_iter().next().cloned(); | ||
| if let Some(signature) = first_signature { |
There was a problem hiding this comment.
I think we need to match the signature here to resolve the best matching overload or it will show incorrect results for overloads
There was a problem hiding this comment.
Yeah that makes sense, I just took the first one for initial demonstration. I'll look into to how to do that
There was a problem hiding this comment.
It could make sense to take a look at the signature_help implementation which does something very similar.
There was a problem hiding this comment.
Okay thank you
There was a problem hiding this comment.
This works well, but it also provides inlay hints for positional only, variadic and keyword_variadic, which i don't think we want
There was a problem hiding this comment.
Fixed that now
| } | ||
| Type::ClassLiteral(class_literal) => ClassType::NonGeneric(class_literal) | ||
| .into_callable(db) | ||
| .into_callable(db), |
There was a problem hiding this comment.
Huh, why do we need to call into_callable twice. This looks fairly odd
There was a problem hiding this comment.
ClassLiteral.into_callable can return bound methods, I agree it looks weird, maybe I should change the class literal function instead to convert them
There was a problem hiding this comment.
I have added more into_callable calls in ClassLiteral.into_callable
|
This doesn't seem to work well with overloads. Seems to be because of one of the internal functions from bindings maybe |
|
@MichaReiser what are you're thoughts on this PR, what more would you like to see? Also, is this something we really want? |
For reference, this is what Python extension provides: // Enable/disable inlay hints for call argument names:
// ```python
// datetime('year='2019, 'month='10, 'day='27)
// ```
//
// - off: Disable inlay hints for call argument names.
// - partial: Enable inlay hints for positional-or-keyword arguments while ignoring positional-only and keyword-only.
// - all: Enable inlay hints for positional-or-keyword and positional-only arguments while ignoring keyword-only.
"python.analysis.inlayHints.callArgumentNames": "off",For now, we can just start with a boolean |
|
Cool, thanks. To be honest I didn't know the python extension provided this |
|
Should we start with Boolean for all inlay hints or for both call arguments and assignments |
I suggest following pylance's approach with a separate boolean option for each (we can change it to a non boolean option in the future)
Were you able to find out how pylance handle call argument names (e.g. how they're formatted?) |
|
They do it like it is done in this PR. They also have some guard that prevents argument names prefixed with "_" being displayed, that seems beneficial to add here |
|
Just checking in (no pressure). Is this waiting on any input from my end? |
|
Sorry, no not really, I just haven't got to doing what I said was beneficial. I'll get that done, then I think I'm happy with this. |
|
Okay, no worries. I just wanted to make sure it isn't blocked on me. |
|
Okay, that's all the changes I think i need to make |
|
Again, this is great. Thank you. I'm not yet sure how many inlay hints we want to enable in the playground but that's something we can easily tune |
* main: Don't cache files with diagnostics (#19869) [ty] support recursive type aliases (#19805) [ty] Remove unsafe `salsa::Update` implementations in `tuple.rs` (#19880) [ty] Function argument inlay hints (#19269) [ty] Remove Salsa interning for `TypedDictType` (#19879) Fix `lint.future-annotations` link (#19876) [ty] Reduce memory usage of `TupleSpec` and `TupleType` (#19872) [ty] Track heap usage of salsa structs (#19790) Update salsa to pull in tracked struct changes (#19843) [ty] simplify CycleDetector::visit signature (#19873) [ty] use interior mutability in type visitors (#19871) [ty] Fix tool name is None when no ty path is given in ty_benchmark (#19870) [ty] Remove `Type::Tuple` (#19669) [ty] Short circuit `ReachabilityConstraints::analyze_single` for dynamic types (#19867)
…aints * dcreager/inferrable: (65 commits) this was right after all mark typevars inferrable as we go fix tests fix inference of constrained typevars [ty] Add precise inference for indexing, slicing and unpacking `NamedTuple` instances (#19560) Add rule code to GitLab description (#19896) [ty] render docstrings in hover (#19882) [ty] simplify return type of place_from_declarations (#19884) [ty] Various minor cleanups to tuple internals (#19891) [ty] Improve `sys.version_info` special casing (#19894) Don't cache files with diagnostics (#19869) [ty] support recursive type aliases (#19805) [ty] Remove unsafe `salsa::Update` implementations in `tuple.rs` (#19880) [ty] Function argument inlay hints (#19269) [ty] Remove Salsa interning for `TypedDictType` (#19879) Fix `lint.future-annotations` link (#19876) [ty] Reduce memory usage of `TupleSpec` and `TupleType` (#19872) [ty] Track heap usage of salsa structs (#19790) Update salsa to pull in tracked struct changes (#19843) [ty] simplify CycleDetector::visit signature (#19873) ...
## Summary Related to astral-sh/ruff#19269, this PR defines the `ty.inlayHints.callArgumentNames` setting in the VS Code extension.
Summary
Add function argument inlay hints.
Note that the inlay hints here have the structure
{name}=instead of{name}:for rust. I think this makes more sense for python.As you can see I have made lots of visibility modifiers public. I don't want to do this, but I have for an initial showcase of this feature.
If anyone can advise on a better solution (maybe some function inside the
ty_python_semanticcrate) that would be greatly appreciated.Test Plan
Add tests to
ty_ide/src/inlay_hints.rs.