Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Sphinx-compatible docstrings and inline comments for main.py (lines 245-490) #13091

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

mohammadaminjohari
Copy link

This pull request addresses issue #12979 by enhancing the documentation and readability of the file pytest/src/_pytest/main.py in the range of lines 245 to 490.
Changes Made:

Docstrings Added:
    All functions and classes in the specified range now include Sphinx-compatible docstrings with:
        A brief overview of the purpose of each function or class.
        Detailed descriptions of parameters (names, types, and expected values).
        Return values and their types.
        Possible exceptions raised by the functions.


    Added meaningful inline comments to clarify complex logic and improve code readability.

Functions and Classes Updated:

validate_basetemp: Validates the base temporary directory path.
wrap_session: Executes the pytest session in a controlled wrapper.
pytest_cmdline_main: The main entry point for pytest command-line execution.
_main: The default protocol for session initialization, test running, and reporting.
pytest_collection: Handles the collection phase of pytest sessions.
pytest_runtestloop: Manages the test run loop for pytest sessions.



All changes were verified locally by running the test suite using tox. No errors or warnings were encountered.



Please review the changes and let me know if further modifications are needed.

Best regards,
Mohammadamin Johari

Copy link
Contributor

@dongfangtianyu dongfangtianyu left a comment

Choose a reason for hiding this comment

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

Thanks @mohammadaminjohari for PR, just 2 suggestions for reference:

  1. Keep the style of the docstring consistent with the existing pytest code, for example,

  2. Do not modify the inline comments for now, it is not necessary at first glance

@@ -248,24 +248,37 @@ def pytest_addoption(parser: Parser) -> None:


def validate_basetemp(path: str) -> str:
# GH 7119
"""
Validate the base temporary directory path.
Copy link
Contributor

@dongfangtianyu dongfangtianyu Dec 31, 2024

Choose a reason for hiding this comment

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

GH 7119 refers to #7119 clarifying the origin of the code, suggest keeping it in the docstrings, for example:

Validate the base temporary directory path (see also :issue:`7119`).

@@ -275,17 +288,26 @@ def is_ancestor(base: Path, query: Path) -> bool:
def wrap_session(
config: Config, doit: Callable[[Config, Session], int | ExitCode | None]
) -> int | ExitCode:
"""Skeleton command line program."""
"""
Execute the pytest session within a controlled wrapper.
Copy link
Contributor

Choose a reason for hiding this comment

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

The purpose is to execute doit, not to execute session,
Consider modifying to:

Execute a function within a pytest session.

initstate = 2
session.exitstatus = doit(config, session) or 0
session.exitstatus = doit(config, session) or 0 # Execute main logic
Copy link
Contributor

Choose a reason for hiding this comment

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

now,execute doit, not execute main. (These line comments may be unnecessary because the method names are already intuitive enough)

Copy link
Member

@Pierre-Sassoulas Pierre-Sassoulas left a comment

Choose a reason for hiding this comment

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

Thanks you for opening the PR. I started a review but @dongfangtianyu did something more detailled, i'll just leave what I had. About inline comment in particular : Commentting what the code does is duplicating informations, inline comment should be about why the code is done this way not what it does.

argparse.ArgumentTypeError: If the path is invalid, such as being empty, the
current working directory, or a parent directory of the current working directory.
"""
# Error message for invalid paths
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Error message for invalid paths

excinfo = None # type: ignore
os.chdir(session.startpath)
os.chdir(session.startpath) # Return to the initial directory
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
os.chdir(session.startpath) # Return to the initial directory
os.chdir(session.startpath)

initstate = 2
session.exitstatus = doit(config, session) or 0
session.exitstatus = doit(config, session) or 0 # Execute main logic
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
session.exitstatus = doit(config, session) or 0 # Execute main logic
session.exitstatus = doit(config, session) or 0

initstate = 1
config.hook.pytest_sessionstart(session=session)
config.hook.pytest_sessionstart(session=session) # Notify start of session
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
config.hook.pytest_sessionstart(session=session) # Notify start of session
config.hook.pytest_sessionstart(session=session)

session = Session.from_config(config)
session.exitstatus = ExitCode.OK
initstate = 0
try:
try:
config._do_configure()
config._do_configure() # Initialize pytest configuration
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
config._do_configure() # Initialize pytest configuration
config._do_configure()

msg = "basetemp must not be empty, the current working directory or any parent directory of it"

# empty path
# Check for an empty path
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
# Check for an empty path

@dongfangtianyu
Copy link
Contributor

Hi @mohammadaminjohari ,
I received a reminder of the review request, but did not see any new changes. If available, you can continue to commit and push on the this branch.

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.

3 participants