Ignore 'misspellings' due to string escapes (#2875)
diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 6726a58..c718501 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py
@@ -897,6 +897,19 @@ 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")) + and lword[1:] not in misspellings + ): + continue + context_shown = False fix = misspellings[lword].fix fixword = fix_case(word, misspellings[lword].data)
diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 89c6ed3..b16a2d8 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py
@@ -98,6 +98,9 @@ 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") + assert cs.main(fname) == 0, "with string escape" + with fname.open("a") as f: f.write("abandonned\n") assert cs.main(fname) == 1, "bad" with fname.open("a") as f: