Skip to content

Commit

Permalink
Rebase Pyink to psf@d16a1db.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 516230302
  • Loading branch information
yilei authored and Copybara-Service committed Mar 13, 2023
1 parent af0d8cd commit 23c77ee
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 60 deletions.
76 changes: 38 additions & 38 deletions patches/pyink.patch
Expand Up @@ -349,7 +349,7 @@
--- a/linegen.py
+++ b/linegen.py
@@ -5,7 +5,7 @@ import sys
from dataclasses import dataclass, replace
from dataclasses import replace
from enum import Enum, auto
from functools import partial, wraps
-from typing import Collection, Iterator, List, Optional, Set, Union, cast
Expand All @@ -363,9 +363,9 @@
from pyink.lines import (
+ Indentation,
Line,
RHSResult,
append_leaves,
can_be_split,
@@ -80,6 +81,15 @@ LeafID = int
@@ -81,6 +82,15 @@ LeafID = int
LN = Union[Leaf, Node]


Expand All @@ -381,7 +381,7 @@
class CannotSplit(CannotTransform):
"""A readable split that fits the allotted line length is impossible."""

@@ -99,7 +109,9 @@ class LineGenerator(Visitor[Line]):
@@ -100,7 +110,9 @@ class LineGenerator(Visitor[Line]):
self.current_line: Line
self.__post_init__()

Expand All @@ -392,7 +392,7 @@
"""Generate a line.

If the line is empty, only emit if it makes sense.
@@ -108,11 +120,20 @@ class LineGenerator(Visitor[Line]):
@@ -109,11 +121,20 @@ class LineGenerator(Visitor[Line]):
If any lines were generated, set up a new current_line.
"""
if not self.current_line:
Expand All @@ -415,7 +415,7 @@
yield complete_line

def visit_default(self, node: LN) -> Iterator[Line]:
@@ -138,7 +159,9 @@ class LineGenerator(Visitor[Line]):
@@ -139,7 +160,9 @@ class LineGenerator(Visitor[Line]):
normalize_prefix(node, inside_brackets=any_open_brackets)
if self.mode.string_normalization and node.type == token.STRING:
node.value = normalize_string_prefix(node.value)
Expand All @@ -426,7 +426,7 @@
if node.type == token.NUMBER:
normalize_numeric_literal(node)
if node.type not in WHITESPACE:
@@ -148,7 +171,10 @@ class LineGenerator(Visitor[Line]):
@@ -149,7 +172,10 @@ class LineGenerator(Visitor[Line]):
def visit_test(self, node: Node) -> Iterator[Line]:
"""Visit an `x if y else z` test"""

Expand All @@ -438,7 +438,7 @@
already_parenthesized = (
node.prev_sibling and node.prev_sibling.type == token.LPAR
)
@@ -164,7 +190,7 @@ class LineGenerator(Visitor[Line]):
@@ -165,7 +191,7 @@ class LineGenerator(Visitor[Line]):
def visit_INDENT(self, node: Leaf) -> Iterator[Line]:
"""Increase indentation level, maybe yield a line."""
# In blib2to3 INDENT never holds comments.
Expand All @@ -447,7 +447,7 @@
yield from self.visit_default(node)

def visit_DEDENT(self, node: Leaf) -> Iterator[Line]:
@@ -179,7 +205,7 @@ class LineGenerator(Visitor[Line]):
@@ -180,7 +206,7 @@ class LineGenerator(Visitor[Line]):
yield from self.visit_default(node)

# Finally, emit the dedent.
Expand All @@ -456,7 +456,7 @@

def visit_stmt(
self, node: Node, keywords: Set[str], parens: Set[str]
@@ -275,9 +301,9 @@ class LineGenerator(Visitor[Line]):
@@ -276,9 +302,9 @@ class LineGenerator(Visitor[Line]):
if self.mode.is_pyi and is_stub_body(node):
yield from self.visit_default(node)
else:
Expand All @@ -468,7 +468,7 @@

else:
if (
@@ -367,10 +393,13 @@ class LineGenerator(Visitor[Line]):
@@ -368,10 +394,13 @@ class LineGenerator(Visitor[Line]):
yield from self.visit_default(node)

def visit_STRING(self, leaf: Leaf) -> Iterator[Line]:
Expand All @@ -484,7 +484,7 @@
# We're ignoring docstrings with backslash newline escapes because changing
# indentation of those changes the AST representation of the code.
if self.mode.string_normalization:
@@ -381,7 +410,9 @@ class LineGenerator(Visitor[Line]):
@@ -382,7 +411,9 @@ class LineGenerator(Visitor[Line]):
# formatting as visit_default() is called *after*. To avoid a
# situation where this function formats a docstring differently on
# the second pass, normalize it early.
Expand All @@ -495,7 +495,7 @@
else:
docstring = leaf.value
prefix = get_string_prefix(docstring)
@@ -395,7 +426,7 @@ class LineGenerator(Visitor[Line]):
@@ -396,7 +427,7 @@ class LineGenerator(Visitor[Line]):
quote_len = 1 if docstring[1] != quote_char else 3
docstring = docstring[quote_len:-quote_len]
docstring_started_empty = not docstring
Expand All @@ -504,7 +504,7 @@

if is_multiline_string(leaf):
docstring = fix_docstring(docstring, indent)
@@ -437,7 +468,11 @@ class LineGenerator(Visitor[Line]):
@@ -438,7 +469,11 @@ class LineGenerator(Visitor[Line]):
# If docstring is one line, we don't put the closing quotes on a
# separate line because it looks ugly (#3320).
lines = docstring.splitlines()
Expand All @@ -517,7 +517,7 @@

# If adding closing quotes would cause the last line to exceed
# the maximum line length then put a line break before the
@@ -476,7 +511,8 @@ class LineGenerator(Visitor[Line]):
@@ -477,7 +512,8 @@ class LineGenerator(Visitor[Line]):
self.visit_classdef = partial(v, keywords={"class"}, parens=Ø)
self.visit_expr_stmt = partial(v, keywords=Ø, parens=ASSIGNMENTS)
self.visit_return_stmt = partial(v, keywords={"return"}, parens={"return"})
Expand All @@ -527,7 +527,7 @@
self.visit_del_stmt = partial(v, keywords=Ø, parens={"del"})
self.visit_async_funcdef = self.visit_async_stmt
self.visit_decorated = self.visit_decorators
@@ -503,10 +539,11 @@ def transform_line(
@@ -504,10 +540,11 @@ def transform_line(

ll = mode.line_length
sn = mode.string_normalization
Expand All @@ -543,17 +543,17 @@

transformers: List[Transformer]
if (
@@ -692,8 +729,7 @@ def _first_right_hand_split(
@@ -682,8 +719,7 @@ def _first_right_hand_split(
omit: Collection[LeafID] = (),
) -> _RHSResult:
) -> RHSResult:
"""Split the line into head, body, tail starting with the last bracket pair.
-
- Note: this function should not have side effects. It's relied upon by
+ Note: this function should not have side effects. It's replied upon by
_maybe_split_omitting_optional_parens to get an opinion whether to prefer
splitting on the right side of an assignment statement.
"""
@@ -722,12 +758,53 @@ def _first_right_hand_split(
@@ -712,12 +748,53 @@ def _first_right_hand_split(
tail_leaves.reverse()
body_leaves.reverse()
head_leaves.reverse()
Expand Down Expand Up @@ -610,7 +610,7 @@
tail = bracket_split_build_line(
tail_leaves, line, opening_bracket, component=_BracketSplitComponent.tail
)
@@ -886,7 +963,7 @@ def bracket_split_build_line(
@@ -876,7 +953,7 @@ def bracket_split_build_line(
result = Line(mode=original.mode, depth=original.depth)
if component is _BracketSplitComponent.body:
result.inside_brackets = True
Expand All @@ -619,7 +619,7 @@
if leaves:
# Since body is a new indent level, remove spurious leading whitespace.
normalize_prefix(leaves[0], inside_brackets=True)
@@ -1444,7 +1521,7 @@ def generate_trailers_to_omit(line: Line
@@ -1434,7 +1511,7 @@ def generate_trailers_to_omit(line: Line
if not line.magic_trailing_comma:
yield omit

Expand All @@ -638,7 +638,7 @@
import sys
from dataclasses import dataclass, field
from typing import (
@@ -41,13 +43,28 @@ Index = int
@@ -42,13 +44,28 @@ Index = int
LeafID = int
LN = Union[Leaf, Node]

Expand Down Expand Up @@ -668,7 +668,7 @@
leaves: List[Leaf] = field(default_factory=list)
# keys ordered like `leaves`
comments: Dict[LeafID, List[Leaf]] = field(default_factory=dict)
@@ -56,6 +73,9 @@ class Line:
@@ -57,6 +74,9 @@ class Line:
should_split_rhs: bool = False
magic_trailing_comma: Optional[Leaf] = None

Expand All @@ -678,7 +678,7 @@
def append(
self, leaf: Leaf, preformatted: bool = False, track_bracket: bool = False
) -> None:
@@ -96,7 +116,7 @@ class Line:
@@ -97,7 +117,7 @@ class Line:
Raises ValueError when any `leaf` is appended after a standalone comment
or when a standalone comment is not the first leaf on the line.
"""
Expand All @@ -687,7 +687,7 @@
if self.is_comment:
raise ValueError("cannot append to standalone comments")

@@ -266,6 +286,20 @@ class Line:
@@ -272,6 +292,20 @@ class Line:

return False

Expand All @@ -708,7 +708,7 @@
def contains_multiline_strings(self) -> bool:
return any(is_multiline_string(leaf) for leaf in self.leaves)

@@ -433,7 +467,7 @@ class Line:
@@ -439,7 +473,7 @@ class Line:
if not self:
return "\n"

Expand All @@ -717,7 +717,7 @@
leaves = iter(self.leaves)
first = next(leaves)
res = f"{first.prefix}{indent}{first.value}"
@@ -484,7 +518,7 @@ class EmptyLineTracker:
@@ -501,7 +535,7 @@ class EmptyLineTracker:
mode: Mode
previous_line: Optional[Line] = None
previous_block: Optional[LinesBlock] = None
Expand All @@ -726,7 +726,7 @@
semantic_leading_comment: Optional[LinesBlock] = None

def maybe_empty_lines(self, current_line: Line) -> LinesBlock:
@@ -529,7 +563,7 @@ class EmptyLineTracker:
@@ -546,7 +580,7 @@ class EmptyLineTracker:

def _maybe_empty_lines(self, current_line: Line) -> Tuple[int, int]:
max_allowed = 1
Expand All @@ -735,7 +735,7 @@
max_allowed = 1 if self.mode.is_pyi else 2
if current_line.leaves:
# Consume the first leaf's extra newlines.
@@ -540,7 +574,7 @@ class EmptyLineTracker:
@@ -557,7 +591,7 @@ class EmptyLineTracker:
else:
before = 0
depth = current_line.depth
Expand All @@ -744,7 +744,7 @@
if self.mode.is_pyi:
assert self.previous_line is not None
if depth and not current_line.is_def and self.previous_line.is_def:
@@ -579,8 +613,9 @@ class EmptyLineTracker:
@@ -596,8 +630,9 @@ class EmptyLineTracker:
if (
self.previous_line
and self.previous_line.is_import
Expand All @@ -756,7 +756,7 @@
):
return (before or 1), 0

@@ -591,6 +626,14 @@ class EmptyLineTracker:
@@ -608,6 +643,14 @@ class EmptyLineTracker:
):
return before, 1

Expand All @@ -771,7 +771,7 @@
if self.previous_line and self.previous_line.opens_block:
return 0, 0
return before, 0
@@ -611,15 +654,16 @@ class EmptyLineTracker:
@@ -628,15 +671,16 @@ class EmptyLineTracker:

return 0, 0

Expand All @@ -791,7 +791,7 @@
and before == 0
):
slc = self.semantic_leading_comment
@@ -636,9 +680,9 @@ class EmptyLineTracker:
@@ -653,9 +697,9 @@ class EmptyLineTracker:

if self.mode.is_pyi:
if current_line.is_class or self.previous_line.is_class:
Expand All @@ -803,7 +803,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
@@ -656,7 +700,7 @@ class EmptyLineTracker:
@@ -673,7 +717,7 @@ class EmptyLineTracker:
# Blank line between a block of functions (maybe with preceding
# decorators) and a block of non-functions
newlines = 1
Expand All @@ -812,7 +812,7 @@
newlines = 1
else:
newlines = 0
@@ -714,10 +758,14 @@ def is_line_short_enough( # noqa: C901
@@ -731,10 +775,14 @@ def is_line_short_enough( # noqa: C901
"""
if not line_str:
line_str = line_to_string(line)
Expand All @@ -828,7 +828,7 @@
and "\n" not in line_str # multiline strings
and not line.contains_standalone_comments()
)
@@ -726,7 +774,7 @@ def is_line_short_enough( # noqa: C901
@@ -743,7 +791,7 @@ def is_line_short_enough( # noqa: C901
return False
if "\n" not in line_str:
# No multiline strings (MLS) present
Expand All @@ -837,7 +837,7 @@

first, *_, last = line_str.split("\n")
if len(first) > mode.line_length or len(last) > mode.line_length:
@@ -899,7 +947,7 @@ def can_omit_invisible_parens(
@@ -933,7 +981,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 @@ -846,7 +846,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:
@@ -923,7 +971,7 @@ def _can_omit_opening_paren(line: Line,
@@ -957,7 +1005,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

0 comments on commit 23c77ee

Please sign in to comment.