Skip to content

Commit

Permalink
Allow up to two empty lines on module level and single empty lines ot…
Browse files Browse the repository at this point in the history
…herwise

Fixes #74
  • Loading branch information
ambv committed Mar 27, 2018
1 parent 1f445a0 commit e5f8251
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ always emit an extra empty line after ``return``, ``raise``, ``break``,
``continue``, and ``yield``. This is to make changes in control flow
more prominent to readers of your code.

*Black* will allow single empty lines left by the original editors,
except when they're added within parenthesized expressions. Since such
expressions are always reformatted to fit minimal space, this whitespace
is lost.
*Black* will allow single empty lines inside functions, and single and
double empty lines on module level left by the original editors, except
when they're within parenthesized expressions. Since such expressions
are always reformatted to fit minimal space, this whitespace is lost.

It will also insert proper spacing before and after function definitions.
It's one line before and after inner functions and two lines before and
Expand Down Expand Up @@ -338,6 +338,9 @@ More details can be found in [CONTRIBUTING](CONTRIBUTING.md).
* fixed 18.3a4 regression: don't crash and burn on empty lines with
trailing whitespace (#80)

* only allow up to two empty lines on module level and only single empty
lines within functions (#74)


### 18.3a4

Expand Down
4 changes: 2 additions & 2 deletions black.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,13 +754,13 @@ def maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:

def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
max_allowed = 1
if current_line.is_comment and current_line.depth == 0:
if current_line.depth == 0:
max_allowed = 2
if current_line.leaves:
# Consume the first leaf's extra newlines.
first_leaf = current_line.leaves[0]
before = first_leaf.prefix.count('\n')
before = min(before, max(before, max_allowed))
before = min(before, max_allowed)
first_leaf.prefix = ''
else:
before = 0
Expand Down
4 changes: 4 additions & 0 deletions tests/empty_lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@ def f():
if t == token.COMMENT: # another trailing comment
return DOUBLESPACE


assert p is not None, f"INTERNAL ERROR: hand-made leaf without parent: {leaf!r}"


prev = leaf.prev_sibling
if not prev:
prevp = preceding_leaf(p)
if not prevp or prevp.type in OPENING_BRACKETS:


return NO


if prevp.type == token.EQUAL:
if prevp.parent and prevp.parent.type in {
syms.typedargslist,
Expand Down

0 comments on commit e5f8251

Please sign in to comment.