From b0c161281eaa6d48a9cb33320439c066048b362e Mon Sep 17 00:00:00 2001 From: "Chris (ChrisJr404)" <11917633+ChrisJr404@users.noreply.github.com> Date: Tue, 25 Aug 2026 05:03:06 -0400 Subject: [PATCH] Check explicitly-named hidden files without --check-hidden --- codespell_lib/_codespell.py | 12 +++++++++--- codespell_lib/tests/test_basic.py | 11 +++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/codespell_lib/_codespell.py b/codespell_lib/_codespell.py index 1ec09fdf8f..7cd1448529 100644 --- a/codespell_lib/_codespell.py +++ b/codespell_lib/_codespell.py @@ -640,7 +640,11 @@ def convert_arg_line_to_args(self, arg_line: str) -> list[str]: "--check-hidden", action="store_true", default=False, - help='check hidden files and directories (those starting with ".") as well.', + help=( + 'check hidden files and directories (those starting with ".") as ' + "well. Hidden files named explicitly on the command line are always " + "checked." + ), ) parser.add_argument( "-A", @@ -1528,8 +1532,10 @@ def main(*args: str) -> int: bad_count = 0 for filename in sorted(options.files): - # ignore hidden files - if is_hidden(filename, options.check_hidden): + # A hidden path named explicitly on the command line is checked even + # without --check-hidden. Directories still honor --check-hidden so that + # recursing into them does not pull in hidden contents unexpectedly. + if is_hidden(filename, options.check_hidden) and os.path.isdir(filename): continue if os.path.isdir(filename): diff --git a/codespell_lib/tests/test_basic.py b/codespell_lib/tests/test_basic.py index 0127f57013..bc4a0ee7ed 100644 --- a/codespell_lib/tests/test_basic.py +++ b/codespell_lib/tests/test_basic.py @@ -774,7 +774,9 @@ def test_check_hidden( # hidden_file = tmp_path / ".test.txt" fname.rename(hidden_file) - assert cs.main(hidden_file) == 0 + # A hidden file named explicitly is checked even without --check-hidden, + # but the same file reached by recursing a directory is still skipped. + assert cs.main(hidden_file) == 1 assert cs.main(tmp_path) == 0 assert cs.main("--check-hidden", hidden_file) == 1 assert cs.main("--check-hidden", tmp_path) == 1 @@ -786,7 +788,8 @@ def test_check_hidden( # typo_file = tmp_path / ".abandonned.txt" hidden_file.rename(typo_file) - assert cs.main(typo_file) == 0 + assert cs.main(typo_file) == 1 + assert cs.main("--check-filenames", typo_file) == 2 assert cs.main(tmp_path) == 0 assert cs.main("--check-hidden", typo_file) == 1 assert cs.main("--check-hidden", tmp_path) == 1 @@ -811,6 +814,10 @@ def test_check_hidden( subdir = hidden / "subdir" subdir.mkdir() copyfile(typo_file, subdir / typo_file.name) + # An explicitly named hidden directory is still skipped without + # --check-hidden, since scanning its contents is a recursive scan. + assert cs.main(hidden) == 0 + assert cs.main("--check-hidden", hidden) == 2 assert cs.main(tmp_path) == 0 assert cs.main("--check-hidden", tmp_path) == 3 assert cs.main("--check-hidden", "--check-filenames", tmp_path) == 8