fix: show correct program name in argparse help/errors#14126
fix: show correct program name in argparse help/errors#14126RonnyPfannschmidt wants to merge 1 commit intopytest-dev:mainfrom
Conversation
Display 'pytest', 'python -m pytest', or 'pytest.main()' based on how pytest was invoked, fixing confusing error messages when calling pytest.main() programmatically. Fixes pytest-dev#1764 Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Anthropic Claude Opus 4 <claude@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This pull request fixes issue #1764 by displaying the correct program name in argparse help and error messages based on how pytest was invoked. Previously, programmatic invocations could show confusing names like setup.py: error:.
Changes:
- Added
_get_prog_name()helper function to determine the appropriate program name based on invocation context - Modified
main()to accept a private_invoked_from_consoleparameter and pass the program name through the configuration chain - Updated
console_main()to set_invoked_from_console=Truewhen called from CLI - Extended
get_config(),_prepareconfig(), andConfig.__init__()to accept and propagate theprogparameter
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/_pytest/config/init.py | Implements the core logic for determining and setting the program name based on invocation context |
| testing/test_config.py | Adds comprehensive test coverage for the new functionality including unit tests for _get_prog_name() and integration tests for error/help messages |
| changelog/1764.improvement.rst | Documents the improvement for users |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| args: list[str] | os.PathLike[str] | None = None, | ||
| plugins: Sequence[str | _PluggyPlugin] | None = None, | ||
| *, | ||
| _invoked_from_console: bool = False, |
There was a problem hiding this comment.
The new _invoked_from_console parameter is not documented in the docstring. Consider adding documentation for this parameter to help future maintainers understand its purpose, even though it's a private parameter.
|
|
||
|
|
||
| def _get_prog_name( | ||
| *, invoked_from_console: bool, _argv: list[str] | None = None |
There was a problem hiding this comment.
I would just make argv a required argument and pass sys.argv where needed instead of making it optional.
Also, since the list is not modified, should type is as Sequence[str].
| :returns: The program name to display in help and error messages. | ||
| """ | ||
| if not invoked_from_console: | ||
| # Called programmatically via pytest.main() |
There was a problem hiding this comment.
I would drop this comment as it's pretty clear without it.
| # Called programmatically via pytest.main() | ||
| return "pytest.main()" | ||
|
|
||
| # Called from CLI - check if it's `python -m pytest` or direct `pytest` |
| argv0 = argv[0] if argv else "" | ||
| # When running as `python -m pytest`, argv[0] is the path to __main__.py | ||
| if os.path.basename(argv0) == "__main__.py": | ||
| return "python -m pytest" |
There was a problem hiding this comment.
Seems a bit odd to me, is it really necessary to distinguish python -m pytest from pytest? I think using pytest for both is OK.
There was a problem hiding this comment.
theres sys.path differences between the 2
| args: list[str] | os.PathLike[str] | None = None, | ||
| plugins: Sequence[str | _PluggyPlugin] | None = None, | ||
| *, | ||
| _invoked_from_console: bool = False, |
There was a problem hiding this comment.
This parameter leaks into the sphinx autodoc. That's not too bad, but since we already have console_main it makes me think maybe we should add a _main function (or with a better name...) that both console_main and main call, such that main is exclusively for programmatic use.
I'm loath to add yet another function to the initialization routines but technically it seems better and might be helpful for other things in the future.
| if prog is not None: | ||
| self._parser.prog = prog |
There was a problem hiding this comment.
Wouldn't it be better to add a prog parameter to Parser's ctor, than to access its internals?
Summary
pytest,python -m pytest, orpytest.main()in help/error messages based on how pytest was invokedsetup.py: error:when callingpytest.main()programmaticallyFixes #1764