diff --git a/src/com/koxudaxi/pydantic/PydanticInspection.kt b/src/com/koxudaxi/pydantic/PydanticInspection.kt index 5e25f5da..22840cbd 100644 --- a/src/com/koxudaxi/pydantic/PydanticInspection.kt +++ b/src/com/koxudaxi/pydantic/PydanticInspection.kt @@ -6,6 +6,7 @@ import com.intellij.codeInspection.ProblemsHolder import com.intellij.psi.PsiElementVisitor import com.jetbrains.python.PyNames import com.jetbrains.python.codeInsight.stdlib.PyDataclassTypeProvider +import com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider import com.jetbrains.python.inspections.PyInspection import com.jetbrains.python.inspections.PyInspectionVisitor import com.jetbrains.python.inspections.quickfix.RenameParameterQuickFix @@ -311,7 +312,9 @@ class PydanticInspection : PyInspection() { private fun inspectCustomRootField(node: PyAssignmentStatement) { val pyClass = getPydanticModelByAttribute(node, false, myTypeEvalContext) ?: return - val fieldName = (node.leftHandSideExpression as? PyTargetExpressionImpl)?.text ?: return + val field = node.leftHandSideExpression as? PyTargetExpression ?: return + if (PyTypingTypeProvider.isClassVar(field, myTypeEvalContext)) return + val fieldName = field.text ?: return if (fieldName.startsWith('_')) return val rootModel = getRootField(pyClass)?.containingClass ?: return if (!isPydanticModel(rootModel, false, myTypeEvalContext)) return diff --git a/testData/inspection/customRoot.py b/testData/inspection/customRoot.py index f0f2dba3..b07195aa 100644 --- a/testData/inspection/customRoot.py +++ b/testData/inspection/customRoot.py @@ -1,3 +1,5 @@ +from typing import ClassVar + from pydantic import BaseModel @@ -27,3 +29,7 @@ def f(): __root__ = 'xyz' g = 'xyz' +class G(BaseModel): + ATTRIBUTE_NAME: ClassVar[str] = "testing" + __root__ = 'xyz' +