106106DOCSTRING_REGEX = re .compile (r'u?r?["\']' )
107107EXTRANEOUS_WHITESPACE_REGEX = re .compile (r'[[({] | []}),;:]' )
108108WHITESPACE_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>[=!]=)' )
113- COMPARE_NEGATIVE_REGEX = re .compile (r'\b(not)\s+[^[({ ]+\s+(in|is)\s' )
109+ COMPARE_SINGLETON_REGEX = re .compile (r'\b(None|False|True)?\s*([=!]=)'
110+ r'\s*(?(1)|(None|False|True))\b' )
111+ COMPARE_NEGATIVE_REGEX = re .compile (r'\b(not)\s+[^][)(}{ ]+\s+(in|is)\s' )
114112COMPARE_TYPE_REGEX = re .compile (r'(?:[=!]=|is(?:\s+not)?)\s*type(?:s.\w+Type'
115113 r'|\s*\(\s*([^)]*[^ )])\s*\))' )
116114KEYWORD_REGEX = re .compile (r'(\s*)\b(?:%s)\b(\s*)' % r'|' .join (KEYWORDS ))
@@ -998,12 +996,10 @@ def comparison_to_singleton(logical_line, noqa):
998996 set to some other value. The other value might have a type (such as a
999997 container) that could be false in a boolean context!
1000998 """
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 )
10041000 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 ) == '==' )
10071003
10081004 msg = "'if cond is %s:'" % (('' if same else 'not ' ) + singleton )
10091005 if singleton in ('None' ,):
@@ -1013,7 +1009,7 @@ def comparison_to_singleton(logical_line, noqa):
10131009 nonzero = ((singleton == 'True' and same ) or
10141010 (singleton == 'False' and not same ))
10151011 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" %
10171013 (code , singleton , msg ))
10181014
10191015
0 commit comments