Insert empty line between suite and alternative branch after def/class (#12294)

When there is a function or class definition at the end of a suite
followed by the beginning of an alternative block, we have to insert a
single empty line between them.

In the if-else-statement example below, we insert an empty line after
the `foo` in the if-block, but none after the else-block `foo`, since in
the latter case the enclosing suite already adds empty lines.

```python
if sys.version_info >= (3, 10):
    def foo():
        return "new"
else:
    def foo():
        return "old"
class Bar:
    pass
```

To do so, we track whether the current suite is the last one in the
current statement with a new option on the suite kind.

Fixes #12199

---------

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
konsti 2024-07-15 12:59:33 +02:00 committed by GitHub
parent ecd4b4d943
commit 9a817a2922
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 699 additions and 149 deletions

View file

@ -505,5 +505,3 @@ def foo():
def bar():
pass
```

View file

@ -220,5 +220,3 @@ else:
with hmm_but_this_should_get_two_preceding_newlines():
pass
```

View file

@ -255,5 +255,3 @@ class Conditional:
def l(self): ...
def m(self): ...
```

View file

@ -255,6 +255,68 @@ if True:
print()
if True:
def a():
return 1
else:
pass
if True:
# fmt: off
def a():
return 1
# fmt: on
else:
pass
match True:
case 1:
def a():
return 1
case 1:
def a():
return 1
try:
def a():
return 1
except RuntimeError:
def a():
return 1
try:
def a():
return 1
finally:
def a():
return 1
try:
def a():
return 1
except RuntimeError:
def a():
return 1
except ZeroDivisionError:
def a():
return 1
else:
def a():
return 1
finally:
def a():
return 1
if raw:
def show_file(lines):
for line in lines:
pass
# Trailing comment not on function or class
else:
pass
# NOTE: Please keep this the last block in this file. This tests that we don't insert
# empty line(s) at the end of the file due to nested function
if True:
@ -558,6 +620,85 @@ if True:
print()
if True:
def a():
return 1
else:
pass
if True:
# fmt: off
def a():
return 1
# fmt: on
else:
pass
match True:
case 1:
def a():
return 1
case 1:
def a():
return 1
try:
def a():
return 1
except RuntimeError:
def a():
return 1
try:
def a():
return 1
finally:
def a():
return 1
try:
def a():
return 1
except RuntimeError:
def a():
return 1
except ZeroDivisionError:
def a():
return 1
else:
def a():
return 1
finally:
def a():
return 1
if raw:
def show_file(lines):
for line in lines:
pass
# Trailing comment not on function or class
else:
pass
# NOTE: Please keep this the last block in this file. This tests that we don't insert
# empty line(s) at the end of the file due to nested function
if True:
@ -593,4 +734,63 @@ def overload4(a: int): ...
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -277,6 +277,7 @@
def a():
return 1
+
else:
pass
@@ -293,6 +294,7 @@
def a():
return 1
+
case 1:
def a():
@@ -303,6 +305,7 @@
def a():
return 1
+
except RuntimeError:
def a():
@@ -313,6 +316,7 @@
def a():
return 1
+
finally:
def a():
@@ -323,18 +327,22 @@
def a():
return 1
+
except RuntimeError:
def a():
return 1
+
except ZeroDivisionError:
def a():
return 1
+
else:
def a():
return 1
+
finally:
def a():
```

View file

@ -160,11 +160,17 @@ def f():
pass
if True:
def a():
return 1
else:
pass
# comment
x = 1
```
## Output
@ -302,10 +308,28 @@ x = 1
def f():
pass
if True:
def a():
return 1
else:
pass
# comment
x = 1
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -134,6 +134,7 @@
if True:
def a():
return 1
+
else:
pass
```

View file

@ -1042,3 +1042,26 @@ def func[T](
lotsoflongargs5: T,
) -> T: ...
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -161,6 +161,7 @@
def f1():
pass # a
+
else:
pass
@@ -170,6 +171,7 @@
def f2():
pass
# a
+
else:
pass
```

View file

@ -609,4 +609,22 @@ if parent_body:
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -93,11 +93,13 @@
def f():
pass
# 1
+
elif True:
def f():
pass
# 2
+
else:
def f():
```

View file

@ -366,4 +366,28 @@ finally:
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -117,16 +117,19 @@
def f():
pass
# a
+
except:
def f():
pass
# b
+
else:
def f():
pass
# c
+
finally:
def f():
```

View file

@ -311,4 +311,24 @@ class ComplexStatements:
```
## Preview changes
```diff
--- Stable
+++ Preview
@@ -132,6 +132,7 @@
if sys.version_info >= (3, 12):
@classmethod
def fromtimestamp(cls, timestamp: float, tz: float | None = ...) -> Self: ...
+
else:
@classmethod
def fromtimestamp(cls, __timestamp: float, tz: float | None = ...) -> Self: ...
@@ -141,6 +142,7 @@
if sys.version_info >= (3, 8):
@classmethod
def now(cls, tz: float | None = None) -> Self: ...
+
else:
@classmethod
def now(cls, tz: None = None) -> Self: ...
```