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

Method append_comment

src/black/lines.py:380–415  ·  src/black/lines.py::Line.append_comment

Add an inline or standalone comment to the line.

(self, comment: Leaf)

Source from the content-addressed store, hash-verified

378 return False
379
380 def append_comment(self, comment: Leaf) -> bool:
381 class="st">""class="st">"Add an inline or standalone comment to the line."class="st">""
382 if (
383 comment.type == STANDALONE_COMMENT
384 and self.bracket_tracker.any_open_brackets()
385 ):
386 comment.prefix = class="st">""
387 return False
388
389 if comment.type != token.COMMENT:
390 return False
391
392 if not self.leaves:
393 comment.type = STANDALONE_COMMENT
394 comment.prefix = class="st">""
395 return False
396
397 last_leaf = self.leaves[-1]
398 if (
399 last_leaf.type == token.RPAR
400 and not last_leaf.value
401 and last_leaf.parent
402 and len(list(last_leaf.parent.leaves())) <= 3
403 and not is_type_comment(comment, mode=self.mode)
404 ):
405 class="cm"># Comments on an optional parens wrapping a single leaf should belong to
406 class="cm"># the wrapped node except if it's a type comment. Pinning the comment like
407 class="cm"># this avoids unstable formatting caused by comment migration.
408 if len(self.leaves) < 2:
409 comment.type = STANDALONE_COMMENT
410 comment.prefix = class="st">""
411 return False
412
413 last_leaf = self.leaves[-2]
414 self.comments.setdefault(id(last_leaf), []).append(comment)
415 return True
416
417 def comments_after(self, leaf: Leaf) -> list[Leaf]:
418 class="st">""class="st">"Generate comments that should appear directly after `leaf`."class="st">""

Callers 1

appendMethod · 0.95

Calls 4

is_type_commentFunction · 0.90
any_open_bracketsMethod · 0.80
leavesMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected