← Back to Pull requests

Fix unstable formatting with a comment and an implicitly concatenated last argument #5587

Closed
unknown closed this pull request without merging on Oct 24, 2026 23:18 UTC
View on GitHub
unknown unknown commented on Oct 23, 2026 01:28 UTC
Author

Description

On main, Black fails on this code with "INTERNAL ERROR: Black ... produced different code on the second pass of the formatter" and exits with 123:

parser.add_argument("--retries",  # 0 disables
                    help="Number of times to retry "
                         "before giving up")

It happens when a comment follows an argument and the last argument is an implicitly concatenated string. The arguments fit on one line inside the call, so the first pass joins them and prints the comment at the end, after the strings. On the next run the comment belongs to the last string, which keeps the strings apart, and the split adds a trailing comma that is printed before the comment. The run after that joins the strings again. With --skip-magic-trailing-comma the output is stable, but the comment ends up after the help= argument.

contains_implicit_multiline_string_with_comments now also returns True when the line ends with an implicitly concatenated string and has a comment anywhere, since joining the line would print that comment right after the strings. The line is then split by delimiter, so the comment stays on the argument it was written after, in both modes:

parser.add_argument(
    "--retries",  # 0 disables
    help="Number of times to retry " "before giving up",
)

Already formatted code doesn't change.

Ran: pytest tests --run-optional no_jupyter, then the jupyter tests with the extra installed, black --check ., isort, flake8 and prettier at the pre-commit versions, and mypy (no new findings).
Checked: about 5,600 CPython 3.14 stdlib statements with a comment injected near an implicit concatenation, and another 5,600 with # type: ignore / # noqa comments, formatted with and without the patch in default, -C, -l 79/100/120, preview and unstable modes (the second set without -l 100 and unstable). Unstable results went from 111 and 91 to 0; the only other changed outputs are 12 and 15 -C cases where the comment now stays on its argument. Re-formatting the output of main with the patch changes nothing, and about 1,700 stdlib files format identically.

Checklist - did you ...

  • Implement any code style changes under the --preview style, following the stability policy?

  • Add an entry in CHANGES.md if necessary?

  • Add / update tests if necessary?

  • Add new / update outdated documentation?
  • Diff: 303 lines (+227 / -76) across 3 files Reviewer: nk-batman21 Review duration: 2749m
    All Vouch review confidence checks passed
    Residual risk 0.17 = Change Risk (0.40) × (1 − Review Confidence (0.57)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.40 · 303 lines diff across 3 files
    0.40
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    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.342
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #5587"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% 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: 40.1% (MEDIUM) · Higher = larger blast radius
    40.1%
    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: 56.8% · Lower = rushed or shallow review
    56.8%
    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: 17.3% · Safe to merge
    17.3%
    Repository