pytest icon indicating copy to clipboard operation
pytest copied to clipboard

Provide a way to push configuration based on the environment

Open Delgan opened this issue 3 years ago • 2 comments

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-plugins plugin
  • It accepts a .ini file as --mypy-ini-file argument
  • 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 pytest command 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.ini only 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

Delgan avatar Jun 26 '22 13:06 Delgan

@jaraco recently created the pytest-enabler plugin

RonnyPfannschmidt avatar Jun 26 '22 13:06 RonnyPfannschmidt

Interesting, such plugin would probably solve the "argument must be ignored for older Python version" part of my issue.

Delgan avatar Jun 26 '22 13:06 Delgan