Add an inline or standalone comment to the line.
(self, comment: Leaf)
| 378 | return False |
| 379 | |
| 380 | def append_comment(self, comment: Leaf) -> bool: |
| 381 | """Add an inline or standalone comment to the line.""" |
| 382 | if ( |
| 383 | comment.type == STANDALONE_COMMENT |
| 384 | and self.bracket_tracker.any_open_brackets() |
| 385 | ): |
| 386 | comment.prefix = "" |
| 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 = "" |
| 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 | # Comments on an optional parens wrapping a single leaf should belong to |
| 406 | # the wrapped node except if it's a type comment. Pinning the comment like |
| 407 | # this avoids unstable formatting caused by comment migration. |
| 408 | if len(self.leaves) < 2: |
| 409 | comment.type = STANDALONE_COMMENT |
| 410 | comment.prefix = "" |
| 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 | """Generate comments that should appear directly after `leaf`.""" |
no test coverage detected