Support attr.fields_dict in inspection (PY-26354)

This commit is contained in:
Semyon Proshev
2018-05-29 16:50:03 +03:00
parent ace12e93d4
commit 95fabba2a8
2 changed files with 17 additions and 3 deletions
@@ -26,8 +26,12 @@ class PyDataclassInspection : PyInspection() {
companion object {
private val ORDER_OPERATORS = setOf("__lt__", "__le__", "__gt__", "__ge__")
private val DATACLASSES_HELPERS = setOf("dataclasses.fields", "dataclasses.asdict", "dataclasses.astuple", "dataclasses.replace")
private val ATTRS_HELPERS =
setOf("attr.__init__.fields", "attr.__init__.asdict", "attr.__init__.astuple", "attr.__init__.assoc", "attr.__init__.evolve")
private val ATTRS_HELPERS = setOf("attr.__init__.fields",
"attr.__init__.fields_dict",
"attr.__init__.asdict",
"attr.__init__.astuple",
"attr.__init__.assoc",
"attr.__init__.evolve")
}
override fun buildVisitor(holder: ProblemsHolder,
@@ -522,7 +526,7 @@ class PyDataclassInspection : PyInspection() {
private fun processHelperAttrsArgument(argument: PyExpression?, calleeQName: String) {
if (argument == null) return
val instance = calleeQName != "attr.__init__.fields"
val instance = calleeQName != "attr.__init__.fields" && calleeQName != "attr.__init__.fields_dict"
if (isNotExpectedDataclass(myTypeEvalContext.getType(argument), PyDataclassParameters.Type.ATTRS, !instance, instance)) {
val presentableCalleeQName = calleeQName.replaceFirst(".__init__.", ".")
@@ -9,6 +9,9 @@ class A:
attr.fields(<warning descr="'attr.fields' method should be called on attrs types">A</warning>)
attr.fields(<warning descr="'attr.fields' method should be called on attrs types">A()</warning>)
attr.fields_dict(<warning descr="'attr.fields_dict' method should be called on attrs types">A</warning>)
attr.fields_dict(<warning descr="'attr.fields_dict' method should be called on attrs types">A()</warning>)
attr.asdict(<warning descr="'attr.asdict' method should be called on attrs instances">A()</warning>)
attr.astuple(<warning descr="'attr.astuple' method should be called on attrs instances">A()</warning>)
attr.assoc(<warning descr="'attr.assoc' method should be called on attrs instances">A()</warning>)
@@ -23,6 +26,9 @@ class B:
attr.fields(B)
attr.fields(<warning descr="'attr.fields' method should be called on attrs types">B()</warning>)
attr.fields_dict(B)
attr.fields_dict(<warning descr="'attr.fields_dict' method should be called on attrs types">B()</warning>)
attr.asdict(B())
attr.astuple(B())
attr.assoc(B())
@@ -36,6 +42,7 @@ attr.evolve(<warning descr="'attr.evolve' method should be called on attrs insta
def unknown(p):
attr.fields(p)
attr.fields_dict(p)
attr.asdict(p)
attr.astuple(p)
@@ -44,6 +51,7 @@ def unknown(p):
def structural(p):
print(len(p))
attr.fields(p)
attr.fields_dict(p)
attr.asdict(p)
attr.astuple(p)
@@ -53,6 +61,7 @@ def structural(p):
def union1(p: Union[A, B]):
attr.fields(<warning descr="'attr.fields' method should be called on attrs types">p</warning>)
attr.fields_dict(<warning descr="'attr.fields_dict' method should be called on attrs types">p</warning>)
attr.asdict(p)
attr.astuple(p)
@@ -62,6 +71,7 @@ def union1(p: Union[A, B]):
def union2(p: Union[Type[A], Type[B]]):
attr.fields(p)
attr.fields_dict(p)
attr.asdict(<warning descr="'attr.asdict' method should be called on attrs instances">p</warning>)
attr.astuple(<warning descr="'attr.astuple' method should be called on attrs instances">p</warning>)