E711 / E712 checks test for None != arg and False == arg
authorHelen ST <helenst@gmail.com>
Thu, 11 Dec 2014 19:28:12 +0000 (19:28 +0000)
committerHelen ST <helenst@gmail.com>
Thu, 11 Dec 2014 19:28:12 +0000 (19:28 +0000)
(Fixes #307)

pep8.py
testsuite/E71.py

diff --git a/pep8.py b/pep8.py
index b31a9781d05bce5b30f19f070e5f936a58133189..20356b927d48a5d754cc75c287fde5e4c8779430 100755 (executable)
--- a/pep8.py
+++ b/pep8.py
@@ -101,7 +101,10 @@ ERRORCODE_REGEX = re.compile(r'\b[A-Z]\d{3}\b')
 DOCSTRING_REGEX = re.compile(r'u?r?["\']')
 EXTRANEOUS_WHITESPACE_REGEX = re.compile(r'[[({] | []}),;:]')
 WHITESPACE_AFTER_COMMA_REGEX = re.compile(r'[,;:]\s*(?:  |\t)')
-COMPARE_SINGLETON_REGEX = re.compile(r'([=!]=)\s*(None|False|True)')
+COMPARE_SINGLETON_REGEX = re.compile(r'(?P<op>[=!]=)\s*'
+                                     r'(?P<singleton>None|False|True)')
+COMPARE_SINGLETON_REVERSE_REGEX = re.compile(r'(?P<singleton>None|False|True)'
+                                             r'\s*(?P<op>[=!]=)')
 COMPARE_NEGATIVE_REGEX = re.compile(r'\b(not)\s+[^[({ ]+\s+(in|is)\s')
 COMPARE_TYPE_REGEX = re.compile(r'(?:[=!]=|is(?:\s+not)?)\s*type(?:s.\w+Type'
                                 r'|\s*\(\s*([^)]*[^ )])\s*\))')
@@ -930,17 +933,22 @@ def comparison_to_singleton(logical_line, noqa):
 
     Okay: if arg is not None:
     E711: if arg != None:
+    E711: if None == arg:
     E712: if arg == True:
+    E712: if False == arg:
 
     Also, beware of writing if x when you really mean if x is not None --
     e.g. when testing whether a variable or argument that defaults to None was
     set to some other value.  The other value might have a type (such as a
     container) that could be false in a boolean context!
     """
-    match = not noqa and COMPARE_SINGLETON_REGEX.search(logical_line)
+
+    match = not noqa and (COMPARE_SINGLETON_REGEX.search(logical_line) or
+                          COMPARE_SINGLETON_REVERSE_REGEX.search(logical_line))
     if match:
-        same = (match.group(1) == '==')
-        singleton = match.group(2)
+        singleton = match.group('singleton')
+        same = match.group('op')
+
         msg = "'if cond is %s:'" % (('' if same else 'not ') + singleton)
         if singleton in ('None',):
             code = 'E711'
index 2ff5deadb39f993782030d43320f36604467c9fb..3f07b1a383efe5eecddc6d0af03bbef1c0380769 100644 (file)
@@ -1,12 +1,29 @@
 #: E711
 if res == None:
     pass
+#: E711
+if res != None:
+    pass
+#: E711
+if None == res:
+    pass
+#: E711
+if None != res:
+    pass
+
+#
 #: E712
 if res == True:
     pass
 #: E712
 if res != False:
     pass
+#: E712
+if True != res:
+    pass
+#: E712
+if False == res:
+    pass
 
 #
 #: E713