summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIan Lee <IanLee1521@gmail.com>2014-12-13 19:55:21 -0800
committerIan Lee <IanLee1521@gmail.com>2014-12-13 19:55:21 -0800
commit17ba1e11f654e375e67299122c620a812d7895d3 (patch)
treeee443a34c0add0c567af9566d2d7c6b49f689b3a
parent79c1fb7b0efcc38dfa42265ee6769eaaf252d551 (diff)
parentc3866095dc5252c80bdb4095d52a891ff76f7626 (diff)
downloadpep8-17ba1e11f654e375e67299122c620a812d7895d3.tar.gz
Merge pull request #347 from helenst/e711-reverse-comparison
Looks good, thanks!
-rwxr-xr-xpep8.py16
-rw-r--r--testsuite/E71.py17
2 files changed, 29 insertions, 4 deletions
diff --git a/pep8.py b/pep8.py
index 0fbe22d..11a39e8 100755
--- a/pep8.py
+++ b/pep8.py
@@ -102,7 +102,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*\))')
@@ -931,17 +934,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'
diff --git a/testsuite/E71.py b/testsuite/E71.py
index 2ff5dea..3f07b1a 100644
--- a/testsuite/E71.py
+++ b/testsuite/E71.py
@@ -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