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 "__root__ cannot be mixed with other fields" -- ty… #780

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
5 changes: 4 additions & 1 deletion src/com/koxudaxi/pydantic/PydanticInspection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions testData/inspection/customRoot.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import ClassVar

from pydantic import BaseModel


Expand Down Expand Up @@ -27,3 +29,7 @@ def f():
__root__ = 'xyz'
g = 'xyz'

class G(BaseModel):
ATTRIBUTE_NAME: ClassVar[str] = "testing"
__root__ = 'xyz'

Loading