pytest
pytest copied to clipboard
Provide a way to push configuration based on the environment
For context, see #10055 which initiated this ticket.
What's the problem this feature will solve?
To give a concrete use case example:
- I'm using the
pytest-mypy-pluginsplugin - It accepts a
.inifile as--mypy-ini-fileargument - The argument must be ignored for older Python version
- The argument path is resolved relative to the working directory
- I would like to dynamically add the argument as an absolute path so that the
pytestcommand works in all cases
Describe the solution you'd like
An "official" way to dynamically adjust the configuration and command line arguments of pytest for non-plugins.
Given that:
- a new hook is not desirable
- mutating the command line arguments is not desirable
- substitution variables in the
pytest.inionly solves part of the problem
Should we add a new method to Config object specifically intended to safely mutate the option namespace?
Alternative Solutions
The documentation (Dynamically adding command line options) suggests to use thepytest_load_initial_conftests() hook, unfortunately it is not called for conftests.py root.
The pytest_cmdline_preparse() is a similar hook which solves the issue. However, it's deprecated and will likely be removed at some point, so newly written code can't reliably use it.
It's also possible to manually mutate the Config instance using pytest_configure() but it's does not look idiomatic, it requires accessing some internals, and it's error-prone.
Additional context
Related issues:
- https://github.com/pytest-dev/pytest/issues/9619
- https://github.com/pytest-dev/pytest/issues/5024
- https://github.com/pytest-dev/pytest/issues/8591
- https://github.com/pytest-dev/pytest/issues/4482
@jaraco recently created the pytest-enabler plugin
Interesting, such plugin would probably solve the "argument must be ignored for older Python version" part of my issue.