Skip to content

Commit 463ac02

Browse files
authored
Ignore 'misspellings' due to string escapes (#2875)
1 parent 44b540e commit 463ac02

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

codespell_lib/_codespell.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,19 @@ def parse_file(
897897
word = match.group()
898898
lword = word.lower()
899899
if lword in misspellings:
900+
# Sometimes we find a 'misspelling' which is actually a valid word
901+
# preceded by a string escape sequence. Ignore such cases as
902+
# they're usually false alarms; see issue #17 among others.
903+
char_before_idx = match.start() - 1
904+
if (
905+
char_before_idx >= 0
906+
and line[char_before_idx] == "\\"
907+
# bell, backspace, formfeed, newline, carriage-return, tab, vtab.
908+
and word.startswith(("a", "b", "f", "n", "r", "t", "v"))
909+
and lword[1:] not in misspellings
910+
):
911+
continue
912+
900913
context_shown = False
901914
fix = misspellings[lword].fix
902915
fixword = fix_case(word, misspellings[lword].data)

codespell_lib/tests/test_basic.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ def test_basic(
9797
with fname.open("a") as f:
9898
f.write("this is a test file\n")
9999
assert cs.main(fname) == 0, "good"
100+
with fname.open("a") as f:
101+
f.write("var = '\\nDoes not error on newline'\n")
102+
assert cs.main(fname) == 0, "with string escape"
100103
with fname.open("a") as f:
101104
f.write("abandonned\n")
102105
assert cs.main(fname) == 1, "bad"

0 commit comments

Comments
 (0)