mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
Highlight the case when default and default_factory are specified (PY-27398)
This commit is contained in:
@@ -16,6 +16,7 @@ import com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider
|
||||
import com.jetbrains.python.psi.*
|
||||
import com.jetbrains.python.psi.impl.PyBuiltinCache
|
||||
import com.jetbrains.python.psi.impl.PyCallExpressionHelper
|
||||
import com.jetbrains.python.psi.impl.stubs.PyDataclassFieldStubImpl
|
||||
import com.jetbrains.python.psi.resolve.PyResolveContext
|
||||
import com.jetbrains.python.psi.types.*
|
||||
|
||||
@@ -55,8 +56,9 @@ class PyDataclassInspection : PyInspection() {
|
||||
|
||||
node.processClassLevelDeclarations { element, _ ->
|
||||
if (element is PyTargetExpression && !PyTypingTypeProvider.isClassVar(element, myTypeEvalContext)) {
|
||||
processDefaultFieldValue(element)
|
||||
processAsInitVar(element, postInit)?.let { initVars.add(it) }
|
||||
processDefaultFieldValue(element)
|
||||
processAsInitVar(element, postInit)?.let { initVars.add(it) }
|
||||
processFieldFunctionCall(element)
|
||||
}
|
||||
|
||||
true
|
||||
@@ -189,6 +191,15 @@ class PyDataclassInspection : PyInspection() {
|
||||
return null
|
||||
}
|
||||
|
||||
private fun processFieldFunctionCall(field: PyTargetExpression) {
|
||||
val fieldStub = PyDataclassFieldStubImpl.create(field)
|
||||
if (fieldStub != null && fieldStub.hasDefault() && fieldStub.hasDefaultFactory()) {
|
||||
val call = field.findAssignedValue() as? PyCallExpression ?: return
|
||||
|
||||
registerProblem(call.argumentList, "cannot specify both default and default_factory", ProblemHighlightType.GENERIC_ERROR)
|
||||
}
|
||||
}
|
||||
|
||||
private fun processPostInitDefinition(postInit: PyFunction,
|
||||
dataclassParameters: DataclassParameters,
|
||||
initVars: List<PyTargetExpression>) {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
import dataclasses
|
||||
|
||||
@dataclasses.dataclass
|
||||
class E1:
|
||||
a: int = dataclasses.field(default=1)
|
||||
b: int = dataclasses.field(default_factory=int)
|
||||
c: int = dataclasses.field<error descr="cannot specify both default and default_factory">(default=1, default_factory=int)</error>
|
||||
@@ -71,6 +71,11 @@ public class PyDataclassInspectionTest extends PyInspectionTestCase {
|
||||
doTest();
|
||||
}
|
||||
|
||||
// PY-27398
|
||||
public void testFieldDefaultAndDefaultFactory() {
|
||||
doTest();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doTest() {
|
||||
runWithLanguageLevel(
|
||||
|
||||
Reference in New Issue
Block a user