Reformat a single file under `src` without spawning child processes. `fast`, `write_back`, and `mode` options are passed to :func:`format_file_in_place` or :func:`format_stdin_to_stdout`.
(
src: Path,
fast: bool,
write_back: WriteBack,
mode: Mode,
report: "Report",
*,
lines: Collection[tuple[int, int]] = (),
no_cache: bool = False,
)
| 892 | # not ideal, but this shouldn't cause any issues ... hopefully. ~ichard26 |
| 893 | @mypyc_attr(patchable=True) |
| 894 | def reformat_one( |
| 895 | src: Path, |
| 896 | fast: bool, |
| 897 | write_back: WriteBack, |
| 898 | mode: Mode, |
| 899 | report: "Report", |
| 900 | *, |
| 901 | lines: Collection[tuple[int, int]] = (), |
| 902 | no_cache: bool = False, |
| 903 | ) -> None: |
| 904 | """Reformat a single file under `src` without spawning child processes. |
| 905 | |
| 906 | `fast`, `write_back`, and `mode` options are passed to |
| 907 | :func:`format_file_in_place` or :func:`format_stdin_to_stdout`. |
| 908 | """ |
| 909 | try: |
| 910 | changed = Changed.NO |
| 911 | |
| 912 | if str(src) == "-": |
| 913 | is_stdin = True |
| 914 | elif str(src).startswith(STDIN_PLACEHOLDER): |
| 915 | is_stdin = True |
| 916 | # Use the original name again in case we want to print something |
| 917 | # to the user |
| 918 | src = Path(str(src)[len(STDIN_PLACEHOLDER) :]) |
| 919 | else: |
| 920 | is_stdin = False |
| 921 | |
| 922 | if is_stdin: |
| 923 | if src.suffix == ".pyi": |
| 924 | mode = replace(mode, is_pyi=True) |
| 925 | elif src.suffix == ".ipynb": |
| 926 | mode = replace(mode, is_ipynb=True) |
| 927 | if format_stdin_to_stdout( |
| 928 | fast=fast, write_back=write_back, mode=mode, lines=lines |
| 929 | ): |
| 930 | changed = Changed.YES |
| 931 | else: |
| 932 | cache = None if no_cache else Cache.read(mode) |
| 933 | if cache is not None and write_back not in ( |
| 934 | WriteBack.DIFF, |
| 935 | WriteBack.COLOR_DIFF, |
| 936 | ): |
| 937 | if not cache.is_changed(src): |
| 938 | changed = Changed.CACHED |
| 939 | if changed is not Changed.CACHED and format_file_in_place( |
| 940 | src, fast=fast, write_back=write_back, mode=mode, lines=lines |
| 941 | ): |
| 942 | changed = Changed.YES |
| 943 | if cache is not None and ( |
| 944 | (write_back is WriteBack.YES and changed is not Changed.CACHED) |
| 945 | or (write_back is WriteBack.CHECK and changed is Changed.NO) |
| 946 | ): |
| 947 | cache.write([src]) |
| 948 | report.done(src, changed) |
| 949 | except Exception as exc: |
| 950 | if report.verbose: |
| 951 | traceback.print_exc() |
no test coverage detected