Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
17 changes: 16 additions & 1 deletion codespell_lib/_spellchecker.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
Copyright (C) 2010-2011 Lucas De Marchi <lucas.de.marchi@gmail.com>
Copyright (C) 2011 ProFUSION embedded systems
"""
import sys
Comment thread
AlightSoulmate marked this conversation as resolved.
Outdated

# Pass all misspellings through this translation table to generate
# alternative misspellings and fixes.
Expand Down Expand Up @@ -54,7 +55,21 @@ def build_dict(
with open(filename, encoding="utf-8") as f:
translate_tables = [(x, str.maketrans(x, y)) for x, y in alt_chars]
for line in f:
[key, data] = line.split("->")
left, pound, _ = line.partition("#")
if pound and left and left[-1] not in (' ', '\t'):
print(
f"WARNING: {filename}: missing spaces before #: {line.rstrip()!r}",
file=sys.stderr,
)
continue

line = left.strip()
if not line:
continue
try:
[key, data] = line.split("->")
Comment thread
AlightSoulmate marked this conversation as resolved.
Outdated
except ValueError:
continue
# TODO: For now, convert both to lower.
# Someday we can maybe add support for fixing caps.
key = key.lower()
Expand Down
32 changes: 32 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1524,3 +1524,35 @@ def test_args_from_file(
print("Testing with direct call to cs_.main()")
r = cs_.main(*args[1:])
print(f"{r=}")


def test_dict_comments(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test dictionary comments and blank lines."""
fname = tmp_path / "bad.txt"
fname.write_text("abandonned\noccured\n")

dictionary = tmp_path / "test.txt"
dictionary.write_text(
"#comment\n"
"# comment\n"
" #comment\n"
"\n"
"\r\n"
"abandonned->abandoned # inline comment\n"
"occured->occurred# invalid inline comment\n"
"abil#ity->ability # hash in illegal position\n",
encoding="utf-8",
)

# Allow valid inline comments.
# Skip entries where '#' is not preceded by whitespace.
result = cs.main("-D", dictionary, fname, std=True)
assert isinstance(result, tuple)
code, stdout, stderr = result
assert code == 1
assert "abandonned ==> abandoned" in stdout
assert "occured ==> occurred" not in stdout
assert "missing spaces before #" in stderr