Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@
COMPARE_NEGATIVE_REGEX = re.compile(r'\b(?<!is\s)(not)\s+[^][)(}{ ]+\s+'
r'(in|is)\s')
COMPARE_TYPE_REGEX = re.compile(
r'[=!]=\s+type(?:\s*\(\s*([^)]*[^\s)])\s*\))'
r'|(?<!\.)\btype(?:\s*\(\s*([^)]*[^\s)])\s*\))\s+[=!]='
r'[=!]=\s+type(?:\s*\(\s*[^)]*[^\s)]\s*\))'
r'|(?<!\.)\btype(?:\s*\(\s*[^)]*[^\s)]\s*\))\s+[=!]='
)
KEYWORD_REGEX = re.compile(r'(\s*)\b(?:%s)\b(\s*)' % r'|'.join(KEYWORDS))
OPERATOR_REGEX = re.compile(r'(?:[^,\s])(\s*)(?:[-+*/|!<=>%&^]+|:=)(\s*)')
Expand Down Expand Up @@ -1498,12 +1498,11 @@ def comparison_type(logical_line, noqa):
Okay: if isinstance(obj, int):
Okay: if type(obj) is int:
E721: if type(obj) == type(1):
E721: if type(obj) == int:
E721: if int == type(obj):
"""
match = COMPARE_TYPE_REGEX.search(logical_line)
if match and not noqa:
inst = match.group(1)
if inst and inst.isidentifier() and inst not in SINGLETONS:
return # Allow comparison for types which are not obvious
yield (
match.start(),
"E721 do not compare types, for exact checks use `is` / `is not`, "
Expand Down
6 changes: 6 additions & 0 deletions testing/data/E72.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
#: E721
if type(res) != type(""):
pass
#: E721
if int == type(res):
pass
#: E721
if str != type(res):
pass
#: Okay
res.type("") == ""
#: Okay
Expand Down
Loading