Skip to content

Commit

Permalink
add migration guide url (#750)
Browse files Browse the repository at this point in the history
  • Loading branch information
koxudaxi authored Jul 17, 2023
1 parent 1ed86aa commit 5f0ee31
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/com/koxudaxi/pydantic/PydanticInspection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ class PydanticInspection : PyInspection() {
inspectDefaultFactory(node)
}

override fun visitPyReferenceExpression(node: PyReferenceExpression) {
if(!pydanticCacheService.isV2) return
val pyFunction = node.reference.resolve() as? PyFunction ?: return

val qualifiedName = (pyFunction as? PyQualifiedNameOwner)?.qualifiedName ?: return
if (!qualifiedName.startsWith("pydantic.")) return
if (!isPydanticDeprecatedSince20(pyFunction)) return
registerProblem(
node.nameElement?.psi ?: node,
"<html><body>" +
"Pydantic V2 Migration Guide: " +
"<a href=\"https://docs.pydantic.dev/dev-v2/migration/\">" +
"https://docs.pydantic.dev/dev-v2/migration/" +
"</a>" +
"</body></html>",
ProblemHighlightType.LIKE_DEPRECATED
)

}
private fun isPydanticDeprecatedSince20(pyFunction: PyFunction): Boolean =
pyFunction.statementList.statements.filterIsInstance<PyExpressionStatement>()
.mapNotNull { (it.expression as? PyCallExpression)?.getArgument(1, PyReferenceExpression::class.java) }
.any { (it.reference.resolve() as? PyTargetExpression)?.findAssignedValue()?.name == "PydanticDeprecatedSince20" }


private fun inspectCustomRootFieldV2(pyClass: PyClass) {
if (getRootField(pyClass) == null) return
if (!isPydanticModel(pyClass, false, myTypeEvalContext)) return
Expand Down
26 changes: 26 additions & 0 deletions testData/inspectionv2/validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from pydantic import BaseModel, <warning descr="Pydantic V2 Migration Guide: https://docs.pydantic.dev/dev-v2/migration/">validator</warning>, <warning descr="Pydantic V2 Migration Guide: https://docs.pydantic.dev/dev-v2/migration/">root_validator</warning>

def check(func):
def inner():
func()
return inner

class A(BaseModel):
a: str


@<warning descr="Pydantic V2 Migration Guide: https://docs.pydantic.dev/dev-v2/migration/">validator</warning>('a')
def validate_a(cls):
pass

@<warning descr="Pydantic V2 Migration Guide: https://docs.pydantic.dev/dev-v2/migration/">root_validator</warning>()
def validate_root(cls):
pass


def dummy(self):
pass

@check
def task(self):
pass
1 change: 1 addition & 0 deletions testData/mock/pydanticv2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
from .types import *
from .config import ConfigDict
from .version import VERSION
from .deprecated import validator, root_validator

__version__ = VERSION
1 change: 1 addition & 0 deletions testData/mock/pydanticv2/deprecated/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from class_validators import validator, root_validator
39 changes: 39 additions & 0 deletions testData/mock/pydanticv2/deprecated/class_validators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from warnings import warn

from ..warnings import PydanticDeprecatedSince20

DeprecationWarning = PydanticDeprecatedSince20


def validator(
__field: str,
*fields: str,
pre: bool = False,
each_item: bool = False,
always: bool = False,
check_fields: bool | None = None,
allow_reuse: bool = False,
) -> Callable[[_V1ValidatorType], _V1ValidatorType]:

warn(
'Pydantic V1 style `@validator` validators are deprecated.'
' You should migrate to Pydantic V2 style `@field_validator` validators,'
' see the migration guide for more details',
DeprecationWarning,
stacklevel=2,
)


def root_validator(
*__args,
pre: bool = False,
skip_on_failure: bool = False,
allow_reuse: bool = False,
) -> Any:
warn(
'Pydantic V1 style `@root_validator` validators are deprecated.'
' You should migrate to Pydantic V2 style `@model_validator` validators,'
' see the migration guide for more details',
DeprecationWarning,
stacklevel=2,
)
4 changes: 4 additions & 0 deletions testSrc/com/koxudaxi/pydantic/PydanticInspectionV2Test.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ open class PydanticInspectionV2Test : PydanticInspectionBase(version = "v2") {
fun testReadOnlyPropertyFrozenConfigDict() {
doTest()
}

fun testValidators() {
doTest()
}
}

0 comments on commit 5f0ee31

Please sign in to comment.