Skip to content

Commit 9a72d59

Browse files
author
Christopher Medrela
committed
Fixed #236 -- added checks for 'not X in L' and 'not X is Y'
1 parent 8d65869 commit 9a72d59

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

docs/intro.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,10 @@ This is the current list of error and warning codes:
301301
+----------+----------------------------------------------------------------------+
302302
| E712 (^) | comparison to True should be 'if cond is True:' or 'if cond:' |
303303
+----------+----------------------------------------------------------------------+
304+
| E713 | evaluating membership should be 'elem not in collection' |
305+
+----------+----------------------------------------------------------------------+
306+
| E714 | testing unequal identities should be 'x is not y' |
307+
+----------+----------------------------------------------------------------------+
304308
| E721 | do not compare types, use 'isinstance()' |
305309
+----------+----------------------------------------------------------------------+
306310
+----------+----------------------------------------------------------------------+
@@ -341,6 +345,7 @@ This is the current list of error and warning codes:
341345
| W604 | backticks are deprecated, use 'repr()' |
342346
+----------+----------------------------------------------------------------------+
343347

348+
344349
**(*)** In the default configuration, the checks **E123**, **E133**, **E226**,
345350
**E241** and **E242** are ignored because they are not rules unanimously
346351
accepted, and `PEP 8`_ does not enforce them. The check **E133** is mutually

pep8.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,40 @@ def python_3000_backticks(logical_line):
10091009
yield pos, "W604 backticks are deprecated, use 'repr()'"
10101010

10111011

1012+
def not_in(logical_line):
1013+
"""
1014+
Check for use of "not in" for evaluating membership.
1015+
1016+
Okay: if x not in y:\n pass
1017+
Okay: if not (X in Y or X is Z):\n pass
1018+
Okay: if not (X in Y):\n pass
1019+
E713: if not X in Y
1020+
E713: if not X.B in Y
1021+
"""
1022+
1023+
split_line = logical_line.split()
1024+
if (len(split_line) == 5 and split_line[0] == 'if' and
1025+
split_line[1] == 'not' and split_line[3] == 'in' and not
1026+
split_line[2].startswith('(')):
1027+
yield (logical_line.find('not'), "E713: Use the 'not in' "
1028+
"operator for collection membership evaluation")
1029+
1030+
1031+
def is_not(logical_line):
1032+
"""
1033+
Check for use of 'is not' for testing unequal identities.
1034+
1035+
Okay: if x is not y:\n pass
1036+
E714: if not X is Y
1037+
E714: if not X.B is Y
1038+
"""
1039+
1040+
split_line = logical_line.split()
1041+
if (len(split_line) == 5 and split_line[0] == 'if' and
1042+
split_line[1] == 'not' and split_line[3] == 'is'):
1043+
yield (logical_line.find('not'), "E714: Use the 'is not' "
1044+
"operator when testing for unequal identities")
1045+
10121046
##############################################################################
10131047
# Helper functions
10141048
##############################################################################

0 commit comments

Comments
 (0)