Raise AssertionError if `dst` reformats differently the second time.
(
src: str, dst: str, mode: Mode, *, lines: Collection[tuple[int, int]] = ()
)
| 1664 | |
| 1665 | |
| 1666 | def assert_stable( |
| 1667 | src: str, dst: str, mode: Mode, *, lines: Collection[tuple[int, int]] = () |
| 1668 | ) -> None: |
| 1669 | """Raise AssertionError if `dst` reformats differently the second time.""" |
| 1670 | if lines: |
| 1671 | # Formatting specified lines requires `adjusted_lines` to map original lines |
| 1672 | # to the formatted lines before re-formatting the previously formatted result. |
| 1673 | # Due to less-ideal diff algorithm, some edge cases produce incorrect new line |
| 1674 | # ranges. Hence for now, we skip the stable check. |
| 1675 | # See https://github.com/psf/black/issues/4033 for context. |
| 1676 | return |
| 1677 | # We shouldn't call format_str() here, because that formats the string |
| 1678 | # twice and may hide a bug where we bounce back and forth between two |
| 1679 | # versions. |
| 1680 | newdst = _format_str_once(dst, mode=mode, lines=lines) |
| 1681 | if dst != newdst: |
| 1682 | log = dump_to_file( |
| 1683 | str(mode), |
| 1684 | diff(src, dst, "source", "first pass"), |
| 1685 | diff(dst, newdst, "first pass", "second pass"), |
| 1686 | ) |
| 1687 | raise AssertionError( |
| 1688 | f"INTERNAL ERROR: {_black_info()} produced different code on the second" |
| 1689 | " pass of the formatter. Please report a bug on" |
| 1690 | f" https://github.com/psf/black/issues. This diff might be helpful: {log}" |
| 1691 | ) from None |
| 1692 | |
| 1693 | |
| 1694 | def patched_main() -> None: |
no test coverage detected