Skip to content

Commit 922e7fd

Browse files
committed
Merge pull request #260 from chrismedrela/ticket236
Fixed #236 -- added checks for 'not X in L' and 'not X is Y'
2 parents 173592f + 9a72d59 commit 922e7fd

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
@@ -302,6 +302,10 @@ This is the current list of error and warning codes:
302302
+----------+----------------------------------------------------------------------+
303303
| E712 (^) | comparison to True should be 'if cond is True:' or 'if cond:' |
304304
+----------+----------------------------------------------------------------------+
305+
| E713 | evaluating membership should be 'elem not in collection' |
306+
+----------+----------------------------------------------------------------------+
307+
| E714 | testing unequal identities should be 'x is not y' |
308+
+----------+----------------------------------------------------------------------+
305309
| E721 | do not compare types, use 'isinstance()' |
306310
+----------+----------------------------------------------------------------------+
307311
+----------+----------------------------------------------------------------------+
@@ -342,6 +346,7 @@ This is the current list of error and warning codes:
342346
| W604 | backticks are deprecated, use 'repr()' |
343347
+----------+----------------------------------------------------------------------+
344348

349+
345350
**(*)** In the default configuration, the checks **E123**, **E133**, **E226**,
346351
**E241** and **E242** are ignored because they are not rules unanimously
347352
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
@@ -1018,6 +1018,40 @@ def python_3000_backticks(logical_line):
10181018
yield pos, "W604 backticks are deprecated, use 'repr()'"
10191019

10201020

1021+
def not_in(logical_line):
1022+
"""
1023+
Check for use of "not in" for evaluating membership.
1024+
1025+
Okay: if x not in y:\n pass
1026+
Okay: if not (X in Y or X is Z):\n pass
1027+
Okay: if not (X in Y):\n pass
1028+
E713: if not X in Y
1029+
E713: if not X.B in Y
1030+
"""
1031+
1032+
split_line = logical_line.split()
1033+
if (len(split_line) == 5 and split_line[0] == 'if' and
1034+
split_line[1] == 'not' and split_line[3] == 'in' and not
1035+
split_line[2].startswith('(')):
1036+
yield (logical_line.find('not'), "E713: Use the 'not in' "
1037+
"operator for collection membership evaluation")
1038+
1039+
1040+
def is_not(logical_line):
1041+
"""
1042+
Check for use of 'is not' for testing unequal identities.
1043+
1044+
Okay: if x is not y:\n pass
1045+
E714: if not X is Y
1046+
E714: if not X.B is Y
1047+
"""
1048+
1049+
split_line = logical_line.split()
1050+
if (len(split_line) == 5 and split_line[0] == 'if' and
1051+
split_line[1] == 'not' and split_line[3] == 'is'):
1052+
yield (logical_line.find('not'), "E714: Use the 'is not' "
1053+
"operator when testing for unequal identities")
1054+
10211055
##############################################################################
10221056
# Helper functions
10231057
##############################################################################

0 commit comments

Comments
 (0)