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 False positive for non-decorator use of validator #818

Merged
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
26 changes: 21 additions & 5 deletions src/com/koxudaxi/pydantic/PydanticReferenceContributor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class PydanticReferenceContributor : PsiReferenceContributor() {
if (element !is PyStringLiteralExpression) {
return false
}
return isValidatorField(element, TypeEvalContext.userInitiated(element.project, element.containingFile))
return isValidatorField(
element,
TypeEvalContext.userInitiated(element.project, element.containingFile)
)
}
}
}
Expand All @@ -41,16 +44,29 @@ class PydanticReferenceContributor : PsiReferenceContributor() {
provider: PsiReferenceProvider
) {
registrar.registerReferenceProvider(
PlatformPatterns.psiElement(
PyStringLiteralExpression::class.java
).withParents(
PlatformPatterns.or(
PlatformPatterns.psiElement(
PyStringLiteralExpression::class.java
).withParents(
PyArgumentList::class.java,
PyCallExpression::class.java,
PyDecorator::class.java,
PyDecoratorList::class.java,
PyFunction::class.java,
PyStatementList::class.java,
PyClass::class.java).with(Holder.VALIDATOR_FIELD), provider
PyClass::class.java
).with(Holder.VALIDATOR_FIELD),
PlatformPatterns.psiElement(
PyStringLiteralExpression::class.java
).withParents(
PyArgumentList::class.java,
PyCallExpression::class.java,
PyCallExpression::class.java,
PyAssignmentStatement::class.java,
PyStatementList::class.java,
PyClass::class.java
).with(Holder.VALIDATOR_FIELD)
), provider
)
}
}
Expand Down
21 changes: 21 additions & 0 deletions testData/inspectionv18/validatorField.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pydantic import BaseModel, validator


class A(BaseModel):
foo: int
bar: float

_validate_stuff = validator(
"bar",
allow_reuse=True,
)(do_stuff)


class B(BaseModel):
foo: int
bar: float

_validate_stuff = validator(
<error descr="Cannot find field 'abc'">"abc"</error>,
allow_reuse=True,
)(do_stuff)
4 changes: 4 additions & 0 deletions testSrc/com/koxudaxi/pydantic/PydanticInspectionV18Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ open class PydanticInspectionV18Test : PydanticInspectionBase(version = "v18") {
fun testDefaultFactory() {
doTest()
}

fun testValidatorField() {
doTest()
}
}