Do not report a fix that --ignore-regex made impossible - #3994
Open
Eljees wants to merge 1 commit into
Open
Conversation
Detection runs on the text with --ignore-regex substituted out, while --write-changes substitutes on the original line. A word that only exists in the substituted text (1nd in 1nd_2nd, with --ignore-regex _) therefore matches nothing: the line is left untouched, but codespell still prints FIXED, still lists the change, and still exits 0. Keep the result of the substitution and only claim the fix when the line actually changed; otherwise fall through to the normal warning, which also restores the exit code. Fixes codespell-project#2056
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.
Fixes #2056.
What happens
Line 2 is reported as fixed, is not fixed, and does not affect the exit code — so a CI run goes green with the misspelling still in the file, and running codespell again prints the same false
FIXED:forever.Why
Detection and writing look at two different texts. Detection walks the line with
--ignore-regexsubstituted out (_ignore_word_subreplaces the match with a space), so it sees1nd 2ndand finds1nd.--write-changesthen substitutes on the original line:In
1nd_2ndthere is no word boundary between1ndand_, sore.submatches nothing. Butchangedis set before the result is inspected,changes_maderecords a change that never happened, andcontinueskips the warning branch that would have set the exit code.The change
Keep the result of the substitution, and only claim the fix when the line really changed; otherwise fall through to the normal warning.
--ignore-regexsemantics are untouched — the word is still detected, it is just reported instead of silently dropped. This is the second of the two outcomes the reporter said would be acceptable ("either fix it, or tell me it can't").After:
Tests
test_ignore_regex_with_write_changesincodespell_lib/tests/test_basic.py, next to the existing--ignore-regextests. It asserts all three symptoms: line 1 is rewritten, line 2 is not listed as fixed, and the exit code is non-zero. Onmainit fails on the middle assertion.pytest codespell_lib/tests/test_basic.py: 85 passed onmain, 86 passed with this change, no failures either way.ruff check,ruff format --checkandmypyare clean (mypy reports the same two pre-existingchardetstub errors on an untouched tree).