Skip to content
Open
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
7 changes: 6 additions & 1 deletion codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -1094,7 +1094,12 @@ def parse_lines(

if options.write_changes and fix:
changed = True
lines[i] = re.sub(rf"\b{word}\b", fixword, lines[i])
# Not \b: the word regex admits a trailing apostrophe, and a
# \b after one can never hold, so the substitution silently
# matched nothing.
lines[i] = re.sub(
rf"(?<!\w){re.escape(word)}(?!\w)", fixword, lines[i]
)
fixed_words.add(word)
changes_made.append((line_number + 1, word, fixword))
continue
Expand Down
16 changes: 16 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,22 @@ def test_default_word_parsing(
assert cs.main(fname) == 1, "misspelling containing typographic apostrophe U+2019"


def test_write_changes_trailing_apostrophe(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
"""A misspelling ending in an apostrophe must actually be written out."""
fname = tmp_path / "trailing_apostrophe"

fname.write_text("dont' go\n", encoding="utf-8") # U+0027
cs.main("-w", fname, count=False)
assert fname.read_text(encoding="utf-8") == "don't go\n"

fname.write_text("dont’ go\n", encoding="utf-8") # U+2019 # noqa: RUF001
cs.main("-w", fname, count=False)
assert fname.read_text(encoding="utf-8") == "don’t go\n" # noqa: RUF001


def test_bad_glob(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
Expand Down
Loading