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

support a collection on parsable-type and acceptable-type #120

Merged
merged 2 commits into from
May 3, 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
8 changes: 6 additions & 2 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@
<vendor email="koaxudai@gmail.com">Koudai Aono @koxudaxi</vendor>
<change-notes><![CDATA[
<h2>version 0.1.5</h2>
<p>BugFixes<p>
<p>Features</p>
<ul>
<li>Support a collection on parsable-type and acceptable-type [#120] </li>
</ul>
<p>BugFixes</p>
<ul>
<li>Fix type-map edge case for parsable-type and acceptable-type [#118]</li>
</ul>
<h2>version 0.1.4</h2>
<p>BugFixes<p>
<p>BugFixes</p>
<ul>
<li>Fix type provider for dataclass [#114] </li>
</ul>
Expand Down
22 changes: 13 additions & 9 deletions src/com/koxudaxi/pydantic/PydanticTypeCheckerInspection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,26 @@ class PydanticTypeCheckerInspection : PyTypeCheckerInspection() {
}

private fun getTypeFromTypeMap(getTypeMap: (project: Project) -> (MutableMap<String, List<String>>), typeForParameter: PyType, cache: MutableMap<PyType, PyType?>): PyType? {
return when {
cache.containsKey(typeForParameter) -> {
cache[typeForParameter]
}
if (cache.containsKey(typeForParameter)) {
return cache[typeForParameter]
}

val newType = when (typeForParameter) {
is PyCollectionType ->
PyCollectionTypeImpl(typeForParameter.pyClass, typeForParameter.isDefinition, typeForParameter.elementTypes.mapNotNull {
getTypeFromTypeMap(getTypeMap, it, cache)
})
else -> {
val project = holder!!.project
val typeMap = getTypeMap(project)
val unionType = PyUnionType.union(getPyClassTypeByPyTypes(typeForParameter).toSet().flatMap { type ->
typeMap[type.classQName]?.mapNotNull {
PyUnionType.union(getPyClassTypeByPyTypes(typeForParameter).toSet().flatMap { type ->
getTypeMap(project)[type.classQName]?.mapNotNull {
createPyClassTypeImpl(it, project, myTypeEvalContext)
} as? List<PyType> ?: listOf()
})
cache[typeForParameter] = unionType
unionType
}
}
cache[typeForParameter] = newType
return newType
}

private fun analyzeCallee(callSite: PyCallSiteExpression, mapping: PyArgumentsMapping) {
Expand Down
16 changes: 16 additions & 0 deletions testData/typecheckerinspection/parsableTypeCollection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from builtins import *
from typing import Union, List

from pydantic import BaseModel


class A(BaseModel):
a: List[str]
b: List[List[str]]

a_int: List[int] = [int('123')]
a_str: List[str] = [str('123')]
b_int: List[List[int]] = [[int('123')]]
b_str: List[List[str]] = [[str('123')]]
A(a=a_str, b=b_str)
A(<warning descr="Field is of type 'List[str]', 'List[int]' may not be parsable to 'List[str]'">a=a_int</warning>, <warning descr="Field is of type 'List[List[str]]', 'List[List[int]]' may not be parsable to 'List[List[str]]'">b=b_int</warning>)
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ open class PydanticTypeCheckerInspectionTest : PydanticInspectionBase() {
pydanticConfigService.parsableTypeMap["builtins.str"] = arrayListOf("builtins.int")
doTest()
}
fun testParsableTypeCollection() {
val pydanticConfigService = PydanticConfigService.getInstance(myFixture!!.project)
pydanticConfigService.parsableTypeMap["builtins.str"] = arrayListOf("builtins.int")
doTest()
}
fun testParsableTypeWeakWarning() {
val pydanticConfigService = PydanticConfigService.getInstance(myFixture!!.project)
pydanticConfigService.parsableTypeMap["builtins.str"] = arrayListOf("builtins.int")
Expand Down