← Back to Pull requests

fix(cli): preserve display output with --quiet #14155

Open
ychampion opened this pull request with 190 lines diff across 18 files
View on GitHub
ychampion ychampion commented on Jul 08, 2026 01:17 UTC
Author

What does this PR do?

Fixes #13018.

--quiet currently raises pip's logging threshold, which also hides primary output from display commands. This keeps primary display output visible when quiet mode suppresses INFO logs, while leaving install/download progress output quiet-suppressible.

Covered display paths include show, help, index versions, list, check, hash, cache dir/info/list, config get/list/debug, debug, and search output.

Validation:

  • uv run --with pre-commit pre-commit run black --files <touched Python files>

  • uv run --with pre-commit pre-commit run ruff-check --files <touched Python files>

  • uv run --with nox nox -s test-3.12 -- tests/functional/test_show.py tests/functional/test_help.py tests/functional/test_index.py tests/functional/test_check.py tests/functional/test_list.py tests/functional/test_hash.py tests/functional/test_cache.py tests/functional/test_configuration.py tests/functional/test_debug.py tests/unit/test_command_show.py -q

  • uv run --with nox nox -s test-3.12 -- tests/functional/test_install.py::test_install_quiet -q

  • Source-tree smoke checks for list --quiet, show --quiet, cache dir --quiet, and install --dry-run --quiet
  • PR Checklist:

  • I agree to follow the PSF Code of Conduct.

  • I have read and have followed the CONTRIBUTING.md file.

  • I have added a news file fragment (or this PR does not need one).

  • I have read and followed the AI_POLICY.md file, and if any AI tools were used, I have disclosed it below.
  • AI tools used: Codex.

    Diff: 190 lines (+168 / -22) across 18 files Reviewer: sepehr-rs Review duration: 156651m
    All Vouch review confidence checks passed
    Residual risk 0.11 = Change Risk (0.70) × (1 − Review Confidence (0.84)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.70 · 190 lines diff across 18 files
    0.70
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.88 · 5 substantive comment(s)
    0.88
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    84%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.314
    Mass File Touch mass_file_touch
    0.080
    Real PR Conversation Timeline
    21 items
    copilot-pull-request-reviewer[bot] copilot-pull-request-reviewer[bot] commented on Jul 08, 2026 01:17 UTC
    Reviewer

    Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

    sepehr-rs sepehr-rs commented on code on Jul 08, 2026 03:05 UTC
    Code Review
    src/pip/_internal/commands/show.py:1
    @@ -187,51 +187,53 @@ def print_results(
         Print the information from installed distributions found.
         """
         results_printed = False
    +
    +    def output(msg: str, *args: object) -> None:

    I'm not sure why we need this helper function. If the goal is to reduce show_on_quiet=True noise, let's just make True the default and explicitly set exceptions to False. That would clean things up and let us remove this helper.

    sepehr-rs sepehr-rs commented on code on Jul 08, 2026 03:32 UTC
    Code Review
    src/pip/_internal/utils/misc.py:1
    @@ -381,8 +381,59 @@ def is_local(path: str) -> bool:
         return path.startswith(normalize_path(sys.prefix))
     
     
    -def write_output(msg: Any, *args: Any) -> None:
    +def _format_output(msg: Any, *args: Any) -> str:
    +    if args:
    +        return str(msg) % args

    Any reason this uses %-style formatting? Could just use .format(): return str(msg).format(*args)

    sepehr-rs sepehr-rs commented on code on Jul 08, 2026 03:35 UTC
    Code Review
    src/pip/_internal/utils/misc.py:1
    @@ -381,8 +381,59 @@ def is_local(path: str) -> bool:
         return path.startswith(normalize_path(sys.prefix))
     
     
    -def write_output(msg: Any, *args: Any) -> None:
    +def _format_output(msg: Any, *args: Any) -> str:
    +    if args:
    +        return str(msg) % args
    +    return str(msg)
    +
    +
    +def _console_logging_accepts_info() -> bool:
    +    if not logger.isEnabledFor(logging.INFO):
    +        return False
    +
    +    has_console_handler = False
    +    for handler in logging.getLogger().handlers:
    +        if getattr(handler, "console", None) is None:
    +            continue
    +        has_console_handler = True
    +        if handler.level <= logging.INFO:
    +            return True
    +
    +    return not has_console_handler
    +
    +
    +def _write_stdout_direct(msg: Any, *args: Any) -> None:
    +    from pip._internal.utils.logging import (
    +        BrokenStdoutLoggingError,
    +        get_console,
    +        get_indentation,
    +    )
    +
    +    text = _format_output(msg, *args)
    +    indentation = get_indentation()
    +    if indentation:
    +        prefix = " " * indentation
    +        text = "".join(prefix + line for line in text.splitlines(True))
    +
    +    try:
    +        try:
    +            console = get_console()
    +        except AssertionError:
    +            console = None
    +
    +        if console is None:
    +            sys.stdout.write(text)
    +            sys.stdout.write(os.linesep)
    +        else:
    +            console.print(text, overflow="ignore", crop=False, highlight=False)
    +    except BrokenPipeError as exc:
    +        raise BrokenStdoutLoggingError() from exc
    +
    +
    +def write_output(msg: Any, *args: Any, show_on_quiet: bool = False) -> None:
         logger.info(msg, *args)
    +    if show_on_quiet and not _console_logging_accepts_info():

    I think we can be a bit more defensive here by moving logger.info(msg, *args) after the if branch. What do you think?

    sepehr-rs sepehr-rs commented on code on Jul 08, 2026 03:38 UTC
    Code Review
    src/pip/_internal/utils/misc.py:1
    @@ -381,8 +381,59 @@ def is_local(path: str) -> bool:
         return path.startswith(normalize_path(sys.prefix))
     
     
    -def write_output(msg: Any, *args: Any) -> None:
    +def _format_output(msg: Any, *args: Any) -> str:
    +    if args:
    +        return str(msg) % args
    +    return str(msg)
    +
    +
    +def _console_logging_accepts_info() -> bool:
    +    if not logger.isEnabledFor(logging.INFO):
    +        return False
    +
    +    has_console_handler = False
    +    for handler in logging.getLogger().handlers:
    +        if getattr(handler, "console", None) is None:
    +            continue
    +        has_console_handler = True
    +        if handler.level <= logging.INFO:
    +            return True
    +
    +    return not has_console_handler
    +
    +
    +def _write_stdout_direct(msg: Any, *args: Any) -> None:
    +    from pip._internal.utils.logging import (
    +        BrokenStdoutLoggingError,
    +        get_console,
    +        get_indentation,
    +    )
    +
    +    text = _format_output(msg, *args)
    +    indentation = get_indentation()
    +    if indentation:
    +        prefix = " " * indentation
    +        text = "".join(prefix + line for line in text.splitlines(True))
    +
    +    try:
    +        try:
    +            console = get_console()
    +        except AssertionError:

    AssertionError exceptions usually indicate programming errors and generally shouldn't be swallowed. If there's a good reason for this, could you add a brief comment explaining why? Otherwise, I think this should be removed.

    sepehr-rs sepehr-rs commented on code on Jul 08, 2026 03:42 UTC
    Code Review
    src/pip/_internal/utils/misc.py:1
    @@ -381,8 +381,59 @@ def is_local(path: str) -> bool:
         return path.startswith(normalize_path(sys.prefix))
     
     
    -def write_output(msg: Any, *args: Any) -> None:
    +def _format_output(msg: Any, *args: Any) -> str:
    +    if args:
    +        return str(msg) % args
    +    return str(msg)
    +
    +
    +def _console_logging_accepts_info() -> bool:
    +    if not logger.isEnabledFor(logging.INFO):
    +        return False
    +
    +    has_console_handler = False
    +    for handler in logging.getLogger().handlers:
    +        if getattr(handler, "console", None) is None:

    Since pip already has RichPipStreamHandler in pip._internal.utils.logging, I think we can change this to if not isinstance(handler, RichPipStreamHandler). To keep things clean, it might make sense to move this to src/pip/_internal/utils/logging.py.

    sepehr-rs sepehr-rs commented on code on Jul 08, 2026 04:06 UTC
    Code Review
    tests/functional/test_cache.py:1
    @@ -184,6 +184,12 @@ def test_cache_dir(script: PipTestEnvironment, cache_dir: str) -> None:
         assert os.path.normcase(cache_dir) == result.stdout.strip()
     
     
    +def test_cache_dir_quiet(script: PipTestEnvironment, cache_dir: str) -> None:

    Can you add a docstring here?

    sepehr-rs sepehr-rs requested changes on Jul 08, 2026 04:16 UTC
    Changes Requested Reviewer

    Hi @ychampion, thanks for your contribution!
    I'm +1 on this overall. This is something I've wanted to tackle for a long time but never got around to.
    The PR looks good overall, though I've left a few comments that I think would improve it further. Let me know if you have any questions!

    ychampion ychampion commented on code on Jul 08, 2026 07:40 UTC
    Code Review
    src/pip/_internal/commands/show.py:1
    @@ -187,51 +187,53 @@ def print_results(
         Print the information from installed distributions found.
         """
         results_printed = False
    +
    +    def output(msg: str, *args: object) -> None:

    Done in 55548c41d. write_output now defaults to showing primary display output under quiet mode, so show.py no longer needs the local helper.

    ychampion ychampion commented on code on Jul 08, 2026 07:40 UTC
    Code Review
    src/pip/_internal/utils/misc.py:1
    @@ -381,8 +381,59 @@ def is_local(path: str) -> bool:
         return path.startswith(normalize_path(sys.prefix))
     
     
    -def write_output(msg: Any, *args: Any) -> None:
    +def _format_output(msg: Any, *args: Any) -> str:
    +    if args:
    +        return str(msg) % args

    I kept % formatting because the same msg, *args are still passed to logger.info, which uses logging-style interpolation. I added a short comment in the helper to make that explicit.

    ychampion ychampion commented on code on Jul 08, 2026 07:40 UTC
    Code Review
    src/pip/_internal/utils/misc.py:1
    @@ -381,8 +381,59 @@ def is_local(path: str) -> bool:
         return path.startswith(normalize_path(sys.prefix))
     
     
    -def write_output(msg: Any, *args: Any) -> None:
    +def _format_output(msg: Any, *args: Any) -> str:
    +    if args:
    +        return str(msg) % args
    +    return str(msg)
    +
    +
    +def _console_logging_accepts_info() -> bool:
    +    if not logger.isEnabledFor(logging.INFO):
    +        return False
    +
    +    has_console_handler = False
    +    for handler in logging.getLogger().handlers:
    +        if getattr(handler, "console", None) is None:
    +            continue
    +        has_console_handler = True
    +        if handler.level <= logging.INFO:
    +            return True
    +
    +    return not has_console_handler
    +
    +
    +def _write_stdout_direct(msg: Any, *args: Any) -> None:
    +    from pip._internal.utils.logging import (
    +        BrokenStdoutLoggingError,
    +        get_console,
    +        get_indentation,
    +    )
    +
    +    text = _format_output(msg, *args)
    +    indentation = get_indentation()
    +    if indentation:
    +        prefix = " " * indentation
    +        text = "".join(prefix + line for line in text.splitlines(True))
    +
    +    try:
    +        try:
    +            console = get_console()
    +        except AssertionError:
    +            console = None
    +
    +        if console is None:
    +            sys.stdout.write(text)
    +            sys.stdout.write(os.linesep)
    +        else:
    +            console.print(text, overflow="ignore", crop=False, highlight=False)
    +    except BrokenPipeError as exc:
    +        raise BrokenStdoutLoggingError() from exc
    +
    +
    +def write_output(msg: Any, *args: Any, show_on_quiet: bool = False) -> None:
         logger.info(msg, *args)
    +    if show_on_quiet and not _console_logging_accepts_info():

    Done in 55548c41d. The direct quiet fallback now returns before the normal logger.info path; otherwise it falls through to logging.

    ychampion ychampion commented on code on Jul 08, 2026 07:40 UTC
    Code Review
    src/pip/_internal/utils/misc.py:1
    @@ -381,8 +381,59 @@ def is_local(path: str) -> bool:
         return path.startswith(normalize_path(sys.prefix))
     
     
    -def write_output(msg: Any, *args: Any) -> None:
    +def _format_output(msg: Any, *args: Any) -> str:
    +    if args:
    +        return str(msg) % args
    +    return str(msg)
    +
    +
    +def _console_logging_accepts_info() -> bool:
    +    if not logger.isEnabledFor(logging.INFO):
    +        return False
    +
    +    has_console_handler = False
    +    for handler in logging.getLogger().handlers:
    +        if getattr(handler, "console", None) is None:
    +            continue
    +        has_console_handler = True
    +        if handler.level <= logging.INFO:
    +            return True
    +
    +    return not has_console_handler
    +
    +
    +def _write_stdout_direct(msg: Any, *args: Any) -> None:
    +    from pip._internal.utils.logging import (
    +        BrokenStdoutLoggingError,
    +        get_console,
    +        get_indentation,
    +    )
    +
    +    text = _format_output(msg, *args)
    +    indentation = get_indentation()
    +    if indentation:
    +        prefix = " " * indentation
    +        text = "".join(prefix + line for line in text.splitlines(True))
    +
    +    try:
    +        try:
    +            console = get_console()
    +        except AssertionError:

    Done in 55548c41d. Removed the AssertionError handling; the fallback now calls get_console() directly instead of hiding a missing console.

    ychampion ychampion commented on code on Jul 08, 2026 07:40 UTC
    Code Review
    src/pip/_internal/utils/misc.py:1
    @@ -381,8 +381,59 @@ def is_local(path: str) -> bool:
         return path.startswith(normalize_path(sys.prefix))
     
     
    -def write_output(msg: Any, *args: Any) -> None:
    +def _format_output(msg: Any, *args: Any) -> str:
    +    if args:
    +        return str(msg) % args
    +    return str(msg)
    +
    +
    +def _console_logging_accepts_info() -> bool:
    +    if not logger.isEnabledFor(logging.INFO):
    +        return False
    +
    +    has_console_handler = False
    +    for handler in logging.getLogger().handlers:
    +        if getattr(handler, "console", None) is None:

    Done in 55548c41d. I moved the fallback helpers into utils/logging.py and now check for RichPipStreamHandler directly.

    ychampion ychampion commented on code on Jul 08, 2026 07:40 UTC
    Code Review
    tests/functional/test_cache.py:1
    @@ -184,6 +184,12 @@ def test_cache_dir(script: PipTestEnvironment, cache_dir: str) -> None:
         assert os.path.normcase(cache_dir) == result.stdout.strip()
     
     
    +def test_cache_dir_quiet(script: PipTestEnvironment, cache_dir: str) -> None:

    Done in 55548c41d.

    sepehr-rs sepehr-rs commented on code on Jul 08, 2026 16:06 UTC
    Code Review
    src/pip/_internal/commands/search.py:1
    @@ -113,21 +113,26 @@ def transform_hits(hits: list[dict[str, str]]) -> list[TransformedHit]:
         return list(packages.values())
     
     
    -def print_dist_installation_info(latest: str, dist: BaseDistribution | None) -> None:
    +def print_dist_installation_info(
    +    latest: str, dist: BaseDistribution | None, show_on_quiet: bool = True

    Why did you add show_on_quiet=True here? Nothing else is changing this parameter, so why not just pass it to write_output explicitly?

    sepehr-rs sepehr-rs commented on code on Jul 08, 2026 16:06 UTC
    Code Review
    src/pip/_internal/commands/show.py:1
    @@ -187,6 +187,7 @@ def print_results(
         Print the information from installed distributions found.
         """
         results_printed = False
    +

    ?

    sepehr-rs sepehr-rs requested changes on Jul 08, 2026 16:24 UTC
    Changes Requested Reviewer

    Thanks for addressing the review comments. I've left a couple more; let me know if you need any help with them.

    ychampion ychampion commented on code on Jul 08, 2026 16:39 UTC
    Code Review
    src/pip/_internal/commands/search.py:1
    @@ -113,21 +113,26 @@ def transform_hits(hits: list[dict[str, str]]) -> list[TransformedHit]:
         return list(packages.values())
     
     
    -def print_dist_installation_info(latest: str, dist: BaseDistribution | None) -> None:
    +def print_dist_installation_info(
    +    latest: str, dist: BaseDistribution | None, show_on_quiet: bool = True

    Agreed. Removed the helper parameter and passed show_on_quiet=True directly at the write_output calls.

    ychampion ychampion commented on code on Jul 08, 2026 16:39 UTC
    Code Review
    src/pip/_internal/commands/show.py:1
    @@ -187,6 +187,7 @@ def print_results(
         Print the information from installed distributions found.
         """
         results_printed = False
    +

    Cleaned up; that blank-line-only diff is gone now.

    sepehr-rs sepehr-rs commented on Jul 09, 2026 14:07 UTC
    Contributor

    Hey @notatallshaw, would you mind taking a look at this when you have a chance? You mentioned earlier that you'd be happy to review a PR for this issue if someone submitted one. Thanks!

    Reviewers
    Assignees
    ychampion ychampion
    Labels
    risk: low confidence: 84.4% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 69.9% (HIGH) · Higher = larger blast radius
    69.9%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 84.4% · Lower = rushed or shallow review
    84.4%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 10.9% · Safe to merge
    10.9%
    Repository