Skip to content

Commit

Permalink
Implemented #1638 / #1644: Provide a flag to ensure same file handle …
Browse files Browse the repository at this point in the history
…is used after sorting.
  • Loading branch information
timothycrosley committed Mar 5, 2021
1 parent 07b32fe commit e09bbbc
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,7 @@ Find out more about isort's release policy [here](https://pycqa.github.io/isort/
- Implemented #1661: Added "wemake" profile.
- Implemented #1669: Parallel (`-j`) now defaults to number of CPU cores if no value is provided.
- Implemented #1668: Added a safeguard against accidental usage against /.
- Implemented #1638 / #1644: Provide a flag `--overwrite-in-place` to ensure same file handle is used after sorting.

### 5.7.0 December 30th 2020
- Fixed #1612: In rare circumstances an extra comma is added after import and before comment.
Expand Down
5 changes: 4 additions & 1 deletion isort/api.py
Expand Up @@ -384,7 +384,10 @@ def sort_file(
):
return False
source_file.stream.close()
tmp_file.replace(source_file.path)
if config.overwrite_in_place:
source_file.path.write_bytes(tmp_file.read_bytes())
else:
tmp_file.replace(source_file.path)
if not config.quiet:
print(f"Fixing {source_file.path}")
finally:
Expand Down
8 changes: 8 additions & 0 deletions isort/main.py
Expand Up @@ -207,6 +207,14 @@ def _build_arg_parser() -> argparse.ArgumentParser:
dest="write_to_stdout",
action="store_true",
)
general_group.add_argument(
"--overwrite-in-place",
help="Tells isort to overwrite in place using the same file handle."
"Comes at a performance and memory usage penalty over it's standard "
"approach but ensures all file flags and modes stay unchanged.",
dest="overwrite_in_place",
action="store_true",
)
general_group.add_argument(
"--show-config",
dest="show_config",
Expand Down
1 change: 1 addition & 0 deletions isort/settings.py
Expand Up @@ -206,6 +206,7 @@ class _Config:
follow_links: bool = True
indented_import_headings: bool = True
honor_case_in_force_sorted_sections: bool = False
overwrite_in_place: bool = False

def __post_init__(self):
py_version = self.py_version
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_api.py
Expand Up @@ -34,6 +34,11 @@ def test_sort_file(imperfect) -> None:
assert imperfect.read() == fixed_content


def test_sort_file_in_place(imperfect) -> None:
assert api.sort_file(imperfect, overwrite_in_place=True)
assert imperfect.read() == fixed_content


def test_sort_file_to_stdout(capsys, imperfect) -> None:
assert api.sort_file(imperfect, write_to_stdout=True)
out, _ = capsys.readouterr()
Expand Down
1 change: 1 addition & 0 deletions tests/unit/test_main.py
Expand Up @@ -78,6 +78,7 @@ def test_parse_args():
assert main.parse_args(["--csi"]) == {"combine_straight_imports": True}
assert main.parse_args(["--combine-straight-imports"]) == {"combine_straight_imports": True}
assert main.parse_args(["--dont-follow-links"]) == {"follow_links": False}
assert main.parse_args(["--overwrite-in-place"]) == {"overwrite_in_place": True}


def test_ascii_art(capsys):
Expand Down

0 comments on commit e09bbbc

Please sign in to comment.