Skip to content

Commit 1f72165

Browse files
committed
Cover more PyTargetExpressions with hints
1 parent 86d587f commit 1f72165

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Changelog
22

33
## [Unreleased]
4+
### Added
5+
- More Python syntax covered with parameter hints
6+
47
### Changed
58
- Reduced the amount of parameter hints to make them more relevant and valuable, a new settings option included
69

src/main/kotlin/space/whitememory/pythoninlayparams/PythonInlayParameterHintsProvider.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,34 @@ class PythonInlayParameterHintsProvider : InlayParameterHintsProvider {
6161
return inlayInfos
6262
}
6363

64+
var useCallMethod = false
65+
if (resolved is PyTargetExpression) {
66+
// The target expression might include a lambda or class attribute
67+
val assignedValue = resolved.findAssignedValue() ?: return inlayInfos
68+
resolved = if (assignedValue is PyLambdaExpression && lambdaHints.isEnabled()) {
69+
assignedValue
70+
} else if (assignedValue is PyCallExpression) {
71+
// Potentially a class instance, very specific and requires more research
72+
useCallMethod = true
73+
assignedValue.callee?.reference?.resolve() ?: return inlayInfos
74+
} else {
75+
return inlayInfos
76+
}
77+
}
78+
6479
var classAttributes = listOf<PyTargetExpression>()
65-
if (resolved is PyTargetExpression && lambdaHints.isEnabled()) {
66-
// TODO: Handle cases other than lambda expressions
67-
// Use the target to find the lambda expression object, and assign it to get its parameters up ahead
68-
resolved = PsiTreeUtil.getNextSiblingOfType(resolved, PyLambdaExpression::class.java) ?: return inlayInfos
69-
} else if (resolved is PyClass && classHints.isEnabled()) {
70-
// This call is made by a class (initialization), so we want to find the parameters it takes.
80+
if (resolved is PyClass && classHints.isEnabled()) {
81+
// This call is made by a class (instantiation/__call__), so we want to find the parameters it takes.
7182
// In order to do so, we first have to check for an init method, and if not found,
7283
// We will use the class attributes instead. (Handle dataclasses, attrs, etc.)
7384
val evalContext = TypeEvalContext.codeAnalysis(element.project, element.containingFile)
74-
val entryMethod = resolved.findInitOrNew(true, evalContext)
85+
val entryMethod = if (useCallMethod) {
86+
// TODO: Find some API sugar to make it more reliable?
87+
resolved.findMethodByName("__call__", false, evalContext)
88+
?: resolved.findInitOrNew(true, evalContext)
89+
} else {
90+
resolved.findInitOrNew(true, evalContext)
91+
}
7592

7693
resolved = if (entryMethod != null && entryMethod.containingClass == resolved) {
7794
entryMethod

0 commit comments

Comments
 (0)