Summary
-c/--config-file does not validate its input consistently. Depending on the extension of the (possibly nonexistent) path given, pytest either silently proceeds with an empty configuration, or crashes with an unhandled FileNotFoundError traceback. Neither is correct — both should be a clean UsageError.
Steps to reproduce
mkdir repro; cd repro
cat > config.ini <<'EOF'
[pytest]
addopts = -v
EOF
Then
pytest -c config.in # real file, wrong extension
pytest -c fake_config.in # doesn't exist, wrong extension
pytest -c fake_config.ini # doesn't exist, correct extension
Actual Behavior
pytest -c config.in and pytest -c fake_config.in both produce identical result:
==================================================== test session starts =====================================================
platform darwin -- Python 3.12.13, pytest-9.1.1, pluggy-1.6.0
rootdir: /Users/user/repro
configfile: config.in
plugins: env-1.6.0, metadata-3.1.1, html-4.2.0
collected 0 items
=================================================== no tests ran in 0.01s ====================================================
note configfile: is reported even though the file was never checked to exist.
pytest -c fake_config.ini, by contrast, raises an unhandled FileNotFoundError and prints a raw Python traceback instead of a clean pytest error.
Root Cause
In src/_pytest/config/findpaths.py:
load_config_dict_from_file() dispatches on Path.suffix(). Only .ini, .cfg, and .toml have handling branches, so everything else is implicitly returned as None.
- for a
.ini file, _parse_ini_config() apparently checks only for malformed-content errors and not missing-file errors, so a nonexistent .ini path raises an uncaught FileNotFoundError
Environment
- pytest 9.1.1
- Python 3.12.13
- macOS (darwin)
Related Issues
Probably #13246
Summary
-c/--config-filedoes not validate its input consistently. Depending on the extension of the (possibly nonexistent) path given, pytest either silently proceeds with an empty configuration, or crashes with an unhandledFileNotFoundErrortraceback. Neither is correct — both should be a cleanUsageError.Steps to reproduce
Then
Actual Behavior
pytest -c config.inandpytest -c fake_config.inboth produce identical result:note
configfile:is reported even though the file was never checked to exist.pytest -c fake_config.ini, by contrast, raises an unhandledFileNotFoundErrorand prints a raw Python traceback instead of a clean pytest error.Root Cause
In
src/_pytest/config/findpaths.py:load_config_dict_from_file()dispatches onPath.suffix(). Only.ini,.cfg, and.tomlhave handling branches, so everything else is implicitly returned asNone..inifile,_parse_ini_config()apparently checks only for malformed-content errors and not missing-file errors, so a nonexistent.inipath raises an uncaughtFileNotFoundErrorEnvironment
Related Issues
Probably #13246