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

fix E721 types regex #1041

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 4 additions & 3 deletions pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ def lru_cache(maxsize=128): # noqa as it's a fake implementation.
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'(?:[=!]=|is(?:\s+not)?)\s+type(?:\s*\(\s*([^)]*[^ )])\s*\))' +
r'|type(?:\s*\(\s*([^)]*[^ )])\s*\))\s+(?:[=!]=|is(?:\s+not)?)'
)
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 @@ -1463,7 +1465,6 @@ def comparison_type(logical_line, noqa):
common base class, basestring, so you can do:

Okay: if isinstance(obj, basestring):
Okay: if type(a1) is type(b1):
"""
match = COMPARE_TYPE_REGEX.search(logical_line)
if match and not noqa:
Expand Down
9 changes: 6 additions & 3 deletions testsuite/E72.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#: E721
if type(res) != type(""):
pass
#: E721
#: Okay
import types

if res == types.IntType:
Expand Down Expand Up @@ -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 @@ -80,3 +78,8 @@ def func_histype(a, b, c):
pass
except Exception:
pass
#: Okay
from . import custom_types as types

Copy link
Member

Choose a reason for hiding this comment

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

These test files aren't ever executed so we don't need a real module backing them here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

thanks, removed custom_types.py in 5df259b

red = types.ColorTypeRED
red is types.ColorType.RED
7 changes: 7 additions & 0 deletions testsuite/custom_types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import enum


class ColorType(enum.Enum):
RED = 1
GREEN = 2
BLUE = 3