Skip to content

Commit

Permalink
Merge pull request #1086 from PyCQA/E721-allow-is-v2
Browse files Browse the repository at this point in the history
allow `is` and `is not` for type comparisons (E721)
  • Loading branch information
asottile committed Jul 15, 2023
2 parents 9a0da24 + ac223bd commit 2326e16
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 16 deletions.
15 changes: 5 additions & 10 deletions pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,10 @@
r'\s*(?(1)|(None|False|True))\b')
COMPARE_NEGATIVE_REGEX = re.compile(r'\b(?<!is\s)(not)\s+[^][)(}{ ]+\s+'
r'(in|is)\s')
COMPARE_TYPE_REGEX = re.compile(r'(?:[=!]=|is(?:\s+not)?)\s+type(?:s.\w+Type'
r'|\s*\(\s*([^)]*[^ )])\s*\))')
COMPARE_TYPE_REGEX = re.compile(
r'[=!]=\s+type(?:\s*\(\s*([^)]*[^ )])\s*\))'
r'|\btype(?:\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*)')
LAMBDA_REGEX = re.compile(r'\blambda\b')
Expand Down Expand Up @@ -1457,14 +1459,7 @@ def comparison_type(logical_line, noqa):
Do not compare types directly.
Okay: if isinstance(obj, int):
E721: if type(obj) is type(1):
When checking if an object is a string, keep in mind that it might
be a unicode string too! In Python 2.3, str and unicode have a
common base class, basestring, so you can do:
Okay: if isinstance(obj, basestring):
Okay: if type(a1) is type(b1):
E721: if type(obj) == type(1):
"""
match = COMPARE_TYPE_REGEX.search(logical_line)
if match and not noqa:
Expand Down
15 changes: 9 additions & 6 deletions testsuite/E72.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
#: E721
if type(res) != type(""):
pass
#: E721
#: Okay
import types

if res == types.IntType:
pass
#: E721
#: Okay
import types

if type(res) is not types.ListType:
Expand All @@ -26,9 +26,9 @@
assert type(res) == type((0))
#: E721
assert type(res) != type((1, ))
#: E721
#: Okay
assert type(res) is type((1, ))
#: E721
#: Okay
assert type(res) is not type((1, ))
#: E211 E721
assert type(res) == type ([2, ])
Expand All @@ -47,8 +47,6 @@
pass
if isinstance(res, types.MethodType):
pass
if type(a) != type(b) or type(a) == type(ccc):
pass
#: Okay
def func_histype(a, b, c):
pass
Expand Down Expand Up @@ -81,6 +79,11 @@ def func_histype(a, b, c):
except Exception:
pass
#: Okay
from . import custom_types as types

red = types.ColorTypeRED
red is types.ColorType.RED
#: Okay
from . import compute_type

if compute_type(foo) == 5:
Expand Down

0 comments on commit 2326e16

Please sign in to comment.