Raise a warning on isinstance and issubclass checks with Final (PEP 591) (PY-34945)

GitOrigin-RevId: c084604a96fbe0c61e149f40da47619e2fc4b12e
This commit is contained in:
Semyon Proshev
2019-07-02 06:52:16 +03:00
committed by intellij-monorepo-bot
parent 9709256ca6
commit 163bddf739
2 changed files with 28 additions and 8 deletions
@@ -64,7 +64,7 @@ class PyTypeHintsInspection : PyInspection() {
super.visitPyClass(node)
if (node != null) {
val superClassExpressions = node.superClassExpressions.toList()
val superClassExpressions = node.superClassExpressions.asList()
checkPlainGenericInheritance(superClassExpressions)
checkGenericDuplication(superClassExpressions)
@@ -246,7 +246,9 @@ class PyTypeHintsInspection : PyInspection() {
PyTypingTypeProvider.GENERIC,
PyTypingTypeProvider.OPTIONAL,
PyTypingTypeProvider.CLASS_VAR,
PyTypingTypeProvider.NO_RETURN ->
PyTypingTypeProvider.NO_RETURN,
PyTypingTypeProvider.FINAL,
PyTypingTypeProvider.FINAL_EXT ->
registerProblem(base,
"'${it.substringAfterLast('.')}' cannot be used with instance and class checks",
ProblemHighlightType.GENERIC_ERROR)
@@ -275,14 +277,12 @@ class PyTypeHintsInspection : PyInspection() {
val qName = it.qualifiedName
when (qName) {
PyTypingTypeProvider.GENERIC -> {
registerProblem(base, "'Generic' cannot be used with instance and class checks", ProblemHighlightType.GENERIC_ERROR)
return@forEach
}
PyTypingTypeProvider.GENERIC,
PyTypingTypeProvider.UNION,
PyTypingTypeProvider.OPTIONAL,
PyTypingTypeProvider.CLASS_VAR -> {
PyTypingTypeProvider.CLASS_VAR,
PyTypingTypeProvider.FINAL,
PyTypingTypeProvider.FINAL_EXT -> {
registerProblem(base,
"'${qName.substringAfterLast('.')}' cannot be used with instance and class checks",
ProblemHighlightType.GENERIC_ERROR)
@@ -342,6 +342,26 @@ public class PyTypeHintsInspectionTest extends PyInspectionTestCase {
"assert issubclass(A, <error descr=\"'Generic' cannot be used with instance and class checks\">C</error>)");
}
// PY-34945
public void testInstanceAndClassChecksOnFinal() {
doTestByText("from typing import TypeVar\n" +
"from typing_extensions import Final\n" +
"\n" +
"T = TypeVar(\"T\")\n" +
"\n" +
"class A:\n" +
" pass\n" +
"\n" +
"assert isinstance(A(), <error descr=\"'Final' cannot be used with instance and class checks\">Final</error>)\n" +
"B = Final\n" +
"assert issubclass(A, <error descr=\"'Final' cannot be used with instance and class checks\">B</error>)\n" +
"\n" +
"assert isinstance(A(), <error descr=\"'Final' cannot be used with instance and class checks\">Final[T]</error>)\n" +
"assert issubclass(A, <error descr=\"'Final' cannot be used with instance and class checks\">B[T]</error>)\n" +
"C = B[T]\n" +
"assert issubclass(A, <error descr=\"'Final' cannot be used with instance and class checks\">C</error>)");
}
// PY-28249
public void testInstanceAndClassChecksOnGenericInheritor() {
doTestByText("from typing import TypeVar, List\n" +