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 fisrt parameter type of a validator method #76

Merged
merged 1 commit into from
Sep 24, 2019
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 @@ -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