Pytest: Don't throw index error when running `pytest --help` when adding ini option via `parser.addini` in `conftest.py` with no help

Created on 20 Jun 2020  路  3Comments  路  Source: pytest-dev/pytest

a detailed description of the bug or suggestion

When running pytest --help with a conftest.py file present that defines an ini option with an empty string as help text, an index error is thrown. We should not throw an error here, but what exactly we should do I think is open for discussion.

When passing None, a different error is throw. We should fix both the issue that happens with None and the empty string as part of this

output of pip list from the virtual environment you are using

pip_list_pytest_help_issue.txt

pytest and operating system versions

Pytest is built from master locally:

(.venv) gnikonorov:~/pytest/gleb_test$ pytest --version
pytest 5.4.1.dev546+g4cc4ebf3c
(.venv) gnikonorov:~/pytest/gleb_test$

minimal example if possible

Contents of conftest.py:

(.venv) gnikonorov:~/pytest/gleb_test$ cat conftest.py
def pytest_addoption(parser):
    parser.addini("my_ini", "", default=True, type="bool")
(.venv) gnikonorov:~/pytest/gleb_test$
(.venv) gnikonorov:~/pytest/gleb_test$ pytest --help
usage: pytest [options] [file_or_dir] [file_or_dir] [...]
.....
                        Automatically find and add a Django project to the Python path.
  FAIL_INVALID_TEMPLATE_VARS (bool):
                        Fail for invalid variables in templates.
  my_ini (bool):        Traceback (most recent call last):
  File "/home/gnikonorov/pytest/.venv/bin/pytest", line 11, in <module>
    load_entry_point('pytest', 'console_scripts', 'pytest')()
  File "/home/gnikonorov/pytest/src/_pytest/config/__init__.py", line 165, in console_main
    code = main()
  File "/home/gnikonorov/pytest/src/_pytest/config/__init__.py", line 143, in main
    config=config
  File "/home/gnikonorov/pytest/.venv/lib/python3.6/site-packages/pluggy/hooks.py", line 286, in __call__
    return self._hookexec(self, self.get_hookimpls(), kwargs)
  File "/home/gnikonorov/pytest/.venv/lib/python3.6/site-packages/pluggy/manager.py", line 93, in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
  File "/home/gnikonorov/pytest/.venv/lib/python3.6/site-packages/pluggy/manager.py", line 87, in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
  File "/home/gnikonorov/pytest/.venv/lib/python3.6/site-packages/pluggy/callers.py", line 208, in _multicall
    return outcome.get_result()
  File "/home/gnikonorov/pytest/.venv/lib/python3.6/site-packages/pluggy/callers.py", line 80, in get_result
    raise ex[1].with_traceback(ex[2])
  File "/home/gnikonorov/pytest/.venv/lib/python3.6/site-packages/pluggy/callers.py", line 187, in _multicall
    res = hook_impl.function(*args)
  File "/home/gnikonorov/pytest/src/_pytest/helpconfig.py", line 148, in pytest_cmdline_main
    showhelp(config)
  File "/home/gnikonorov/pytest/src/_pytest/helpconfig.py", line 194, in showhelp
    tw.line(wrapped[0])
IndexError: list index out of range
(.venv) gnikonorov:~/pytest/gleb_test$
bug

Most helpful comment

Hi, I thought I'd take a stab at resolving this bug and have submitted a PR that would resolve a None help parameter to an empty string and then fixed showhelp to work with an empty string (so both help="" and help=None should work). Let me know your feedback (and also if I've followed the guidelines correctly). Cheers.

All 3 comments

I have tried this on a new virtualenv, on my Mac with the same commit hash. I couldn't repro the bug. Your pip seems too old.
What am I missing here?

@srinivasreddy, the issue actually has nothing to do with the commit. I didn't realize I had a conftest.py that added an ini option with no help text in my directory. Feel free to submit a PR for this if you'd like. I think we definitely should not throw here.

Hi, I thought I'd take a stab at resolving this bug and have submitted a PR that would resolve a None help parameter to an empty string and then fixed showhelp to work with an empty string (so both help="" and help=None should work). Let me know your feedback (and also if I've followed the guidelines correctly). Cheers.

Was this page helpful?
0 / 5 - 0 ratings