Skip to content

Commit

Permalink
Fix fisrt parameter type of a validator method (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi committed Sep 24, 2019
1 parent 62900a4 commit 5e89bfd
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
1 change: 1 addition & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<h2>version 0.0.22</h2>
<p>Features, BugFixes</p>
<ul>
<li>Fix first parameter type of a validator method [#76] </li>
<li>Fix auto-completion for Fields [#75] </li>
<li>Improve to insert validate methods [#74] </li>
</ul>
Expand Down
19 changes: 13 additions & 6 deletions src/com/koxudaxi/pydantic/PydanticTypeProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,20 @@ class PydanticTypeProvider : PyTypeProviderBase() {
}

override fun getParameterType(param: PyNamedParameter, func: PyFunction, context: TypeEvalContext): Ref<PyType>? {
if (!param.isPositionalContainer && !param.isKeywordContainer && param.annotationValue == null && func.name == "__init__") {
val pyClass = func.containingClass ?: return null
if (!isPydanticModel(pyClass, context)) return null
val name = param.name ?: return null
getRefTypeFromFieldName(name, context, pyClass)?.let { return it }
return when {
!param.isPositionalContainer && !param.isKeywordContainer && param.annotationValue == null && func.name == "__init__" -> {
val pyClass = func.containingClass ?: return null
if (!isPydanticModel(pyClass, context)) return null
val name = param.name ?: return null
getRefTypeFromFieldName(name, context, pyClass)?.let { it }
}
param.isSelf && isValidatorMethod(func) -> {
val pyClass = func.containingClass ?: return null
if (!isPydanticModel(pyClass, context)) return null
Ref.create(context.getType(pyClass))
}
else -> null
}
return null
}

private fun getRefTypeFromFieldName(name: String, context: TypeEvalContext, pyClass: PyClass): Ref<PyType>? {
Expand Down
13 changes: 13 additions & 0 deletions testData/completion/classValidatorCls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
,from builtins import *

from pydantic import BaseModel, validator


class A(BaseModel):
abc: str

@validator('abc')
def test(cls):
return cls.<caret>


8 changes: 8 additions & 0 deletions testSrc/com/koxudaxi/pydantic/PydanticCompletionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,14 @@ open class PydanticCompletionTest : PydanticTestCase() {
)
}

fun testClassValidatorCls() {
doFieldTest(
listOf(
Pair("___slots__", "BaseModel")
)
)
}

fun testMethodSelf() {
doFieldTest(
listOf(
Expand Down

0 comments on commit 5e89bfd

Please sign in to comment.