Repository navigation
report file arguments that cannot be used - #4070
Open
qianeric-backup wants to merge 2 commits into
Open
qianeric-backup wants to merge 2 commits into
qianeric-backup wants to merge 2 commits into
Conversation
Three options take a path, and each one mishandled a path that does not exist:
* `--config`, `--toml`: `configparser.read()` silently ignores missing files, so
a typo in the CI job silently ran codespell with no configuration at all:
$ codespell --config .codespellrc src/
src/foo.py:1: teh ==> the
$ echo $?
65
`--toml` crashed instead, because the file is opened directly:
$ codespell --toml pyproject.toml src/
Traceback (most recent call last):
File ".../codespell_lib/_codespell.py", line 760, in parse_options
with open(toml_file, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'pyproject.toml'
* `--exclude-file`: unlike `--ignore-words`, it has no existence check, so it
ended in the same traceback:
$ codespell --exclude-file .codespell-exclude src/
Traceback (most recent call last):
File ".../codespell_lib/_codespell.py", line 1597, in main
build_exclude_hashes(exclude_file, exclude_lines)
FileNotFoundError: [Errno 2] No such file or directory: '.codespell-exclude'
`--ignore-words` already answers this case properly:
if not os.path.isfile(ignore_words_file):
return _usage_error(
parser,
f"ERROR: cannot find ignore-words file: {ignore_words_file}",
)
## Fix
`parse_options()` raises the new `_FileUsageError`, and `main()` turns it into
the usual usage error, so all three behave like `--ignore-words`:
```
$ codespell --config /nope.toml src/ ; echo $?
usage: codespell ...
ERROR: cannot find config file: /nope.toml
64
```
| case | before | after |
|---|---|---|
| `--config <missing>` | exit 65, runs unconfigured | `ERROR: cannot find config file: ...`, exit 64 |
| `--toml <missing>` | traceback, exit 1 | `ERROR: cannot find toml file: ...`, exit 64 |
| `--exclude-file <missing>` | traceback, exit 1 | `ERROR: cannot find exclude-file: ...`, exit 64 |
| `--config`/`--toml`/`--exclude-file` valid | works | unchanged |
| auto-detected `setup.cfg`/`.codespellrc`/`pyproject.toml` missing | ignored | unchanged |
The `_FileUsageError` type is needed because the three checks live in
`parse_options()`, which returns `(options, parser, used_cfg_files)` and cannot
return an exit code; `_usage_error()` stays as it is and is reused unchanged.
## Tests
`test_basic` gains assertions for the three cases; they fail on the current tree
(`assert 0 == 64` for the silent `--config` case, tracebacks for the other two).
`pytest codespell_lib/tests/` gives 132 passed / 19 failed on this machine both
before and after the change -- the 19 failures are pre-existing and unrelated
(interactive-prompt and bitmask-option tests that need a TTY/`pytest-subtests`
setup this sandbox does not provide).
qianeric-backup
requested review from
larsoner and
peternewman
as code owners
November 22, 2026 11:46
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three options take a path, and each one mishandled a path that does not exist.
--configand--tomlconfigparser.read()silently ignores missing files, so a typo in the CI job silently ran codespell with no configuration at all:--tomlcrashed instead, because that file is opened directly:--exclude-fileUnlike
--ignore-words, it has no existence check, so it ends in the same traceback:--ignore-wordsalready answers this case properly:Fix
parse_options()raises the new_FileUsageError, andmain()turns it into the usual usage error, so all three behave like--ignore-words:--config <missing>ERROR: cannot find config file: ..., exit 64--toml <missing>ERROR: cannot find toml file: ..., exit 64--exclude-file <missing>ERROR: cannot find exclude-file: ..., exit 64--config/--toml/--exclude-filevalidsetup.cfg/.codespellrc/pyproject.tomlmissingThe
_FileUsageErrortype is needed because the three checks live inparse_options(), which returns(options, parser, used_cfg_files)and cannot return an exit code;_usage_error()stays as it is and is reused unchanged.Tests
test_basicgains assertions for the three cases; they fail on the current tree (assert 0 == 64for the silent--configcase, tracebacks for the other two).pytest codespell_lib/tests/gives 132 passed / 19 failed on this machine both before and after the change — the 19 failures are pre-existing and unrelated (interactive-prompt and bitmask-option tests that need a TTY /pytest-subtestssetup this sandbox does not provide).