-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Remove newline after code block open #3035
Changes from all commits
df60609
13f2aaf
8d6c373
b991571
549a094
c30a951
17516d7
bb97364
466767d
2922f44
10a299c
a9eaf0f
f2a5b65
700c51d
fd1da5c
c845c0c
f8bbdbb
6fe0a98
4fd907a
b2a015c
b6e720c
e496d90
860941b
f44edba
24b48ca
92ce12c
ee5888b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -148,6 +148,7 @@ Multiple contributions by: | |
- [Rishikesh Jha](mailto:[email protected]) | ||
- [Rupert Bedford](mailto:[email protected]) | ||
- Russell Davis | ||
- [Sagi Shadur](mailto:[email protected]) | ||
- [Rémi Verschelde](mailto:[email protected]) | ||
- [Sami Salonen](mailto:[email protected]) | ||
- [Samuel Cormier-Iijima](mailto:[email protected]) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,3 +49,28 @@ plain strings. User-made splits are respected when they do not exceed the line l | |
limit. Line continuation backslashes are converted into parenthesized strings. | ||
Unnecessary parentheses are stripped. The stability and status of this feature is | ||
tracked in [this issue](https://github.com/psf/black/issues/2188). | ||
|
||
### Removing trailing newlines after code block open | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh lovely, I've been worried lately our style documentation is out of date with the current state of affairs, but this quells my worries with this PR ^^ |
||
|
||
_Black_ will remove trailing newlines after code block openings. That means that the | ||
following code: | ||
|
||
```python | ||
def my_func(): | ||
|
||
print("The line above me will be deleted!") | ||
|
||
print("But the line above me won't!") | ||
``` | ||
|
||
Will be changed to: | ||
|
||
```python | ||
def my_func(): | ||
print("The line above me will be deleted!") | ||
|
||
print("But the line above me won't!") | ||
``` | ||
|
||
This new feature will be applied to **all code blocks**: `def`, `class`, `if`, `for`, | ||
`while`, `with`, `case` and `match`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
import random | ||
|
||
|
||
def foo1(): | ||
|
||
print("The newline above me should be deleted!") | ||
|
||
|
||
def foo2(): | ||
|
||
|
||
|
||
print("All the newlines above me should be deleted!") | ||
|
||
|
||
def foo3(): | ||
|
||
print("No newline above me!") | ||
|
||
print("There is a newline above me, and that's OK!") | ||
|
||
|
||
def foo4(): | ||
|
||
# There is a comment here | ||
|
||
print("The newline above me should not be deleted!") | ||
|
||
|
||
class Foo: | ||
def bar(self): | ||
|
||
print("The newline above me should be deleted!") | ||
|
||
|
||
for i in range(5): | ||
|
||
print(f"{i}) The line above me should be removed!") | ||
|
||
|
||
for i in range(5): | ||
|
||
|
||
|
||
print(f"{i}) The lines above me should be removed!") | ||
|
||
|
||
for i in range(5): | ||
|
||
for j in range(7): | ||
|
||
print(f"{i}) The lines above me should be removed!") | ||
|
||
|
||
if random.randint(0, 3) == 0: | ||
|
||
print("The new line above me is about to be removed!") | ||
|
||
|
||
if random.randint(0, 3) == 0: | ||
|
||
|
||
|
||
|
||
print("The new lines above me is about to be removed!") | ||
|
||
|
||
if random.randint(0, 3) == 0: | ||
if random.uniform(0, 1) > 0.5: | ||
print("Two lines above me are about to be removed!") | ||
|
||
|
||
while True: | ||
|
||
print("The newline above me should be deleted!") | ||
|
||
|
||
while True: | ||
|
||
|
||
|
||
print("The newlines above me should be deleted!") | ||
|
||
|
||
while True: | ||
|
||
while False: | ||
|
||
print("The newlines above me should be deleted!") | ||
|
||
|
||
with open("/path/to/file.txt", mode="w") as file: | ||
|
||
file.write("The new line above me is about to be removed!") | ||
|
||
|
||
with open("/path/to/file.txt", mode="w") as file: | ||
|
||
|
||
|
||
file.write("The new lines above me is about to be removed!") | ||
|
||
|
||
with open("/path/to/file.txt", mode="r") as read_file: | ||
|
||
with open("/path/to/output_file.txt", mode="w") as write_file: | ||
|
||
write_file.writelines(read_file.readlines()) | ||
|
||
# output | ||
|
||
import random | ||
|
||
|
||
def foo1(): | ||
print("The newline above me should be deleted!") | ||
|
||
|
||
def foo2(): | ||
print("All the newlines above me should be deleted!") | ||
|
||
|
||
def foo3(): | ||
print("No newline above me!") | ||
|
||
print("There is a newline above me, and that's OK!") | ||
|
||
|
||
def foo4(): | ||
# There is a comment here | ||
|
||
print("The newline above me should not be deleted!") | ||
|
||
|
||
class Foo: | ||
def bar(self): | ||
print("The newline above me should be deleted!") | ||
|
||
|
||
for i in range(5): | ||
print(f"{i}) The line above me should be removed!") | ||
|
||
|
||
for i in range(5): | ||
print(f"{i}) The lines above me should be removed!") | ||
|
||
|
||
for i in range(5): | ||
for j in range(7): | ||
print(f"{i}) The lines above me should be removed!") | ||
|
||
|
||
if random.randint(0, 3) == 0: | ||
print("The new line above me is about to be removed!") | ||
|
||
|
||
if random.randint(0, 3) == 0: | ||
print("The new lines above me is about to be removed!") | ||
|
||
|
||
if random.randint(0, 3) == 0: | ||
if random.uniform(0, 1) > 0.5: | ||
print("Two lines above me are about to be removed!") | ||
|
||
|
||
while True: | ||
print("The newline above me should be deleted!") | ||
|
||
|
||
while True: | ||
print("The newlines above me should be deleted!") | ||
|
||
|
||
while True: | ||
while False: | ||
print("The newlines above me should be deleted!") | ||
|
||
|
||
with open("/path/to/file.txt", mode="w") as file: | ||
file.write("The new line above me is about to be removed!") | ||
|
||
|
||
with open("/path/to/file.txt", mode="w") as file: | ||
file.write("The new lines above me is about to be removed!") | ||
|
||
|
||
with open("/path/to/file.txt", mode="r") as read_file: | ||
with open("/path/to/output_file.txt", mode="w") as write_file: | ||
write_file.writelines(read_file.readlines()) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
def http_status(status): | ||
|
||
match status: | ||
|
||
case 400: | ||
|
||
return "Bad request" | ||
|
||
case 401: | ||
|
||
return "Unauthorized" | ||
|
||
case 403: | ||
|
||
return "Forbidden" | ||
|
||
case 404: | ||
|
||
return "Not found" | ||
|
||
# output | ||
def http_status(status): | ||
match status: | ||
case 400: | ||
return "Bad request" | ||
|
||
case 401: | ||
return "Unauthorized" | ||
|
||
case 403: | ||
return "Forbidden" | ||
|
||
case 404: | ||
return "Not found" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1455,7 +1455,6 @@ def test_newline_comment_interaction(self) -> None: | |
black.assert_stable(source, output, mode=DEFAULT_MODE) | ||
|
||
def test_bpo_2142_workaround(self) -> None: | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Haha, I like it! |
||
# https://bugs.python.org/issue2142 | ||
|
||
source, _ = read_data("miscellaneous", "missing_final_newline") | ||
|
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.
This AUTHORS list is so out of date, gosh. Good for you adding yourself!