Preserve inline comments inside annotation subscripts#5130
Merged
Conversation
cobaltt7
reviewed
May 16, 2026
Collaborator
cobaltt7
left a comment
There was a problem hiding this comment.
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.
cobaltt7
reviewed
May 16, 2026
7c5c1d5 to
5e02bac
Compare
Contributor
Author
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. |
JelleZijlstra
approved these changes
May 18, 2026
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` |  |  | --- ### 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[...]`) ([#​5130](psf/black#5130)) - Preserve inline comments (including `# type: ignore`) immediately before a `# fmt: skip` line, avoiding AST equivalence failures ([#​5139](psf/black#5139)) ##### Packaging - Correct the version in the published executables ([#​5137](psf/black#5137)) ##### Documentation - Add Neovim integration guide covering conform.nvim, ALE, and simple command approaches ([#​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` |  |  | --- ### 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[...]`) ([#​5130](psf/black#5130)) - Preserve inline comments (including `# type: ignore`) immediately before a `# fmt: skip` line, avoiding AST equivalence failures ([#​5139](psf/black#5139)) ##### Packaging - Correct the version in the published executables ([#​5137](psf/black#5137)) ##### Documentation - Add Neovim integration guide covering conform.nvim, ALE, and simple command approaches ([#​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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #4733.
Black crashes with
INTERNAL ERROR: produced different code on the second pass of the formatteron inputs like:Root cause
The collapsed line is one char over the limit thanks to the inline comment
on the subscript's
[. That's enough forrun_transformerto try theFORCE_OPTIONAL_PARENTHESESsecond opinion, which wraps the annotation ininvisible 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_transformerwhenresult[0]only goes overbecause of comments attached to a subscript
[(LSQB). The comment stopsmigrating, formatting converges in one or two passes, and it stays where
the user wrote it.
Checklist - did you ...
--previewstyle, following the stability policy?CHANGES.mdif necessary?