Skip to content

Conversation

@RonnyPfannschmidt
Copy link
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

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>
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
Copy link

Copilot AI left a comment

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.

args: list[str] | os.PathLike[str] | None = None,
plugins: Sequence[str | _PluggyPlugin] | None = None,
*,
_invoked_from_console: bool = False,
Copy link

Copilot AI Jan 18, 2026

Choose a reason for hiding this comment

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

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.

Copilot uses AI. Check for mistakes.


def _get_prog_name(
*, invoked_from_console: bool, _argv: list[str] | None = None
Copy link
Member

Choose a reason for hiding this comment

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

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()
Copy link
Member

Choose a reason for hiding this comment

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

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`
Copy link
Member

Choose a reason for hiding this comment

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

Would drop this comment.

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"
Copy link
Member

Choose a reason for hiding this comment

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

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

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,
Copy link
Member

@bluetech bluetech Jan 18, 2026

Choose a reason for hiding this comment

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

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.

Comment on lines +1138 to +1139
if prog is not None:
self._parser.prog = prog
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be better to add a prog parameter to Parser's ctor, than to access its internals?

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

2 participants