Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore 'misspellings' due to string escapes #2875

Merged
merged 2 commits into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,19 @@ def parse_file(
word = match.group()
lword = word.lower()
if lword in misspellings:
# Sometimes we find a 'misspelling' which is actually a valid word
# preceded by a string escape sequence. Ignore such cases as
# they're usually false alarms; see issue #17 among others.
char_before_idx = match.start() - 1
if (
char_before_idx >= 0
and line[char_before_idx] == "\\"
# bell, backspace, formfeed, newline, carriage-return, tab, vtab.
and word.startswith(("a", "b", "f", "n", "r", "t", "v"))
Comment on lines +906 to +908
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't deal with anything where someone has put in e.g. \ to render a backslash (or \\ etc) and the following (unrelated) word is a typo.

and lword[1:] not in misspellings
):
continue

context_shown = False
fix = misspellings[lword].fix
fixword = fix_case(word, misspellings[lword].data)
Expand Down
3 changes: 3 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def test_basic(
with fname.open("a") as f:
f.write("this is a test file\n")
assert cs.main(fname) == 0, "good"
with fname.open("a") as f:
f.write("var = '\\nDoes not error on newline'\n")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test relied on ndoes being in the dictionary, which I don't think it is.

I also found when adding a positive test that it doesn't match/trigger anyway because of the apostrophe at the start.

I've added some more thorough tests in #2879 .

assert cs.main(fname) == 0, "with string escape"
with fname.open("a") as f:
f.write("abandonned\n")
assert cs.main(fname) == 1, "bad"
Expand Down