Like :func:`append()` but disallow invalid standalone comment structure. Raises ValueError when any `leaf` is appended after a standalone comment or when a standalone comment is not the first leaf on the line.
(self, leaf: Leaf, preformatted: bool = False)
| 92 | self.leaves.append(leaf) |
| 93 | |
| 94 | def append_safe(self, leaf: Leaf, preformatted: bool = False) -> None: |
| 95 | """Like :func:`append()` but disallow invalid standalone comment structure. |
| 96 | |
| 97 | Raises ValueError when any `leaf` is appended after a standalone comment |
| 98 | or when a standalone comment is not the first leaf on the line. |
| 99 | """ |
| 100 | if ( |
| 101 | self.bracket_tracker.depth == 0 |
| 102 | or self.bracket_tracker.any_open_for_or_lambda() |
| 103 | ): |
| 104 | if self.is_comment: |
| 105 | raise ValueError("cannot append to standalone comments") |
| 106 | |
| 107 | if self.leaves and leaf.type == STANDALONE_COMMENT: |
| 108 | raise ValueError( |
| 109 | "cannot append standalone comments to a populated line" |
| 110 | ) |
| 111 | |
| 112 | self.append(leaf, preformatted=preformatted) |
| 113 | |
| 114 | @property |
| 115 | def is_comment(self) -> bool: |
no test coverage detected