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

add migration guide url for 232 #752

Merged
merged 1 commit into from
Jul 17, 2023
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
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 @@ -25,4 +25,8 @@ open class PydanticInspectionV2Test : PydanticInspectionBase(version = "v2") {
fun testReadOnlyPropertyFrozenConfigDict() {
doTest()
}

fun testValidators() {
doTest()
}
}