Repository navigation
Conversation
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.
Summary
Preserve Python source encodings when
ChangedFile.apply()writes a refactoring result, and encode the complete result before opening the destination so an encoding error cannot truncate the original file.Currently,
open(path, 'w', newline='')uses the platform default encoding. For example, renaming a variable in a Latin-1 file containingcaféwrites different bytes under a UTF-8 locale, while retaining the Latin-1 coding declaration. Under a Windows GBK locale, even a UTF-8 source file can be silently rewritten as GBK. Characters that cannot be encoded can also raise after the destination has already been truncated.The change derives the encoding from the refactored source's PEP 263 declaration / UTF-8 BOM using
tokenize.detect_encoding. It defaults to UTF-8 for ordinary Python source, retains a single BOM, pre-encodes the text, and writes the bytes without newline conversion. It uses the current source buffer, so refactorings can still create a file whose path does not exist yet.Tests
Ten new regression cases exercise:
On the original code, all ten cases fail on Windows / CPython 3.13.5 with a GBK locale. After the fix:
python -m pytest test/test_api/test_refactoring.py test/test_integration.py -k refactor -q --tb=short: 116 passed, 2263 deselected.git diff --checkpassed.The typeshed and django-stubs submodules were initialized at their pinned revisions. The entire completion/inference suite and other interpreter/platform combinations were not run.
Added a changelog entry and Alex0AI to AUTHORS.txt as requested by CONTRIBUTING.md.
This investigation, implementation, tests and PR description were prepared with OpenAI Codex assistance.