Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow is and is not for type comparisons (E721) #1086

Merged
merged 2 commits into from
Jul 15, 2023
Merged
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
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'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't find the rationale for removing types.*Type comparisons in the linked issues. As far as I can tell, those should still be included in the checks, shouldn't they?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think those got removed because the name types isn't necessarily the stdlib types module -- not 100% sure though

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the original patch is here: #1041 -- this is just a revert-revert of that with an additional commit on top

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose the precise reasoning is the following, then:

This check also breaks many ligimitate cases (such as having your own types module with an Enum class for instance, and comparing a value to the enum member).

which I can relate to. You can, of course, also shadow the built-in type function, but that's just not something people do in general and having a custom types module is much more likely, although also ending your enum in Type is most likely less common.

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