Skip to content

-c/--config-file has two failure modes for invalid paths: silent no-op for unrecognized extensions, and raw FileNotFoundError for recognized ones #14716

Description

@SecondarySkyler

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions