Skip to content

Commit ab806f3

Browse files
authored
Merge pull request #1085 from PyCQA/revert-1041
Revert "Merge pull request #1041 from asfaltboy/issue-830-e721-types-regex-incorrect"
2 parents d356662 + c14bd2a commit ab806f3

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

pycodestyle.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,8 @@
134134
r'\s*(?(1)|(None|False|True))\b')
135135
COMPARE_NEGATIVE_REGEX = re.compile(r'\b(?<!is\s)(not)\s+[^][)(}{ ]+\s+'
136136
r'(in|is)\s')
137-
COMPARE_TYPE_REGEX = re.compile(
138-
r'(?:[=!]=|is(?:\s+not)?)\s+type(?:\s*\(\s*([^)]*[^ )])\s*\))' +
139-
r'|\btype(?:\s*\(\s*([^)]*[^ )])\s*\))\s+(?:[=!]=|is(?:\s+not)?)'
140-
)
137+
COMPARE_TYPE_REGEX = re.compile(r'(?:[=!]=|is(?:\s+not)?)\s+type(?:s.\w+Type'
138+
r'|\s*\(\s*([^)]*[^ )])\s*\))')
141139
KEYWORD_REGEX = re.compile(r'(\s*)\b(?:%s)\b(\s*)' % r'|'.join(KEYWORDS))
142140
OPERATOR_REGEX = re.compile(r'(?:[^,\s])(\s*)(?:[-+*/|!<=>%&^]+|:=)(\s*)')
143141
LAMBDA_REGEX = re.compile(r'\blambda\b')
@@ -1446,6 +1444,13 @@ def comparison_type(logical_line, noqa):
14461444
14471445
Okay: if isinstance(obj, int):
14481446
E721: if type(obj) is type(1):
1447+
1448+
When checking if an object is a string, keep in mind that it might
1449+
be a unicode string too! In Python 2.3, str and unicode have a
1450+
common base class, basestring, so you can do:
1451+
1452+
Okay: if isinstance(obj, basestring):
1453+
Okay: if type(a1) is type(b1):
14491454
"""
14501455
match = COMPARE_TYPE_REGEX.search(logical_line)
14511456
if match and not noqa:

testsuite/E72.py

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

1010
if res == types.IntType:
@@ -47,6 +47,8 @@
4747
pass
4848
if isinstance(res, types.MethodType):
4949
pass
50+
if type(a) != type(b) or type(a) == type(ccc):
51+
pass
5052
#: Okay
5153
def func_histype(a, b, c):
5254
pass
@@ -79,11 +81,6 @@ def func_histype(a, b, c):
7981
except Exception:
8082
pass
8183
#: Okay
82-
from . import custom_types as types
83-
84-
red = types.ColorTypeRED
85-
red is types.ColorType.RED
86-
#: Okay
8784
from . import compute_type
8885

8986
if compute_type(foo) == 5:

0 commit comments

Comments
 (0)