Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid warning when field type is any #101

Merged
merged 2 commits into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}