Skip to content

Commit 17ba1e1

Browse files
committed
Merge pull request #347 from helenst/e711-reverse-comparison
Looks good, thanks!
2 parents 79c1fb7 + c386609 commit 17ba1e1

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

pep8.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@
102102
DOCSTRING_REGEX = re.compile(r'u?r?["\']')
103103
EXTRANEOUS_WHITESPACE_REGEX = re.compile(r'[[({] | []}),;:]')
104104
WHITESPACE_AFTER_COMMA_REGEX = re.compile(r'[,;:]\s*(?: |\t)')
105-
COMPARE_SINGLETON_REGEX = re.compile(r'([=!]=)\s*(None|False|True)')
105+
COMPARE_SINGLETON_REGEX = re.compile(r'(?P<op>[=!]=)\s*'
106+
r'(?P<singleton>None|False|True)')
107+
COMPARE_SINGLETON_REVERSE_REGEX = re.compile(r'(?P<singleton>None|False|True)'
108+
r'\s*(?P<op>[=!]=)')
106109
COMPARE_NEGATIVE_REGEX = re.compile(r'\b(not)\s+[^[({ ]+\s+(in|is)\s')
107110
COMPARE_TYPE_REGEX = re.compile(r'(?:[=!]=|is(?:\s+not)?)\s*type(?:s.\w+Type'
108111
r'|\s*\(\s*([^)]*[^ )])\s*\))')
@@ -931,17 +934,22 @@ def comparison_to_singleton(logical_line, noqa):
931934
932935
Okay: if arg is not None:
933936
E711: if arg != None:
937+
E711: if None == arg:
934938
E712: if arg == True:
939+
E712: if False == arg:
935940
936941
Also, beware of writing if x when you really mean if x is not None --
937942
e.g. when testing whether a variable or argument that defaults to None was
938943
set to some other value. The other value might have a type (such as a
939944
container) that could be false in a boolean context!
940945
"""
941-
match = not noqa and COMPARE_SINGLETON_REGEX.search(logical_line)
946+
947+
match = not noqa and (COMPARE_SINGLETON_REGEX.search(logical_line) or
948+
COMPARE_SINGLETON_REVERSE_REGEX.search(logical_line))
942949
if match:
943-
same = (match.group(1) == '==')
944-
singleton = match.group(2)
950+
singleton = match.group('singleton')
951+
same = (match.group('op') == '==')
952+
945953
msg = "'if cond is %s:'" % (('' if same else 'not ') + singleton)
946954
if singleton in ('None',):
947955
code = 'E711'

testsuite/E71.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
11
#: E711
22
if res == None:
33
pass
4+
#: E711
5+
if res != None:
6+
pass
7+
#: E711
8+
if None == res:
9+
pass
10+
#: E711
11+
if None != res:
12+
pass
13+
14+
#
415
#: E712
516
if res == True:
617
pass
718
#: E712
819
if res != False:
920
pass
21+
#: E712
22+
if True != res:
23+
pass
24+
#: E712
25+
if False == res:
26+
pass
1027

1128
#
1229
#: E713

0 commit comments

Comments
 (0)