|
102 | 102 | DOCSTRING_REGEX = re.compile(r'u?r?["\']') |
103 | 103 | EXTRANEOUS_WHITESPACE_REGEX = re.compile(r'[[({] | []}),;:]') |
104 | 104 | 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>[=!]=)') |
106 | 109 | COMPARE_NEGATIVE_REGEX = re.compile(r'\b(not)\s+[^[({ ]+\s+(in|is)\s') |
107 | 110 | COMPARE_TYPE_REGEX = re.compile(r'(?:[=!]=|is(?:\s+not)?)\s*type(?:s.\w+Type' |
108 | 111 | r'|\s*\(\s*([^)]*[^ )])\s*\))') |
@@ -931,17 +934,22 @@ def comparison_to_singleton(logical_line, noqa): |
931 | 934 |
|
932 | 935 | Okay: if arg is not None: |
933 | 936 | E711: if arg != None: |
| 937 | + E711: if None == arg: |
934 | 938 | E712: if arg == True: |
| 939 | + E712: if False == arg: |
935 | 940 |
|
936 | 941 | Also, beware of writing if x when you really mean if x is not None -- |
937 | 942 | e.g. when testing whether a variable or argument that defaults to None was |
938 | 943 | set to some other value. The other value might have a type (such as a |
939 | 944 | container) that could be false in a boolean context! |
940 | 945 | """ |
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)) |
942 | 949 | if match: |
943 | | - same = (match.group(1) == '==') |
944 | | - singleton = match.group(2) |
| 950 | + singleton = match.group('singleton') |
| 951 | + same = (match.group('op') == '==') |
| 952 | + |
945 | 953 | msg = "'if cond is %s:'" % (('' if same else 'not ') + singleton) |
946 | 954 | if singleton in ('None',): |
947 | 955 | code = 'E711' |
|
0 commit comments