Skip to content

Commit 742228c

Browse files
committed
re-allow decorated one-liners
1 parent 99f354d commit 742228c

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

pycodestyle.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,41 @@ def maximum_line_length(physical_line, max_line_length, multiline,
314314
########################################################################
315315

316316

317+
def _is_one_liner(logical_line, indent_level, lines, line_number):
318+
if not STARTSWITH_TOP_LEVEL_REGEX.match(logical_line):
319+
return False
320+
321+
line_idx = line_number - 1
322+
323+
if line_idx < 1:
324+
prev_indent = 0
325+
else:
326+
prev_indent = expand_indent(lines[line_idx - 1])
327+
328+
if prev_indent > indent_level:
329+
return False
330+
331+
while line_idx < len(lines):
332+
line = lines[line_idx].strip()
333+
if not line.startswith('@') and STARTSWITH_TOP_LEVEL_REGEX.match(line):
334+
break
335+
else:
336+
line_idx += 1
337+
else:
338+
return False # invalid syntax: EOF while searching for def/class
339+
340+
next_idx = line_idx + 1
341+
while next_idx < len(lines):
342+
if lines[next_idx].strip():
343+
break
344+
else:
345+
next_idx += 1
346+
else:
347+
return True # line is last in the file
348+
349+
return expand_indent(lines[next_idx]) <= indent_level
350+
351+
317352
@register_check
318353
def blank_lines(logical_line, blank_lines, indent_level, line_number,
319354
blank_before, previous_logical,
@@ -360,16 +395,11 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
360395
):
361396
yield 0, "E303 too many blank lines (%d)" % blank_lines
362397
elif STARTSWITH_TOP_LEVEL_REGEX.match(logical_line):
363-
# If this is a one-liner (i.e. this is not a decorator and the
364-
# next line is not more indented), and the previous line is also
365-
# not deeper (it would be better to check if the previous line
366-
# is part of another def/class at the same level), don't require
367-
# blank lines around this.
368-
prev_line = lines[line_number - 2] if line_number >= 2 else ''
369-
next_line = lines[line_number] if line_number < len(lines) else ''
370-
if (not logical_line.startswith("@") and
371-
expand_indent(prev_line) <= indent_level and
372-
expand_indent(next_line) <= indent_level):
398+
# allow a group of one-liners
399+
if (
400+
_is_one_liner(logical_line, indent_level, lines, line_number) and
401+
blank_before == 0
402+
):
373403
return
374404
if indent_level:
375405
if not (blank_before == method_lines or

testsuite/E30not.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,32 @@ def foo():
177177
# for no E30x being emitted.
178178
def bar(): pass
179179
def baz(): pass
180+
#: E704:8:1 E704:10:1
181+
from typing import overload
182+
from typing import Union
183+
184+
185+
# This emits the (ignored-by-default) E704, but here we're testing
186+
# for no E30x being emitted.
187+
@overload
188+
def f(x: int) -> int: ...
189+
@overload
190+
def f(x: str) -> str: ...
191+
192+
193+
def f(x: Union[int, str]) -> Union[int, str]:
194+
return x
195+
#: E704:8:5 E704:10:5
196+
from typing import Protocol
197+
198+
199+
class C(Protocol):
200+
# This emits the (ignored-by-default) E704, but here we're testing
201+
# for no E30x being emitted.
202+
@property
203+
def f(self) -> int: ...
204+
@property
205+
def g(self) -> str: ...
180206
#: Okay
181207
#!python
182208
# -*- coding: utf-8 -*-

0 commit comments

Comments
 (0)