106
106
DOCSTRING_REGEX = re .compile (r'u?r?["\']' )
107
107
EXTRANEOUS_WHITESPACE_REGEX = re .compile (r'[[({] | []}),;:]' )
108
108
WHITESPACE_AFTER_COMMA_REGEX = re .compile (r'[,;:]\s*(?: |\t)' )
109
- COMPARE_SINGLETON_REGEX = re .compile (r'(?P<op>[=!]=)\s*'
110
- r'(?P<singleton>None|False|True)' )
111
- COMPARE_SINGLETON_REVERSE_REGEX = re .compile (r'(?P<singleton>None|False|True)'
112
- r'\s*(?P<op>[=!]=)' )
109
+ COMPARE_SINGLETON_REGEX = re .compile (r'\b(None|False|True)?\s*([=!]=)'
110
+ r'\s*(?(1)|(None|False|True))\b' )
113
111
COMPARE_NEGATIVE_REGEX = re .compile (r'\b(not)\s+[^[({ ]+\s+(in|is)\s' )
114
112
COMPARE_TYPE_REGEX = re .compile (r'(?:[=!]=|is(?:\s+not)?)\s*type(?:s.\w+Type'
115
113
r'|\s*\(\s*([^)]*[^ )])\s*\))' )
@@ -998,12 +996,10 @@ def comparison_to_singleton(logical_line, noqa):
998
996
set to some other value. The other value might have a type (such as a
999
997
container) that could be false in a boolean context!
1000
998
"""
1001
-
1002
- match = not noqa and (COMPARE_SINGLETON_REGEX .search (logical_line ) or
1003
- COMPARE_SINGLETON_REVERSE_REGEX .search (logical_line ))
999
+ match = not noqa and COMPARE_SINGLETON_REGEX .search (logical_line )
1004
1000
if match :
1005
- singleton = match .group ('singleton' )
1006
- same = (match .group ('op' ) == '==' )
1001
+ singleton = match .group (1 ) or match . group ( 3 )
1002
+ same = (match .group (2 ) == '==' )
1007
1003
1008
1004
msg = "'if cond is %s:'" % (('' if same else 'not ' ) + singleton )
1009
1005
if singleton in ('None' ,):
@@ -1013,7 +1009,7 @@ def comparison_to_singleton(logical_line, noqa):
1013
1009
nonzero = ((singleton == 'True' and same ) or
1014
1010
(singleton == 'False' and not same ))
1015
1011
msg += " or 'if %scond:'" % ('' if nonzero else 'not ' )
1016
- yield match .start (1 ), ("%s comparison to %s should be %s" %
1012
+ yield match .start (2 ), ("%s comparison to %s should be %s" %
1017
1013
(code , singleton , msg ))
1018
1014
1019
1015
0 commit comments