Fix crash on standalone comments in comprehensions and lambdas#5144
Open
Pedro-Muller29 wants to merge 5 commits into
Open
Fix crash on standalone comments in comprehensions and lambdas#5144Pedro-Muller29 wants to merge 5 commits into
Pedro-Muller29 wants to merge 5 commits into
Conversation
0f6c882 to
61af152
Compare
Collaborator
|
Thanks!
Yeah, that's not ideal indentation. However, that's already handled with the [[
x
for x
# comment
in [
# comment
"ABC"
]
]]On the other hand, that style is currently restricted to If possible, it'd be nice to improve that here as well, but if it's a lot of extra code it's fine to merge as-is and depend on |
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 #4296.
Three inputs from the issue crash on the first formatting pass:
[ x for # comment x, y in ["AB",] ] [ [ x for x # comment in [ # comment "ABC" ] ] ] {(lambda # comment x: [ # comment ] )}All of them blow up with
Cannot parse: ... bad inputbecause Black dumps a string where a standalone comment sits inline next to the following token, and Python's tokenizer swallows everything to the next newline.Root cause
Two distinct things.
For cases B and C,
transform_lineruns through its splitters and, when nothing succeeds, falls through toyield line.Line.__str__concatenates leaves without inserting newlines, so aSTANDALONE_COMMENTmid-stream renders glued to whatever follows.Line.append_safeenforces "comment on its own line" only at bracket depth 0 or inside an openfor/lambda. For comments living inside arbitrary nested brackets, there's no guard.For case A,
standalone_comment_splitdoes pull the comment betweenforand the tuple target onto its own line, but the resulting sub-linex, y in ["AB",]no longer carries theforkeyword. Its bracket tracker walks fresh, the comma betweenxandylands at depth 0, getsCOMMA_PRIORITY, anddelimiter_splitbreaks the tuple apart. Black also tacks a stray trailing comma onto the iterable.Fix
linegen.py: when no transformer succeeds and the line still carries standalone comments, fall back to_force_standalone_comment_split, which yields each comment on its own line. The alternative is invalid Python, so the unconditional split is safe.brackets.py:is_split_after_delimiterreturns 0 for commas whose parent isexprlistand whose grandparent iscomp_for/old_comp_for/for_stmt. Those commas are tuple-packing, not list delimiters.The fixture covers the three issue cases plus async-for and a nested tuple target (
for (a, b), c in ...) to lock both fixes in.Flagging that case B comes out with loose indentation:
[ [x for x # comment in [ # comment "ABC"]] ]The inner
[...]doesn't re-explode, so the comment and"ABC"]]sit at the wrong depth. Let me know if there's a target shape you'd want, or if this is fine for the crash fix.Checklist - did you ...
--previewstyle, following the stability policy?CHANGES.mdif necessary?