MCPcopy
hub / github.com/psf/black / append

Method append

src/black/lines.py:53–92  ·  src/black/lines.py::Line.append

Add a new `leaf` to the end of the line. Unless `preformatted` is True, the `leaf` will receive a new consistent whitespace prefix and metadata applied by :class:`BracketTracker`. Trailing commas are maybe removed, unpacked for loop variables are demoted from being d

(
        self, leaf: Leaf, preformatted: bool = False, track_bracket: bool = False
    )

Source from the content-addressed store, hash-verified

51 magic_trailing_comma: Leaf | None = None
52
53 def append(
54 self, leaf: Leaf, preformatted: bool = False, track_bracket: bool = False
55 ) -> None:
56 class="st">"""Add a new `leaf` to the end of the line.
57
58 Unless `preformatted` is True, the `leaf` will receive a new consistent
59 whitespace prefix and metadata applied by :class:`BracketTracker`.
60 Trailing commas are maybe removed, unpacked for loop variables are
61 demoted from being delimiters.
62
63 Inline comments are put aside.
64 class="st">"""
65 has_value = (
66 leaf.type in BRACKETS
67 class="cm"># empty fstring and tstring middles must not be truncated
68 or leaf.type in (token.FSTRING_MIDDLE, token.TSTRING_MIDDLE)
69 or bool(leaf.value.strip())
70 )
71 if not has_value:
72 return
73
74 if leaf.type == token.COLON and self.is_class_paren_empty:
75 del self.leaves[-2:]
76 if self.leaves and not preformatted:
77 class="cm"># Note: at this point leaf.prefix should be empty except for
78 class="cm"># imports, for which we only preserve newlines.
79 leaf.prefix += whitespace(
80 leaf,
81 complex_subscript=self.is_complex_subscript(leaf),
82 mode=self.mode,
83 )
84 if self.inside_brackets or not preformatted or track_bracket:
85 self.bracket_tracker.mark(leaf)
86 if self.mode.magic_trailing_comma:
87 if self.has_magic_trailing_comma(leaf):
88 self.magic_trailing_comma = leaf
89 elif self.has_magic_trailing_comma(leaf):
90 self.remove_trailing_comma()
91 if not self.append_comment(leaf):
92 self.leaves.append(leaf)
93
94 def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None:
95 class="st">"""Like :func:`append()` but disallow invalid standalone comment structure.

Callers 15

append_safeMethod · 0.95
bracket_split_build_lineFunction · 0.95
append_to_lineFunction · 0.95
do_transformMethod · 0.95
replace_cell_magicsFunction · 0.45
replace_magicsFunction · 0.45
_get_str_argsFunction · 0.45
visit_AssignMethod · 0.45
visit_ExprMethod · 0.45
parse_line_rangesFunction · 0.45

Calls 6

is_complex_subscriptMethod · 0.95
remove_trailing_commaMethod · 0.95
append_commentMethod · 0.95
whitespaceFunction · 0.90
markMethod · 0.80

Tested by

no test coverage detected