Skip to content

Commit

Permalink
Cherry pick psf#4028.
Browse files Browse the repository at this point in the history
This keeps two blank lines between the module docstring and the following def/class when there are no other code in between.

Why cherrypick not rebase? Pyink is not yet ready to rebase to Black 23.11.0, I'm waiting for psf#4012 to be merged as Pyink has local patches related to hugging parens.

Also added two missing patches.

PiperOrigin-RevId: 582704838
  • Loading branch information
yilei authored and Copybara-Service committed Nov 15, 2023
1 parent 5edc4e2 commit 3431f23
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
50 changes: 39 additions & 11 deletions patches/pyink.patch
Expand Up @@ -794,7 +794,15 @@
leaves = iter(self.leaves)
first = next(leaves)
res = f"{first.prefix}{indent}{first.value}"
@@ -591,7 +625,7 @@ class EmptyLineTracker:
@@ -561,6 +595,7 @@ class EmptyLineTracker:
and self.previous_block.previous_block is None
and len(self.previous_block.original_line.leaves) == 1
and self.previous_block.original_line.is_triple_quoted_string
+ and not (current_line.is_class or current_line.is_def)
):
before = 1

@@ -591,7 +626,7 @@ class EmptyLineTracker:

def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
max_allowed = 1
Expand All @@ -803,7 +811,7 @@
max_allowed = 1 if self.mode.is_pyi else 2
if current_line.leaves:
# Consume the first leaf's extra newlines.
@@ -606,7 +640,7 @@ class EmptyLineTracker:
@@ -606,7 +641,7 @@ class EmptyLineTracker:
depth = current_line.depth

previous_def = None
Expand All @@ -812,7 +820,7 @@
previous_def = self.previous_defs.pop()

if previous_def is not None:
@@ -655,10 +689,25 @@ class EmptyLineTracker:
@@ -655,10 +690,25 @@ class EmptyLineTracker:

if (
self.previous_line
Expand Down Expand Up @@ -840,7 +848,7 @@
):
return (before or 1), 0

@@ -671,6 +720,14 @@ class EmptyLineTracker:
@@ -671,6 +721,14 @@ class EmptyLineTracker:
return 0, 1
return before, 1

Expand All @@ -855,7 +863,7 @@
if self.previous_line and self.previous_line.opens_block:
return 0, 0
return before, 0
@@ -691,15 +748,16 @@ class EmptyLineTracker:
@@ -691,15 +749,16 @@ class EmptyLineTracker:

return 0, 0

Expand All @@ -875,7 +883,7 @@
and before == 0
):
slc = self.semantic_leading_comment
@@ -716,9 +774,9 @@ class EmptyLineTracker:
@@ -716,9 +775,9 @@ class EmptyLineTracker:

if self.mode.is_pyi:
if current_line.is_class or self.previous_line.is_class:
Expand All @@ -887,7 +895,7 @@
newlines = 1
elif current_line.is_stub_class and self.previous_line.is_stub_class:
# No blank line between classes with an empty body
@@ -747,7 +805,7 @@ class EmptyLineTracker:
@@ -747,7 +806,7 @@ class EmptyLineTracker:
# Blank line between a block of functions (maybe with preceding
# decorators) and a block of non-functions
newlines = 1
Expand All @@ -896,7 +904,7 @@
newlines = 1
else:
newlines = 0
@@ -815,10 +873,14 @@ def is_line_short_enough( # noqa: C901
@@ -815,10 +874,14 @@ def is_line_short_enough( # noqa: C901
line_str = line_to_string(line)

width = str_width if mode.preview else len
Expand All @@ -912,7 +920,7 @@
and "\n" not in line_str # multiline strings
and not line.contains_standalone_comments()
)
@@ -827,7 +889,7 @@ def is_line_short_enough( # noqa: C901
@@ -827,7 +890,7 @@ def is_line_short_enough( # noqa: C901
return False
if "\n" not in line_str:
# No multiline strings (MLS) present
Expand All @@ -921,7 +929,7 @@

first, *_, last = line_str.split("\n")
if width(first) > mode.line_length or width(last) > mode.line_length:
@@ -1017,7 +1079,7 @@ def can_omit_invisible_parens(
@@ -1017,7 +1080,7 @@ def can_omit_invisible_parens(
def _can_omit_opening_paren(line: Line, *, first: Leaf, line_length: int) -> bool:
"""See `can_omit_invisible_parens`."""
remainder = False
Expand All @@ -930,7 +938,7 @@
_index = -1
for _index, leaf, leaf_length in line.enumerate_with_length():
if leaf.type in CLOSING_BRACKETS and leaf.opening_bracket is first:
@@ -1041,7 +1103,7 @@ def _can_omit_opening_paren(line: Line,
@@ -1041,7 +1104,7 @@ def _can_omit_opening_paren(line: Line,

def _can_omit_closing_paren(line: Line, *, last: Leaf, line_length: int) -> bool:
"""See `can_omit_invisible_parens`."""
Expand Down Expand Up @@ -1435,6 +1443,26 @@

class TestDeFactoAPI:
"""Test that certain symbols that are commonly used externally keep working.
--- a/tests/util.py
+++ b/tests/util.py
@@ -246,6 +246,8 @@ def get_flags_parser() -> argparse.Argum
" version works correctly."
),
)
+ parser.add_argument("--pyink", default=False, action="store_true")
+ parser.add_argument("--pyink-indentation", default=4, type=int, choices=[2, 4])
return parser


@@ -260,6 +262,8 @@ def parse_mode(flags_line: str) -> TestC
is_ipynb=args.ipynb,
magic_trailing_comma=not args.skip_magic_trailing_comma,
preview=args.preview,
+ is_pyink=args.pyink,
+ pyink_indentation=args.pyink_indentation,
)
return TestCaseArgs(mode=mode, fast=args.fast, minimum_version=args.minimum_version)

--- a/tox.ini
+++ b/tox.ini
@@ -95,4 +95,4 @@ setenv = PYTHONPATH = {toxinidir}/src
Expand Down
1 change: 1 addition & 0 deletions src/pyink/lines.py
Expand Up @@ -595,6 +595,7 @@ def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
and self.previous_block.previous_block is None
and len(self.previous_block.original_line.leaves) == 1
and self.previous_block.original_line.is_triple_quoted_string
and not (current_line.is_class or current_line.is_def)
):
before = 1

Expand Down

0 comments on commit 3431f23

Please sign in to comment.