Skip to content

Commit

Permalink
Fix False positive for non-decorator use of validator (#818)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi committed Oct 12, 2023
1 parent 25cae75 commit 87c92a9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
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()
}
}

0 comments on commit 87c92a9

Please sign in to comment.