Skip to content

Commit

Permalink
Fix invalid warning when field type is any (#101)
Browse files Browse the repository at this point in the history
* fix invalid warning when field type is any

* update history
  • Loading branch information
koxudaxi authored Apr 18, 2020
1 parent bc0b86a commit 431c230
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<p>BugFixes</p>
<ul>
<li>Fix plugin build settings [#100] </li>
<li>Fix an invalid warning when a field type is any [#101] </li>
</ul>
<h2>version 0.1.1</h2>
<p>Features</p>
Expand Down
38 changes: 23 additions & 15 deletions src/com/koxudaxi/pydantic/PydanticTypeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import com.intellij.openapi.util.Ref
import com.intellij.psi.PsiElement
import com.intellij.psi.util.PsiTreeUtil
import com.jetbrains.python.psi.*
import com.jetbrains.python.psi.impl.PyCallExpressionImpl
import com.jetbrains.python.psi.impl.PyCallExpressionNavigator
import com.jetbrains.python.psi.impl.PySubscriptionExpressionImpl
import com.jetbrains.python.psi.impl.*
import com.jetbrains.python.psi.types.*
import com.koxudaxi.pydantic.PydanticConfigService.Companion.getInstance
import one.util.streamex.StreamEx
Expand Down Expand Up @@ -177,19 +175,29 @@ class PydanticTypeProvider : PyTypeProviderBase() {

when (val value = field.findAssignedValue()) {
null -> {
val annotation = (field.annotation?.value as? PySubscriptionExpressionImpl) ?: return null

when {
annotation.qualifier == null -> return value
annotation.qualifier!!.text == "Optional" -> return ellipsis
annotation.qualifier!!.text == "Union" -> annotation.children
.filterIsInstance<PyTupleExpression>()
.forEach {
it.children
.forEach { type -> if (type is PyNoneLiteralExpression) return ellipsis }
}
when (val annotation = field.annotation?.value) {
is PySubscriptionExpressionImpl -> {
when {
annotation.qualifier == null -> return value
annotation.qualifier!!.text == "Optional" -> return ellipsis
annotation.qualifier!!.text == "Union" -> annotation.children
.filterIsInstance<PyTupleExpression>()
.forEach {
it.children
.forEach { type -> if (type is PyNoneLiteralExpression) return ellipsis }
}
}
return value
}
is PyReferenceExpressionImpl -> {
return if (annotation.text == "Any") {
ellipsis
} else null
}
else -> {
return null
}
}
return value
}
else -> return getDefaultValueByAssignedValue(field, ellipsis, context, pydanticVersion)
}
Expand Down
3 changes: 3 additions & 0 deletions testData/mock/stub/typing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Optional:
def __getitem__(cls, item):
pass

class Any:
pass


class Type:
@classmethod
Expand Down
17 changes: 17 additions & 0 deletions testData/typecheckerinspection/field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from builtins import *

from typing import *

from pydantic import BaseModel


class A(BaseModel):
a: int
b: Any
c: Optional[int]
d: Union[str, int, None]


A(a=int(123))
A(a=int(123), b=456, c=789, d=345)
A(<warning descr="Expected type 'int', got 'str' instead">a=str(123)</warning>, b=456, <warning descr="Expected type 'Optional[int]', got 'str' instead">c=str(789)</warning>, <warning descr="Expected type 'Union[str, int, None]', got 'bytes' instead">d=bytes(234)</warning>)
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ open class PydanticTypeCheckerInspectionTest : PydanticInspectionBase() {
pydanticConfigService.parsableTypeMap["builtins.str"] = arrayListOf("int")
doTest()
}

fun testField() {
doTest()
}
}

0 comments on commit 431c230

Please sign in to comment.