Skip to content

Commit 1db351c

Browse files
Fix unsafe cast causing issues with set[] (#31)
* Fix unsafe cast causing issues with set[] * Update CHANGELOG.md * Fix None hint irregularities with sets
1 parent 2cdcd67 commit 1db351c

File tree

4 files changed

+18
-4
lines changed

4 files changed

+18
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
### Fixed
88
- Incorrect `Literal` behavior with type hints
9+
- `set[]` type hint were causing exceptions when declared explicitly
910

1011
## [0.3.1] - 2022-10-02
1112
### Fixed

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

-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ package space.whitememory.pythoninlayparams.types.hints
44

55
import com.intellij.psi.PsiElement
66
import com.jetbrains.python.PyNames
7-
import com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider
87
import com.jetbrains.python.psi.PyElement
9-
import com.jetbrains.python.psi.PyFunction
108
import com.jetbrains.python.psi.PyLambdaExpression
119
import com.jetbrains.python.psi.types.*
1210

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -264,14 +264,16 @@ enum class HintResolver {
264264

265265
if (assignmentValue !is PyCallExpression) return true
266266

267+
if (typeAnnotation is PyNoneType) return true
268+
267269
val resolvedClass = PyCallExpressionHelper.resolveCalleeClass(assignmentValue) ?: return true
268270

269271
if (!collectionNames.contains(resolvedClass.name)) {
270272
return resolvedClass.name != typeAnnotation?.name
271273
}
272274

273-
return typeAnnotation?.isBuiltin == true
274-
&& (typeAnnotation as PyCollectionType).elementTypes.filterNotNull().isNotEmpty()
275+
val collectionType = (typeAnnotation as? PyCollectionType) ?: return false
276+
return collectionType.isBuiltin && collectionType.elementTypes.filterNotNull().isNotEmpty()
275277
}
276278
},
277279

src/test/kotlin/space/whitememory/pythoninlayparams/PythonVariableTypesTest.kt

+13
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,19 @@ class PythonVariableTypesTest : PythonAbstractInlayHintsTestCase() {
183183
""".trimIndent()
184184
)
185185

186+
fun testIterableHints() = doTest(
187+
"""
188+
item_set<# [: [set [ str ]]] #> = set()
189+
x<# [: None] #> = item_set.add("1")
190+
191+
explicit_item_set: set[str] = set()
192+
y<# [: None] #> = explicit_item_set.add("1")
193+
194+
unclear_item_set: set = set()
195+
z<# [: None] #> = unclear_item_set.add("1")
196+
""".trimIndent()
197+
)
198+
186199
private fun doTest(text: String) {
187200
testProvider(
188201
"foo.py",

0 commit comments

Comments
 (0)