Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Case ignore #3272

Merged
merged 8 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
46 changes: 36 additions & 10 deletions codespell_lib/_codespell.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,24 +655,37 @@ def parse_options(
return options, parser, used_cfg_files


def parse_ignore_words_option(ignore_words_option: List[str]) -> Set[str]:
def parse_ignore_words_option(
ignore_words_option: List[str],
) -> Tuple[Set[str], Set[str]]:
ignore_words: Set[str] = set()
ignore_words_cased: Set[str] = set()
if ignore_words_option:
for comma_separated_words in ignore_words_option:
ignore_words.update(
word.strip() for word in comma_separated_words.split(",")
)
return ignore_words
for word in comma_separated_words.split(","):
word = word.strip()
if word == word.lower():
ignore_words.add(word)
else:
ignore_words_cased.add(word)
return (ignore_words, ignore_words_cased)
DimitriPapadopoulos marked this conversation as resolved.
Show resolved Hide resolved


def build_exclude_hashes(filename: str, exclude_lines: Set[str]) -> None:
with open(filename, encoding="utf-8") as f:
exclude_lines.update(line.rstrip() for line in f)


def build_ignore_words(filename: str, ignore_words: Set[str]) -> None:
def build_ignore_words(
filename: str, ignore_words: Set[str], ignore_words_cased: Set[str]
) -> None:
with open(filename, encoding="utf-8") as f:
ignore_words.update(line.strip() for line in f)
for line in f:
word = line.strip()
if word == word.lower():
ignore_words.add(word)
else:
ignore_words_cased.add(word)
DimitriPapadopoulos marked this conversation as resolved.
Show resolved Hide resolved


def add_misspelling(
Expand Down Expand Up @@ -865,6 +878,7 @@ def parse_file(
colors: TermColors,
summary: Optional[Summary],
misspellings: Dict[str, Misspelling],
ignore_words_cased: Set[str],
exclude_lines: Set[str],
file_opener: FileOpener,
word_regex: Pattern[str],
Expand All @@ -885,6 +899,8 @@ def parse_file(
else:
if options.check_filenames:
for word in extract_words(filename, word_regex, ignore_word_regex):
if word in ignore_words_cased:
continue
lword = word.lower()
if lword not in misspellings:
continue
Expand Down Expand Up @@ -958,6 +974,8 @@ def parse_file(
)
for match in check_matches:
word = match.group()
if word in ignore_words_cased:
continue
lword = word.lower()
if lword in misspellings:
# Sometimes we find a 'misspelling' which is actually a valid word
Expand Down Expand Up @@ -1112,7 +1130,10 @@ def main(*args: str) -> int:
ignore_word_regex = None

ignore_words_files = options.ignore_words or []
ignore_words = parse_ignore_words_option(options.ignore_words_list)
ignore_words, ignore_words_cased = parse_ignore_words_option(
options.ignore_words_list
)

for ignore_words_file in ignore_words_files:
if not os.path.isfile(ignore_words_file):
print(
Expand All @@ -1121,7 +1142,7 @@ def main(*args: str) -> int:
)
parser.print_help()
return EX_USAGE
build_ignore_words(ignore_words_file, ignore_words)
build_ignore_words(ignore_words_file, ignore_words, ignore_words_cased)

uri_regex = options.uri_regex or uri_regex_def
try:
Expand All @@ -1133,7 +1154,10 @@ def main(*args: str) -> int:
)
parser.print_help()
return EX_USAGE
uri_ignore_words = parse_ignore_words_option(options.uri_ignore_words_list)
uri_ignore_words_lowercase, uri_ignore_words_cased = parse_ignore_words_option(
options.uri_ignore_words_list
)
uri_ignore_words = uri_ignore_words_lowercase | uri_ignore_words_cased
DimitriPapadopoulos marked this conversation as resolved.
Show resolved Hide resolved

dictionaries = options.dictionary or ["-"]

Expand Down Expand Up @@ -1242,6 +1266,7 @@ def main(*args: str) -> int:
colors,
summary,
misspellings,
ignore_words_cased,
exclude_lines,
file_opener,
word_regex,
Expand All @@ -1266,6 +1291,7 @@ def main(*args: str) -> int:
colors,
summary,
misspellings,
ignore_words_cased,
exclude_lines,
file_opener,
word_regex,
Expand Down
37 changes: 37 additions & 0 deletions codespell_lib/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,43 @@ def test_ignore_dictionary(
assert cs.main("-I", fname, bad_name) == 1


def test_ignore_words_with_cases(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
) -> None:
"""Test case-sensitivity implemented for -I and -L options in #3272."""
bad_name = tmp_path / "MIS.txt"
bad_name.write_text(
"1 MIS (Management Information System) 1\n"
"2 Les Mis (1980 musical) 2\n"
"3 mis 3\n"
)
assert cs.main(bad_name) == 3
assert cs.main(bad_name, "-f") == 4
fname = tmp_path / "ignore.txt"

fname.write_text("miS")
assert cs.main("-I", fname, bad_name) == 3
assert cs.main("-LmiS", bad_name) == 3
assert cs.main("-I", fname, "-f", bad_name) == 4
assert cs.main("-LmiS", "-f", bad_name) == 4
fname.write_text("MIS")
assert cs.main("-I", fname, bad_name) == 2
assert cs.main("-LMIS", bad_name) == 2
assert cs.main("-I", fname, "-f", bad_name) == 2
assert cs.main("-LMIS", "-f", bad_name) == 2
fname.write_text("MIS\nMis")
assert cs.main("-I", fname, bad_name) == 1
assert cs.main("-LMIS,Mis", bad_name) == 1
assert cs.main("-I", fname, "-f", bad_name) == 1
assert cs.main("-LMIS,Mis", "-f", bad_name) == 1
fname.write_text("mis")
assert cs.main("-I", fname, bad_name) == 0
assert cs.main("-Lmis", bad_name) == 0
assert cs.main("-I", fname, "-f", bad_name) == 0
assert cs.main("-Lmis", "-f", bad_name) == 0


def test_ignore_word_list(
tmp_path: Path,
capsys: pytest.CaptureFixture[str],
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ max-complexity = 45

[tool.ruff.lint.pylint]
allow-magic-value-types = ["bytes", "int", "str",]
max-args = 12
max-args = 13
max-branches = 49
max-returns = 11
max-statements = 111
max-statements = 113
Loading