PY-14082 Django: support queryset_only field for QuerySet.as_manager()
authorIlya.Kazakevich <Ilya.Kazakevich@jetbrains.com>
Mon, 13 Oct 2014 15:10:39 +0000 (19:10 +0400)
committerIlya.Kazakevich <Ilya.Kazakevich@jetbrains.com>
Mon, 13 Oct 2014 15:10:39 +0000 (19:10 +0400)
python/src/com/jetbrains/python/psi/impl/PyEvaluator.java

index 3f948c496592fa9c915084ceadb062c8fd93d665..aae0b8883dba070956db057f1cc85edcb712d797 100644 (file)
@@ -65,14 +65,9 @@ public class PyEvaluator {
     if (expr instanceof PySequenceExpression) {
       return evaluateSequenceExpression((PySequenceExpression)expr);
     }
-    if (expr instanceof PyQualifiedExpression) { // support bool
-      final String referencedName = ((PyQualifiedExpression)expr).getReferencedName();
-      if (PyNames.TRUE.equals(referencedName)) {
-        return true;
-      }
-      if (PyNames.FALSE.equals(referencedName)) {
-        return true;
-      }
+    final Boolean booleanExpression = getBooleanExpression(expr);
+    if (booleanExpression != null) { // support bool
+      return booleanExpression;
     }
     if (expr instanceof PyCallExpression) {
       return evaluateCall((PyCallExpression)expr);
@@ -97,6 +92,32 @@ public class PyEvaluator {
     return null;
   }
 
+  /**
+   * TODO: Move to PyExpression? PyUtil?
+   * True/False is bool literal in Py3K, but reference in Python2.
+   *
+   * @param expression expression to check
+   * @return true if expression is boolean
+   */
+  @Nullable
+  private static Boolean getBooleanExpression(@NotNull final PyExpression expression) {
+    final boolean py3K = LanguageLevel.forElement(expression).isPy3K();
+    if ((py3K && (expression instanceof PyBoolLiteralExpression))) {
+      return ((PyBoolLiteralExpression)expression).getValue(); // Cool in Py2K
+    }
+    if ((!py3K && (expression instanceof PyReferenceExpression))) {
+      final String text = ((PyReferenceExpression)expression).getReferencedName(); // Ref in Python2
+      if (PyNames.TRUE.equals(text)) {
+        return true;
+      }
+      if (PyNames.FALSE.equals(text)) {
+        return false;
+      }
+    }
+
+    return null;
+  }
+
   /**
    * Evaluates some sequence (tuple, list)
    *