New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix empty line handling when formatting typing stubs #1646
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now on master Black enforces this formatting:
While the empty line between class B and C is right, I am not sure if the empty line between class A and class B is right since class A has an empty body. That empty line happens since Black can't tell the difference between the previous class not having an empty body or the current class being preceded by a decorator. Currently this commit keeps this behaviour since as far as I know classes don't have a reason to be decorated in stubs, but no idea whether that's right or not...
/cc @ambv @JelleZijlstra (I barely have any experience with typing stubs so your knowledge would be appreciated)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would put a blank line there. Nontrivial classes (with a body that's not
...
) should have an empty line on either side.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And there are some use cases for decorating classes in stubs, like
@runtime_checkable
for protocols.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I meant for the case of:
The empty line between class A and B doesn't seem right as A and B have empty bodies. Master and this commit enforces this formatting, but I feel like it's wrong since if that decorator wasn't there, that empty line would disappear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I see, I could go either way on that. I feel like if we do add blank lines, it should have blank lines both above and below it.