Skip to content

fix: show correct program name in argparse help/errors#14126

Merged
RonnyPfannschmidt merged 4 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:fix-1764-argparse-program-name
May 25, 2026
Merged

fix: show correct program name in argparse help/errors#14126
RonnyPfannschmidt merged 4 commits into
pytest-dev:mainfrom
RonnyPfannschmidt:fix-1764-argparse-program-name

Conversation

@RonnyPfannschmidt

Copy link
Copy Markdown
Member

Summary

  • Display pytest, python -m pytest, or pytest.main() in help/error messages based on how pytest was invoked
  • Fixes confusing error messages like setup.py: error: when calling pytest.main() programmatically

Fixes #1764

Copilot AI review requested due to automatic review settings January 18, 2026 10:21
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Jan 18, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_console parameter and pass the program name through the configuration chain
  • Updated console_main() to set _invoked_from_console=True when called from CLI
  • Extended get_config(), _prepareconfig(), and Config.__init__() to accept and propagate the prog parameter

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.

Comment thread src/_pytest/config/__init__.py Outdated
Comment thread src/_pytest/config/__init__.py Outdated
Comment thread src/_pytest/config/__init__.py Outdated
Comment thread src/_pytest/config/__init__.py Outdated
Comment thread src/_pytest/config/__init__.py
Comment thread src/_pytest/config/__init__.py Outdated
Comment thread src/_pytest/config/__init__.py Outdated
RonnyPfannschmidt and others added 3 commits May 24, 2026 10:36
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>
@RonnyPfannschmidt RonnyPfannschmidt force-pushed the fix-1764-argparse-program-name branch from c921149 to 438c01d Compare May 24, 2026 08:38
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 nicoddemus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, never noticed that particular wart.

@RonnyPfannschmidt

Copy link
Copy Markdown
Member Author

its one of the oldest bugs still open, oldest first is terrifying sometimes

@RonnyPfannschmidt RonnyPfannschmidt merged commit 59dc7f6 into pytest-dev:main May 25, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pytest.main() displays error messages as if it were a script, guessing the name of the program running it

4 participants