From dba8137dcbc59747b22bef62f8bdefa46007ab19 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Mon, 13 Aug 2012 17:13:38 +0400 Subject: [PATCH 1/7] Revert "Don't ignore unused parameters in __new__ and __init__ (PY-7178)" This reverts commit be7e5dc7d855308d709a37c8263529ae0f97ad0e. --- .../python/inspections/PyUnusedLocalInspectionVisitor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/src/com/jetbrains/python/inspections/PyUnusedLocalInspectionVisitor.java b/python/src/com/jetbrains/python/inspections/PyUnusedLocalInspectionVisitor.java index 6c9e59593682..5a0db99d6155 100644 --- a/python/src/com/jetbrains/python/inspections/PyUnusedLocalInspectionVisitor.java +++ b/python/src/com/jetbrains/python/inspections/PyUnusedLocalInspectionVisitor.java @@ -276,7 +276,7 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor { continue; } if (func.asMethod() != null) { - if (funcName != null && isSpecialName(funcName) && !PyNames.INIT.equals(funcName) && !PyNames.NEW.equals(funcName)) { + if (funcName != null && isSpecialName(funcName)) { continue; } Boolean isEmpty = emptyFunctions.get(func); From 2a6a45875910ca2e1f6067bddb8b5774f384d7a7 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Mon, 13 Aug 2012 17:14:59 +0400 Subject: [PATCH 2/7] Revert "Don't highlight unused parameters of special methods with double underscore (PY-7178)" This reverts commit ccfcb039fbc5b5a8e49cc000a387ff486f6d6204. --- .../inspections/PyUnusedLocalInspectionVisitor.java | 11 +---------- .../PyUnusedLocalVariableInspection/test.py | 6 ------ 2 files changed, 1 insertion(+), 16 deletions(-) diff --git a/python/src/com/jetbrains/python/inspections/PyUnusedLocalInspectionVisitor.java b/python/src/com/jetbrains/python/inspections/PyUnusedLocalInspectionVisitor.java index 5a0db99d6155..07667e9cf957 100644 --- a/python/src/com/jetbrains/python/inspections/PyUnusedLocalInspectionVisitor.java +++ b/python/src/com/jetbrains/python/inspections/PyUnusedLocalInspectionVisitor.java @@ -266,8 +266,7 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor { if (paramList != null && paramList.getParent() instanceof PyFunction) { final PyFunction func = (PyFunction) paramList.getParent(); containingClass = func.getContainingClass(); - final String funcName = func.getName(); - if (PyNames.INIT.equals(funcName) && containingClass != null) { + if (PyNames.INIT.equals(func.getName()) && containingClass != null) { if (!namedParameter.isKeywordContainer() && !namedParameter.isPositionalContainer()) { mayBeField = true; } @@ -276,9 +275,6 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor { continue; } if (func.asMethod() != null) { - if (funcName != null && isSpecialName(funcName)) { - continue; - } Boolean isEmpty = emptyFunctions.get(func); if (isEmpty == null) { isEmpty = isEmptyFunction(func); @@ -313,11 +309,6 @@ public class PyUnusedLocalInspectionVisitor extends PyInspectionVisitor { } } - private static boolean isSpecialName(@NotNull String name) { - final String dunder = "__"; - return name.startsWith(dunder) && name.endsWith(dunder) && name.length() > (dunder.length() * 2); - } - private boolean isRangeIteration(PyForStatement forStatement) { final PyExpression source = forStatement.getForPart().getSource(); if (!(source instanceof PyCallExpression)) { diff --git a/python/testData/inspections/PyUnusedLocalVariableInspection/test.py b/python/testData/inspections/PyUnusedLocalVariableInspection/test.py index 087e5f3f2bb2..ec747ac101f2 100644 --- a/python/testData/inspections/PyUnusedLocalVariableInspection/test.py +++ b/python/testData/inspections/PyUnusedLocalVariableInspection/test.py @@ -286,9 +286,3 @@ class C: # PY-7126 def test_unused_empty_function(x): pass - - -# PY-7178 -class C(object): - def __getattr__(self, name): # pass - return 42 From 613b414c1c6ad328d8ba0d2a1107ef617582143a Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Mon, 13 Aug 2012 21:28:08 +0400 Subject: [PATCH 3/7] Guess the return type of __enter__ for completion as a weak type (PY-7168) --- .../jetbrains/python/psi/impl/PyTargetExpressionImpl.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java index 5e379d6004e6..6e42b989c675 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java @@ -181,7 +181,12 @@ public class PyTargetExpressionImpl extends PyPresentableElementImpl Date: Mon, 13 Aug 2012 21:29:36 +0400 Subject: [PATCH 4/7] Not null check --- .../jetbrains/python/psi/impl/PyTargetExpressionImpl.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java b/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java index 6e42b989c675..84a1bd255be6 100644 --- a/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java +++ b/python/src/com/jetbrains/python/psi/impl/PyTargetExpressionImpl.java @@ -446,7 +446,10 @@ public class PyTargetExpressionImpl extends PyPresentableElementImpl Date: Mon, 13 Aug 2012 21:54:17 +0400 Subject: [PATCH 5/7] Show the type of the resolved element in quick documentation pop-up, not the type of the original element (PY-7127) --- .../python/documentation/PythonDocumentationProvider.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java b/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java index d36d71d6c5e4..6b1fc574f899 100644 --- a/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java +++ b/python/src/com/jetbrains/python/documentation/PythonDocumentationProvider.java @@ -97,7 +97,7 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i return describeDecorators(cls, LSame2, ", ", LSame1).add(describeClass(cls, LSame2, false, false)).toString() + "\n" + summary; } else if (element instanceof PyTargetExpression || element instanceof PyNamedParameter) { - return describeExpression((PyExpression)element, originalElement); + return describeExpression((PyExpression)element); } return null; } @@ -126,7 +126,7 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i } @Nullable - private static String describeExpression(PyExpression expr, PsiElement originalElement) { + private static String describeExpression(PyExpression expr) { final String name = expr.getName(); if (name != null) { StringBuilder result = new StringBuilder((expr instanceof PyNamedParameter) ? "parameter" : "variable"); @@ -138,9 +138,7 @@ public class PythonDocumentationProvider extends AbstractDocumentationProvider i result.append(String.format(" \"%s\"", function.getName())); } } - if (originalElement instanceof PyExpression && originalElement.isValid()) { - result.append("\n").append(describeExpressionType((PyExpression)originalElement)); - } + result.append("\n").append(describeExpressionType(expr)); return result.toString(); } return null; From 9f9a11503209e8f5242d9eb6fba70fe38f83a455 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Mon, 13 Aug 2012 22:28:32 +0400 Subject: [PATCH 6/7] Don't show "Introduce variable for statement" quickfix for simple reference expressions (PY-7189) --- python/src/com/jetbrains/python/PyBundle.properties | 1 + .../inspections/PyStatementEffectInspection.java | 11 ++++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/python/src/com/jetbrains/python/PyBundle.properties b/python/src/com/jetbrains/python/PyBundle.properties index 4e23df221842..a747d89160b7 100644 --- a/python/src/com/jetbrains/python/PyBundle.properties +++ b/python/src/com/jetbrains/python/PyBundle.properties @@ -328,6 +328,7 @@ INSP.empty.docstring=Empty docstring # PyStatementEffectInspection INSP.NAME.statement.effect=Statement has no effect +INSP.NAME.statement.message=Statement seems to have no effect # PySimplifyBooleanCheckInspection INSP.NAME.check.can.be.simplified=Boolean variable check can be simplified diff --git a/python/src/com/jetbrains/python/inspections/PyStatementEffectInspection.java b/python/src/com/jetbrains/python/inspections/PyStatementEffectInspection.java index 7157791158ed..264d2c7efa0f 100644 --- a/python/src/com/jetbrains/python/inspections/PyStatementEffectInspection.java +++ b/python/src/com/jetbrains/python/inspections/PyStatementEffectInspection.java @@ -62,10 +62,15 @@ public class PyStatementEffectInspection extends PyInspection { return; } } - if (checkStringLiteral(expression)) + if (checkStringLiteral(expression)) { return; - registerProblem(expression, "Statement seems to have no effect", - new StatementEffectIntroduceVariableQuickFix()); + } + if (expression instanceof PyReferenceExpression && ((PyReferenceExpression)expression).getQualifier() == null) { + registerProblem(expression, PyBundle.message("INSP.NAME.statement.message")); + } + else { + registerProblem(expression, PyBundle.message("INSP.NAME.statement.message"), new StatementEffectIntroduceVariableQuickFix()); + } } private boolean checkStringLiteral(PyExpression expression) { From ad1e026fe381a70a926113b7e046d21cdc5c3704 Mon Sep 17 00:00:00 2001 From: Andrey Vlasovskikh Date: Mon, 13 Aug 2012 23:06:12 +0400 Subject: [PATCH 7/7] Don't highlight __package__ as unresolved (PY-7043) --- python/src/com/jetbrains/python/psi/types/PyModuleType.java | 2 +- .../PyUnresolvedReferencesInspection/dunderPackage.py | 1 + .../inspections/PyUnresolvedReferencesInspectionTest.java | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 python/testData/inspections/PyUnresolvedReferencesInspection/dunderPackage.py diff --git a/python/src/com/jetbrains/python/psi/types/PyModuleType.java b/python/src/com/jetbrains/python/psi/types/PyModuleType.java index 111b763dc9a9..eba982c4d781 100644 --- a/python/src/com/jetbrains/python/psi/types/PyModuleType.java +++ b/python/src/com/jetbrains/python/psi/types/PyModuleType.java @@ -39,7 +39,7 @@ public class PyModuleType implements PyType { // Modules don't descend from obje @NotNull private final PyFile myModule; @Nullable private final PyImportedModule myImportedModule; - protected static ImmutableSet ourPossibleFields = ImmutableSet.of("__name__", "__file__", "__path__", "__doc__", "__dict__"); + protected static ImmutableSet ourPossibleFields = ImmutableSet.of("__name__", "__file__", "__path__", "__doc__", "__dict__", "__package__"); public PyModuleType(@NotNull PyFile source) { this(source, null); diff --git a/python/testData/inspections/PyUnresolvedReferencesInspection/dunderPackage.py b/python/testData/inspections/PyUnresolvedReferencesInspection/dunderPackage.py new file mode 100644 index 000000000000..5439e8997e21 --- /dev/null +++ b/python/testData/inspections/PyUnresolvedReferencesInspection/dunderPackage.py @@ -0,0 +1 @@ +__package__ #pass \ No newline at end of file diff --git a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java index 10a1e5e9364a..efd5d0eb1606 100644 --- a/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java +++ b/python/testSrc/com/jetbrains/python/inspections/PyUnresolvedReferencesInspectionTest.java @@ -165,6 +165,11 @@ public class PyUnresolvedReferencesInspectionTest extends PyTestCase { doTest(); } + // PY-7043 + public void testDunderPackage() { + doTest(); + } + private void doTest() { myFixture.configureByFile(TEST_DIRECTORY + getTestName(true) + ".py"); myFixture.enableInspections(PyUnresolvedReferencesInspection.class);