fix: show correct program name in argparse help/errors#14126
Merged
RonnyPfannschmidt merged 4 commits intoMay 25, 2026
Merged
Conversation
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.
bluetech
reviewed
Jan 18, 2026
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>
- Extract _main() so main() stays a clean public API without internal parameters leaking into autodoc - _get_prog_name() now takes a plain argv: Sequence[str] — the invoked_from_console boolean is gone; console_main resolves the prog name from sys.argv, main() always passes "pytest.main()" - Pass prog through Parser/PytestArgumentParser constructors instead of setting the attribute after construction - Simplify tests to match the new single-responsibility _get_prog_name Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Anthropic Claude Opus 4 <claude@anthropic.com>
Make the real implementation _console_main() private. The public console_main() now emits PytestRemovedIn10Warning and delegates. Entry points (pyproject.toml) and __main__.py call _console_main directly so normal CLI usage is unaffected. Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Anthropic Claude Opus 4 <claude@anthropic.com>
c921149 to
438c01d
Compare
The deprecated pytest.console_main() is referenced with :func: in the changelog fragment and deprecations.rst, but has no autofunction directive in the reference docs (intentionally, since it's being removed). Add it to nitpick_ignore so the RTD build passes. Co-authored-by: Cursor AI <ai@cursor.sh> Co-authored-by: Anthropic Claude Opus 4 <claude@anthropic.com>
nicoddemus
approved these changes
May 25, 2026
nicoddemus
left a comment
Member
There was a problem hiding this comment.
Nice, never noticed that particular wart.
Member
Author
|
its one of the oldest bugs still open, oldest first is terrifying sometimes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
pytest,python -m pytest, orpytest.main()in help/error messages based on how pytest was invokedsetup.py: error:when callingpytest.main()programmaticallyFixes #1764