From 463ac02940b09c8238662516eecdeb82c7ca3be1 Mon Sep 17 00:00:00 2001 From: Zac Hatfield-Dodds Date: Wed, 14 Jun 2023 09:43:16 -0700 Subject: [PATCH] Ignore 'misspellings' due to string escapes (#2875) --- codespell_lib/_codespell.py | 13 +++++++++++++ codespell_lib/tests/test_basic.py | 3 +++ 2 files changed, 16 insertions(+) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 6726a58733..c71850191d 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -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")) + 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 89c6ed305c..b16a2d82b7 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -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") + assert cs.main(fname) == 0, "with string escape" with fname.open("a") as f: f.write("abandonned\n") assert cs.main(fname) == 1, "bad"