Skip to content

Commit

Permalink
add new error E204 for whitespace after decorator @
Browse files Browse the repository at this point in the history
  • Loading branch information
asottile committed Jun 15, 2024
1 parent d343c39 commit 3cedd4c
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
2 changes: 2 additions & 0 deletions docs/intro.rst
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ This is the current list of error and warning codes:
+------------+----------------------------------------------------------------------+
| E203 | whitespace before ',', ';', or ':' |
+------------+----------------------------------------------------------------------+
| E204 | whitespace after decorator '@' |
+------------+----------------------------------------------------------------------+
+------------+----------------------------------------------------------------------+
| E211 | whitespace before '(' |
+------------+----------------------------------------------------------------------+
Expand Down
7 changes: 7 additions & 0 deletions pycodestyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@
ERRORCODE_REGEX = re.compile(r'\b[A-Z]\d{3}\b')
DOCSTRING_REGEX = re.compile(r'u?r?["\']')
EXTRANEOUS_WHITESPACE_REGEX = re.compile(r'[\[({][ \t]|[ \t][\]}),;:](?!=)')
WHITESPACE_AFTER_DECORATOR_REGEX = re.compile(r'@\s')
WHITESPACE_AFTER_COMMA_REGEX = re.compile(r'[,;:]\s*(?: |\t)')
COMPARE_SINGLETON_REGEX = re.compile(r'(\bNone|\bFalse|\bTrue)?\s*([=!]=)'
r'\s*(?(1)|(None|False|True))\b')
Expand Down Expand Up @@ -438,6 +439,9 @@ def extraneous_whitespace(logical_line):
E203: if x == 4: print x, y; x, y = y , x
E203: if x == 4: print x, y ; x, y = y, x
E203: if x == 4 : print x, y; x, y = y, x
Okay: @decorator
E204: @ decorator
"""
line = logical_line
for match in EXTRANEOUS_WHITESPACE_REGEX.finditer(line):
Expand All @@ -451,6 +455,9 @@ def extraneous_whitespace(logical_line):
code = ('E202' if char in '}])' else 'E203') # if char in ',;:'
yield found, f"{code} whitespace before '{char}'"

if WHITESPACE_AFTER_DECORATOR_REGEX.match(logical_line):
yield 1, "E204 whitespace after decorator '@'"


@register_check
def whitespace_around_keywords(logical_line):
Expand Down
17 changes: 17 additions & 0 deletions testing/data/E20.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,21 @@
x, y = y, x
a[b1, :] == a[b1, ...]
b = a[:, b1]
#: E204:1:2
@ decorator
def f():
pass
#: E204:1:2
@ decorator
def f():
pass
#: E204:1:2
@ decorator
def f():
pass
#: E204:2:6
if True:
@ decorator
def f():
pass
#:

0 comments on commit 3cedd4c

Please sign in to comment.