Repository navigation
Conversation
When a module ends with a from-import and has no trailing newline, such as `from os import system`, Worder.is_from_aliased() looks for an `as` keyword one character past the end of the source. The `min(..., len(self.code))` clamp still points one past the last character, so _find_word_start() raises IndexError. Rename and find_occurrences() crash whenever they scan such a module. is_import_statement_aliased_module() got an _has_enough_len_for_as() guard for the same situation in 2021, but is_from_aliased() never did. Add the same guard there, with tests for both Worder methods and for a rename across modules where the middle module ends without a newline. Fixes: #119
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #928 +/- ##
=======================================
Coverage 95.34% 95.34%
=======================================
Files 134 134
Lines 26768 26789 +21
=======================================
+ Hits 25522 25543 +21
Misses 1246 1246 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
mcepl
requested review from
lieryan
and
a balanced review from Copilot
and removed request for
Copilot
November 2, 2026 23:03
This branch has not been deployed
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.
Description
Worder.is_from_aliased()raisedIndexErrorwhen a module ends with a from-import and has no trailing newline, for example a module containing onlyfrom os import system. It looks for anaskeyword after the imported name, and themin(..., len(self.code))clamp still points one past the last character, so_find_word_start()readsself.code[len(self.code)]. Rename andfind_occurrences()crash whenever they scan such a module, which is the traceback in #119.is_import_statement_aliased_module()already has an_has_enough_len_for_as()guard for the same situation (added in bd08f09), butis_from_aliased()never got one. This adds the same guard.Fixes #119
Checklist (delete if not relevant):
Tests added:
WordRangeFinderTest.test_is_from_aliased_at_end_of_file_without_newlinefails without the fix.WordRangeFinderTest.test_is_import_statement_aliased_module_at_end_of_file_without_newlinecovers the existing guard, so the two methods stay consistent.RenameRefactoringTest.test_renaming_from_import_at_end_of_file_without_newlinerenames a function across three modules where the middle one ends without a newline. It fails without the fix.The full test suite passes locally on Python 3.13: 2158 passed, 7 skipped, 5 xfailed.