Keep the required trailing comma of a one-element subscript under --skip-magic-trailing-comma - #5272
Open
LuShadowX wants to merge 3 commits into
Open
Keep the required trailing comma of a one-element subscript under --skip-magic-trailing-comma#5272LuShadowX wants to merge 3 commits into
LuShadowX wants to merge 3 commits into
Conversation
hug_power_op rebuilt the line from cloned leaves, and a clone has no parent, so Line.append's tree-based guards could not tell a syntactically required trailing comma from a magic one. Under --skip-magic-trailing-comma the comma of a one-element subscript was dropped, turning a[x,] into a[x] and failing Black's own AST safety check. Only the operands around a hugged ** need a copy now; every other leaf is reused.
Author
|
One more data point: 6000 randomised examples through |
cobaltt7
reviewed
Jul 29, 2026
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
--skip-magic-trailing-commadrops the trailing comma from a one-element subscript when the line is long enough to be split and contains a power operator, which turnsa[x,](indexing with a one-tuple) intoa[x]and trips Black's own AST safety check:The comma survives on a short line —
tests/data/cases/skip_magic_trailing_comma.pyopens by asserting exactly that — so this is the same rule failing on a longer one. Under--fastthere is no safety net and the changed code is written out.hug_power_opis the path. It rebuilds the line withleaf.clone()for every leaf, and a clone has no parent, so both tree-based guards that protect a required comma stop working:has_magic_trailing_comma's single-element-subscript branch needsclosing.parent.type == syms.trailer, and the one-tuple guard inLine.appendneedsleaf.parent. Neither can fire, the comma looks magic, andremove_trailing_comma()takes it.Only the operands either side of a hugged
**actually need their prefix rewritten, so this stops cloning the rest and reuses the leaf, keeping it attached to the tree. Reusing leaves across lines is whatbracket_split_build_linealready does, and withpreformatted=TrueLine.appenddoes not touch the prefix — it only re-runsBracketTracker.mark, which recomputes the same metadata for an identical leaf sequence.The one-tuple guard the same clone breaks (
foo(lambda x=(1,): x), already in that case file) is fixed by this too; I could not build a failing example for it because the lambda cases are short enough not to reachhug_power_op.Found with a randomised run of
scripts/fuzz.py's property — the committed harness pinsderandomize=True, max_examples=1000, so it re-explores the same thousand inputs on every CI run and never reaches this one.Verification
tests/data/cases/skip_magic_trailing_comma.py. It fails onmain(alpha[beta]()in the output) and passes with the change.pytest: 474 passed, 3 skipped, unchanged frommain.black --check .,flake8,isort,mypyclean.--skip-magic-trailing-commabefore and after, comparing output hashes: zero differences, no errors either way. Power-operator hugging is unaffected (x = a ** bstill becomesx = a**b).Checklist - did you ...
--previewstyle, following the stability policy? — no style change; this restores the documented behaviour for a case that was crashingCHANGES.mdif necessary?