Skip to content

Commit db80d07

Browse files
authored
Merge pull request #823 from anntzer/allow-no-blanks-around-one-liners
Allow omitting blank lines around one-liner definitions.
2 parents 8940d64 + 0138bb1 commit db80d07

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

pycodestyle.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,16 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
357357
):
358358
yield 0, "E303 too many blank lines (%d)" % blank_lines
359359
elif STARTSWITH_TOP_LEVEL_REGEX.match(logical_line):
360+
# If this is a one-liner (i.e. the next line is not more
361+
# indented), and the previous line is also not deeper
362+
# (it would be better to check if the previous line is part
363+
# of another def/class at the same level), don't require blank
364+
# lines around this.
365+
prev_line = lines[line_number - 2] if line_number >= 2 else ''
366+
next_line = lines[line_number] if line_number < len(lines) else ''
367+
if (expand_indent(prev_line) <= indent_level and
368+
expand_indent(next_line) <= indent_level):
369+
return
360370
if indent_level:
361371
if not (blank_before == method_lines or
362372
previous_indent_level < indent_level or

testsuite/E30.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,21 @@ async def x():
163163

164164
async def x(y: int = 1):
165165
pass
166+
#: E704:3:1 E302:3:1
167+
def bar():
168+
pass
169+
def baz(): pass
170+
#: E704:1:1 E302:2:1
171+
def bar(): pass
172+
def baz():
173+
pass
174+
#: E704:4:5 E306:4:5
175+
def foo():
176+
def bar():
177+
pass
178+
def baz(): pass
179+
#: E704:2:5 E306:3:5
180+
def foo():
181+
def bar(): pass
182+
def baz():
183+
pass

testsuite/E30not.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,14 @@ class Bar(object):
162162
def foo(x):
163163
classification = x
164164
definitely = not classification
165+
#: E704:3:1 E704:4:1
166+
# This emits the (ignored-by-default) E704, but here we're testing
167+
# for no E30x being emitted.
168+
def bar(): pass
169+
def baz(): pass
170+
#: E704:4:5 E704:5:5
171+
def foo():
172+
# This emits the (ignored-by-default) E704, but here we're testing
173+
# for no E30x being emitted.
174+
def bar(): pass
175+
def baz(): pass

0 commit comments

Comments
 (0)