Skip to content

Commit 2326e16

Browse files
authored
Merge pull request #1086 from PyCQA/E721-allow-is-v2
allow `is` and `is not` for type comparisons (E721)
2 parents 9a0da24 + ac223bd commit 2326e16

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

pycodestyle.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,10 @@
128128
r'\s*(?(1)|(None|False|True))\b')
129129
COMPARE_NEGATIVE_REGEX = re.compile(r'\b(?<!is\s)(not)\s+[^][)(}{ ]+\s+'
130130
r'(in|is)\s')
131-
COMPARE_TYPE_REGEX = re.compile(r'(?:[=!]=|is(?:\s+not)?)\s+type(?:s.\w+Type'
132-
r'|\s*\(\s*([^)]*[^ )])\s*\))')
131+
COMPARE_TYPE_REGEX = re.compile(
132+
r'[=!]=\s+type(?:\s*\(\s*([^)]*[^ )])\s*\))'
133+
r'|\btype(?:\s*\(\s*([^)]*[^ )])\s*\))\s+[=!]='
134+
)
133135
KEYWORD_REGEX = re.compile(r'(\s*)\b(?:%s)\b(\s*)' % r'|'.join(KEYWORDS))
134136
OPERATOR_REGEX = re.compile(r'(?:[^,\s])(\s*)(?:[-+*/|!<=>%&^]+|:=)(\s*)')
135137
LAMBDA_REGEX = re.compile(r'\blambda\b')
@@ -1457,14 +1459,7 @@ def comparison_type(logical_line, noqa):
14571459
Do not compare types directly.
14581460
14591461
Okay: if isinstance(obj, int):
1460-
E721: if type(obj) is type(1):
1461-
1462-
When checking if an object is a string, keep in mind that it might
1463-
be a unicode string too! In Python 2.3, str and unicode have a
1464-
common base class, basestring, so you can do:
1465-
1466-
Okay: if isinstance(obj, basestring):
1467-
Okay: if type(a1) is type(b1):
1462+
E721: if type(obj) == type(1):
14681463
"""
14691464
match = COMPARE_TYPE_REGEX.search(logical_line)
14701465
if match and not noqa:

testsuite/E72.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
#: E721
55
if type(res) != type(""):
66
pass
7-
#: E721
7+
#: Okay
88
import types
99

1010
if res == types.IntType:
1111
pass
12-
#: E721
12+
#: Okay
1313
import types
1414

1515
if type(res) is not types.ListType:
@@ -26,9 +26,9 @@
2626
assert type(res) == type((0))
2727
#: E721
2828
assert type(res) != type((1, ))
29-
#: E721
29+
#: Okay
3030
assert type(res) is type((1, ))
31-
#: E721
31+
#: Okay
3232
assert type(res) is not type((1, ))
3333
#: E211 E721
3434
assert type(res) == type ([2, ])
@@ -47,8 +47,6 @@
4747
pass
4848
if isinstance(res, types.MethodType):
4949
pass
50-
if type(a) != type(b) or type(a) == type(ccc):
51-
pass
5250
#: Okay
5351
def func_histype(a, b, c):
5452
pass
@@ -81,6 +79,11 @@ def func_histype(a, b, c):
8179
except Exception:
8280
pass
8381
#: Okay
82+
from . import custom_types as types
83+
84+
red = types.ColorTypeRED
85+
red is types.ColorType.RED
86+
#: Okay
8487
from . import compute_type
8588

8689
if compute_type(foo) == 5:

0 commit comments

Comments
 (0)