Skip to content

Commit 0138bb1

Browse files
committed
Allow omitting blank lines around one-liner definitions.
1 parent 3d37ea0 commit 0138bb1

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
@@ -354,6 +354,16 @@ def blank_lines(logical_line, blank_lines, indent_level, line_number,
354354
):
355355
yield 0, "E303 too many blank lines (%d)" % blank_lines
356356
elif STARTSWITH_TOP_LEVEL_REGEX.match(logical_line):
357+
# If this is a one-liner (i.e. the next line is not more
358+
# indented), and the previous line is also not deeper
359+
# (it would be better to check if the previous line is part
360+
# of another def/class at the same level), don't require blank
361+
# lines around this.
362+
prev_line = lines[line_number - 2] if line_number >= 2 else ''
363+
next_line = lines[line_number] if line_number < len(lines) else ''
364+
if (expand_indent(prev_line) <= indent_level and
365+
expand_indent(next_line) <= indent_level):
366+
return
357367
if indent_level:
358368
if not (blank_before == method_lines or
359369
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)