pytest-dev / pytest-dev/pytest

Improve error message in presence of conflicting options

Open
#11,514 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type: enhancement
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

What's the problem this feature will solve?

When 2 plugins add/register the command-line option, the error displayed by pytest is not clear enough.
It would be nice if pytest could display the list of plugins adding the same option.

Describe the solution you'd like

The actual error is:

pytest --help

Traceback (most recent call last):
  File "/home/harmin/.local/bin/pytest", line 8, in <module>
    sys.exit(console_main())
  File "/home/harmin/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 192, in console_main
    code = main()
  File "/home/harmin/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 150, in main
    config = _prepareconfig(args, plugins)
  File "/home/harmin/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 331, in _prepareconfig
    config = pluginmanager.hook.pytest_cmdline_parse(
  File "/usr/local/lib/python3.8/dist-packages/pluggy/_hooks.py", line 265, in __call__
    return self._hookexec(self.name, self.get_hookimpls(), kwargs, firstresult)
  File "/usr/local/lib/python3.8/dist-packages/pluggy/_manager.py", line 80, in _hookexec
    return self._inner_hookexec(hook_name, methods, kwargs, firstresult)
  File "/usr/local/lib/python3.8/dist-packages/pluggy/_callers.py", line 55, in _multicall
    gen.send(outcome)
  File "/home/harmin/.local/lib/python3.8/site-packages/_pytest/helpconfig.py", line 104, in pytest_cmdline_parse
    config: Config = outcome.get_result()
  File "/usr/local/lib/python3.8/dist-packages/pluggy/_result.py", line 60, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/usr/local/lib/python3.8/dist-packages/pluggy/_callers.py", line 39, in _multicall
    res = hook_impl.function(*args)
  File "/home/harmin/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 1075, in pytest_cmdline_parse
    self.parse(args)
  File "/home/harmin/.local/lib/python3.8/site-packages/_pytest/config/__init__.py", line 1430, in parse
    args = self._parser.parse_setoption(
  File "/home/harmin/.local/lib/python3.8/site-packages/_pytest/config/argparsing.py", line 143, in parse_setoption
    parsedoption = self.parse(args, namespace=namespace)
  File "/home/harmin/.local/lib/python3.8/site-packages/_pytest/config/argparsing.py", line 113, in parse
    self.optparser = self._getparser()
  File "/home/harmin/.local/lib/python3.8/site-packages/_pytest/config/argparsing.py", line 130, in _getparser
    arggroup.add_argument(*n, **a)
  File "/usr/lib/python3.8/argparse.py", line 1398, in add_argument
    return self._add_action(action)
  File "/usr/lib/python3.8/argparse.py", line 1602, in _add_action
    action = super(_ArgumentGroup, self)._add_action(action)
  File "/usr/lib/python3.8/argparse.py", line 1412, in _add_action
    self._check_conflict(action)
  File "/usr/lib/python3.8/argparse.py", line 1551, in _check_conflict
    conflict_handler(action, confl_optionals)
  File "/usr/lib/python3.8/argparse.py", line 1560, in _handle_conflict_error
    raise ArgumentError(action, message % conflict_string)
argparse.ArgumentError: argument --browser: conflicting option string: --browser

It would be nice to add or replace the last line with a list of plugins adding the same option.
argparse.ArgumentError: argument --browser: conflicting option string: --browser added by plugins: ['plugin1', 'plugin2']

or at least, a more clear, generic message like:
argparse.ArgumentError: argument --browser: conflicting option string: --browser added by two or more plugins.

As time passes by, the number of third-party plugins is going to grow and the risk of command-line option collisions will become inevitable, making the co-existence of some plugins impossible.

A more clear message can help understand the user which plugins are incompatible, so the user can install them in separate python virtual environments.

Alternative Solutions

No workaround.

Additional context

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in _pytest/config/argparsing.py at _getparser and trace how _pytest/config/init.py parses command-line options through pytest_cmdline_parse. Reproduce the conflict with two plugins registering --browser, then verify that pytest --help reports the conflicting plugins or clearly identifies that multiple plugins added the option.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.