Repository navigation
Preserve encoding and line endings with --overwrite-in-place - #2743
Merged
Merged
Conversation
The in-place writer buffered the output in StringIO(newline=None),
which turns CRLF and CR into LF, and wrote it back with
path.open("w"), which uses the locale encoding and os.linesep, instead
of the encoding detected when the file was read. A file declaring
another encoding was rewritten in the locale one, the UTF-8 BOM was
dropped, CRLF line endings became LF (LF became CRLF on Windows), and
when the locale encoding could not represent the content the file was
left empty, which matches what PyCQA#2199 reported on Windows.
Keep newlines in the buffer as they are, encode the output with the
detected encoding before opening the file and write the bytes, the
same bytes the .isorted temporary file gets. An encoding error now
leaves the file as it was.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2743 +/- ##
==========================================
- Coverage 99.38% 99.38% -0.01%
==========================================
Files 40 40
Lines 3274 3273 -1
Branches 706 706
==========================================
- Hits 3254 3253 -1
Misses 12 12
Partials 8 8 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
🟢 Approved
The focused implementation preserves the source format and adds regression coverage for the reported failures, with no unresolved findings.
No open issues 🎉
What changed in this PR
This PR makes --overwrite-in-place preserve source encoding and line endings, aligning its output with the default file-writing mode.
Changes:
- Preserve line endings in the in-memory output buffer.
- Encode output using the source encoding before opening the file, preventing truncation on encoding errors.
- Add regression tests for LF/CRLF, Latin-1, UTF-8 BOM, and file preservation after encoding failures.
| File | Description |
|---|---|
| tests/unit/test_api.py | Adds byte-level checks for encoding, line endings, and encoding-error safety. |
| isort/api.py | Preserves buffered newlines and writes pre-encoded bytes in overwrite mode. |
Review effort: Balanced
DanielNoord
approved these changes
Oct 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
--overwrite-in-place, isort writes the sorted file back in the locale encoding and with the platform's line endings, not the ones the file had. On Linux with 9.0.2, a CRLF file comes back with LF and a latin-1 file with a coding cookie is rewritten in UTF-8:A UTF-8 BOM is dropped too, while the default mode keeps all three. On Windows the same write turns LF into CRLF and uses the ANSI code page unless Python runs in UTF-8 mode. When a character doesn't fit, the write fails after the file was truncated, so isort warns, exits 0 and leaves the file empty, which matches #2199.
The in-place writer buffers the output in
StringIO(newline=None)and writes it back withpath.open("w"):isort/isort/api.py
Lines 356 to 363 in a514c71
while the default mode opens its
.isortedfile withencoding=source_file.encoding, newline="". The buffer now keeps the newlines and the output is encoded withsource_file.encodingbefore the file is opened, so both modes write the same bytes, and an encoding error (a configured heading the file's encoding can't hold, for example) leaves the file as it was.