Skip to content

Commit ced78a9

Browse files
committed
Fix return hints with incomplete defs
1 parent e8deafd commit ced78a9

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

src/main/kotlin/space/whitememory/pythoninlayparams/types/functions/PythonFunctionInlayTypeHintsCollector.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package space.whitememory.pythoninlayparams.types.functions
33
import com.intellij.codeInsight.hints.InlayHintsSink
44
import com.intellij.openapi.editor.Editor
55
import com.intellij.psi.PsiElement
6+
import com.intellij.psi.tree.TokenSet
67
import com.intellij.psi.util.PsiTreeUtil
78
import com.intellij.refactoring.suggested.endOffset
9+
import com.jetbrains.python.PyTokenTypes
810
import com.jetbrains.python.psi.PyElement
911
import com.jetbrains.python.psi.PyFunction
1012
import com.jetbrains.python.psi.PyParameterList
@@ -18,7 +20,13 @@ class PythonFunctionInlayTypeHintsCollector(editor: Editor, settings: Any) :
1820
override val textBeforeTypeHint = "->"
1921

2022
override fun validateExpression(element: PsiElement): Boolean {
21-
return element is PyFunction
23+
// Not a function or has only def keyword
24+
if (element !is PyFunction || element.nameNode == null) {
25+
return false
26+
}
27+
28+
val colonToken = TokenSet.create(PyTokenTypes.COLON)
29+
return element.node.getChildren(colonToken).isNotEmpty()
2230
}
2331

2432
override fun collect(element: PsiElement, editor: Editor, sink: InlayHintsSink): Boolean {

src/main/kotlin/space/whitememory/pythoninlayparams/types/hints/HintResolver.kt

+14-5
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ enum class HintResolver {
132132
},
133133

134134
CLASS_ATTRIBUTE_HINT {
135-
override fun isApplicable(settings: PythonVariablesInlayTypeHintsProvider.Settings): Boolean = !settings.showClassAttributeHints
135+
override fun isApplicable(settings: PythonVariablesInlayTypeHintsProvider.Settings): Boolean =
136+
!settings.showClassAttributeHints
136137

137138
override fun shouldShowTypeHint(
138139
element: PyTargetExpression,
@@ -321,7 +322,9 @@ enum class HintResolver {
321322
if (isLiteralExpression(assignedValue)) {
322323
if ((typeAnnotation is PyTypedDictType || (typeAnnotation is PyClassType && typeAnnotation.pyClass.qualifiedName == PyNames.DICT)) && assignedValue is PySequenceExpression) {
323324
// Handle case when dict contains all literal expressions
324-
if (assignedValue.elements.isNotEmpty() && assignedValue.elements.all { it is PyKeyValueExpression && isLiteralExpression(it.value) }) {
325+
if (assignedValue.elements.isNotEmpty() && assignedValue.elements.all {
326+
it is PyKeyValueExpression && isLiteralExpression(it.value)
327+
}) {
325328
return false
326329
}
327330

@@ -408,7 +411,12 @@ enum class HintResolver {
408411

409412
companion object {
410413
val builtinMethods = setOf("globals", "locals")
411-
fun resolve(element: PyTargetExpression, typeEvalContext: TypeEvalContext, settings: PythonVariablesInlayTypeHintsProvider.Settings): Boolean {
414+
415+
fun resolve(
416+
element: PyTargetExpression,
417+
typeEvalContext: TypeEvalContext,
418+
settings: PythonVariablesInlayTypeHintsProvider.Settings
419+
): Boolean {
412420
val typeAnnotation = getExpressionAnnotationType(element, typeEvalContext)
413421

414422
return resolveEnabled(settings).any {
@@ -454,13 +462,14 @@ enum class HintResolver {
454462
|| (element is PyFunction && typeAnnotation is PyNoneType)
455463
|| ((element is PyFunction || element is PyTargetExpression) && (element as PyTypeCommentOwner).typeCommentAnnotation != null)
456464
|| (element is PyAnnotationOwner && element.annotation != null)
457-
|| (element is PyFunction && !element.textContains(":".single()))
458465
) {
459466
return false
460467
}
461468

462469
if (typeAnnotation is PyUnionType) {
463-
return !typeAnnotation.members.all { PyTypeChecker.isUnknown(it, false, typeEvalContext) || (it is PyNoneType || it == null) }
470+
return !typeAnnotation.members.all {
471+
PyTypeChecker.isUnknown(it, false, typeEvalContext) || (it is PyNoneType || it == null)
472+
}
464473
}
465474

466475
if (PyTypeChecker.isUnknown(typeAnnotation, false, typeEvalContext)) {

0 commit comments

Comments
 (0)