Skip to content

Preserve inline comments inside annotation subscripts#5130

Merged
cobaltt7 merged 5 commits into
psf:mainfrom
Pedro-Muller29:regression-test-issue-4733
May 18, 2026
Merged

Preserve inline comments inside annotation subscripts#5130
cobaltt7 merged 5 commits into
psf:mainfrom
Pedro-Muller29:regression-test-issue-4733

Conversation

@Pedro-Muller29
Copy link
Copy Markdown
Contributor

Description

Fixes #4733.

Black crashes with INTERNAL ERROR: produced different code on the second pass of the formatter on inputs like:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa: list[  # pyright: ignore[...]
    int
] = []

Root cause

The collapsed line is one char over the limit thanks to the inline comment
on the subscript's [. That's enough for run_transformer to try the
FORCE_OPTIONAL_PARENTHESES second opinion, which wraps the annotation in
invisible parens and pushes the comment out of the subscript. On the next
parse the comment lands on a different leaf, the splitter picks a different
shape, and the second-pass stability check blows up.

Fix

Skip the second opinion in run_transformer when result[0] only goes over
because of comments attached to a subscript [ (LSQB). The comment stops
migrating, formatting converges in one or two passes, and it stays where
the user wrote it.

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?

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 14, 2026

diff-shades results comparing this PR (24ac9a0) to main (9cbd95f):

--preview style: no changes

--stable style: no changes


What is this? | Workflow run | diff-shades documentation

Copy link
Copy Markdown
Collaborator

@cobaltt7 cobaltt7 left a comment

Choose a reason for hiding this comment

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

With this change, the original test case is reformatted badly:

 def foo():
     possibly_redundant_lowlevel_checkpoints: list[  # pyright: ignore[reportUnknownVariableType]
         cst.BaseExpression
-    ] = field(default_factory=list)
+    ] = field(
+        default_factory=list
+    )

To be fair this is an improvement, but I think there's more work to be done on this issue.

Comment thread tests/test_format.py Outdated
@Pedro-Muller29 Pedro-Muller29 force-pushed the regression-test-issue-4733 branch from 7c5c1d5 to 5e02bac Compare May 17, 2026 21:39
@Pedro-Muller29
Copy link
Copy Markdown
Contributor Author

With this change, the original test case is reformatted badly:

 def foo():
     possibly_redundant_lowlevel_checkpoints: list[  # pyright: ignore[reportUnknownVariableType]
         cst.BaseExpression
-    ] = field(default_factory=list)
+    ] = field(
+        default_factory=list
+    )

To be fair this is an improvement, but I think there's more work to be done on this issue.

Both addressed in 5e02bac (moved tests under tests/data/cases/) and 24ac9a0 (keeps the omit-based RHS split so field(default_factory=list) no longer explodes).

CI is red but seems unrelated to my changes. Looks like Click 8.4.0 broke the FakeParameter stub at tests/test_black.py:108.

@cobaltt7 cobaltt7 merged commit 5809338 into psf:main May 18, 2026
57 checks passed
luketainton pushed a commit to luketainton/repos_roboluke that referenced this pull request May 23, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [black](https://github.com/psf/black) ([changelog](https://github.com/psf/black/blob/main/CHANGES.md)) | `<26.5.1,>=26.5.0` → `<26.5.2,>=26.5.1` | ![age](https://developer.mend.io/api/mc/badges/age/pypi/black/26.5.1?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/black/26.5.0/26.5.1?slim=true) |

---

### Release Notes

<details>
<summary>psf/black (black)</summary>

### [`v26.5.1`](https://github.com/psf/black/blob/HEAD/CHANGES.md#Version-2651)

[Compare Source](psf/black@26.5.0...26.5.1)

##### Stable style

- Fix unstable formatting of annotated assignments whose subscript annotation contains
  an inline comment (e.g. `x: list[  # pyright: ignore[...]`) ([#&#8203;5130](psf/black#5130))
- Preserve inline comments (including `# type: ignore`) immediately before a
  `# fmt: skip` line, avoiding AST equivalence failures ([#&#8203;5139](psf/black#5139))

##### Packaging

- Correct the version in the published executables ([#&#8203;5137](psf/black#5137))

##### Documentation

- Add Neovim integration guide covering conform.nvim, ALE, and simple command approaches
  ([#&#8203;5124](psf/black#5124))

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xODMuMSIsInVwZGF0ZWRJblZlciI6IjQzLjE4My4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJ0eXBlL2RlcGVuZGVuY2llcyJdfQ==-->

Reviewed-on: https://git.tainton.uk/repos/roboluke/pulls/450
Reviewed-by: Luke Tainton <luke@tainton.uk>
Co-authored-by: renovate[bot] <renovate-bot@git.tainton.uk>
Co-committed-by: renovate[bot] <renovate-bot@git.tainton.uk>
luketainton pushed a commit to luketainton/luke_instant-msg-api that referenced this pull request May 23, 2026
This PR contains the following updates:

| Package | Change | [Age](https://docs.renovatebot.com/merge-confidence/) | [Confidence](https://docs.renovatebot.com/merge-confidence/) |
|---|---|---|---|
| [black](https://github.com/psf/black) ([changelog](https://github.com/psf/black/blob/main/CHANGES.md)) | `<26.5.1,>=26.5.0` → `<26.5.2,>=26.5.1` | ![age](https://developer.mend.io/api/mc/badges/age/pypi/black/26.5.1?slim=true) | ![confidence](https://developer.mend.io/api/mc/badges/confidence/pypi/black/26.5.0/26.5.1?slim=true) |

---

### Release Notes

<details>
<summary>psf/black (black)</summary>

### [`v26.5.1`](https://github.com/psf/black/blob/HEAD/CHANGES.md#Version-2651)

[Compare Source](psf/black@26.5.0...26.5.1)

##### Stable style

- Fix unstable formatting of annotated assignments whose subscript annotation contains
  an inline comment (e.g. `x: list[  # pyright: ignore[...]`) ([#&#8203;5130](psf/black#5130))
- Preserve inline comments (including `# type: ignore`) immediately before a
  `# fmt: skip` line, avoiding AST equivalence failures ([#&#8203;5139](psf/black#5139))

##### Packaging

- Correct the version in the published executables ([#&#8203;5137](psf/black#5137))

##### Documentation

- Add Neovim integration guide covering conform.nvim, ALE, and simple command approaches
  ([#&#8203;5124](psf/black#5124))

</details>

---

### Configuration

📅 **Schedule**: (UTC)

- Branch creation
  - At any time (no schedule defined)
- Automerge
  - At any time (no schedule defined)

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0My4xODMuMSIsInVwZGF0ZWRJblZlciI6IjQzLjE4My4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJ0eXBlL2RlcGVuZGVuY2llcyJdfQ==-->

Reviewed-on: https://git.tainton.uk/luke/instant-msg-api/pulls/260
Reviewed-by: Luke Tainton <luke@tainton.uk>
Co-authored-by: renovate[bot] <renovate-bot@git.tainton.uk>
Co-committed-by: renovate[bot] <renovate-bot@git.tainton.uk>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Internal error: producing different code on second pass of the formatter

3 participants