Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix empty line handling when formatting typing stubs (#1646)
Black used to erroneously remove all empty lines between non-function code and decorators when formatting typing stubs. Now a single empty line is enforced. I chose for putting empty lines around decorated classes that have empty bodies since removing empty lines around such classes would cause a formatting issue that seems to be impossible to fix. For example: ``` class A: ... @some_decorator class B: ... class C: ... class D: ... @some_other_decorator def foo(): -> None: ... ``` It is easy to enforce no empty lines between class A, B, and C. Just return 0, 0 for a line that is a decorator and precedes an stub class. Fortunately before this commit, empty lines after that class would be removed already. Now let's look at the empty line between class D and function foo. In this case, there should be an empty line there since it's class code next to function code. The problem is that when deciding to add X empty lines before a decorator, you can't tell whether it's before a class or a function. If the decorator is before a function, then an empty line is needed, while no empty lines are needed when the decorator is before a class. So even though I personally prefer no empty lines around decorated classes, I had to go the other way surrounding decorated classes with empty lines. Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
- Loading branch information
1 parent
ecc1f17
commit c0a8e42
Showing
5 changed files
with
82 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,65 @@ | ||
def f(): ... | ||
from typing import Union | ||
|
||
@bird | ||
def zoo(): ... | ||
|
||
class A: ... | ||
@bar | ||
class B: | ||
def BMethod(self) -> None: ... | ||
@overload | ||
def BMethod(self, arg : List[str]) -> None: ... | ||
|
||
class C: ... | ||
@hmm | ||
class D: ... | ||
class E: ... | ||
|
||
@baz | ||
def foo() -> None: | ||
... | ||
|
||
class F (A , C): ... | ||
def spam() -> None: ... | ||
|
||
@overload | ||
def spam(arg: str) -> str: ... | ||
|
||
var : int = 1 | ||
|
||
def eggs() -> Union[str, int]: ... | ||
|
||
def g(): ... | ||
# output | ||
def f(): ... | ||
def g(): ... | ||
|
||
from typing import Union | ||
|
||
@bird | ||
def zoo(): ... | ||
|
||
class A: ... | ||
|
||
@bar | ||
class B: | ||
def BMethod(self) -> None: ... | ||
@overload | ||
def BMethod(self, arg: List[str]) -> None: ... | ||
|
||
class C: ... | ||
|
||
@hmm | ||
class D: ... | ||
|
||
class E: ... | ||
|
||
@baz | ||
def foo() -> None: ... | ||
|
||
class F(A, C): ... | ||
|
||
def spam() -> None: ... | ||
@overload | ||
def spam(arg: str) -> str: ... | ||
|
||
var: int = 1 | ||
|
||
def eggs() -> Union[str, int]: ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters