Repository navigation
fix: handle async with/def/for + semicolon + # fmt: skip crash (issue #5307) - #5314
Closed
HosnainRafi wants to merge 1 commit into
Closed
HosnainRafi wants to merge 1 commit into
HosnainRafi wants to merge 1 commit into
Conversation
When a one-line async with/def/for statement has a semicolon-separated body and a `# fmt: skip` comment, Black crashes because the ASYNC token is not included in the ignored nodes. The ASYNC token is a sibling of the compound statement (inside async_stmt), not a child, so it was being excluded from the header preservation logic. This fix ensures that when a compound statement is wrapped in an async_stmt, the ASYNC token is included in the header leaves so the entire statement is preserved inline. Fixes psf#5307
3 of 4 tasks
Collaborator
|
Thanks! Sorry, but I ended up going with #5311 due to the more concise and targeted fix. |
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.
Summary
Black crashes with a
ParseErrorwhen formatting a one-lineasync withorasync defstatement that has a semicolon-separated body and a# fmt: skipcomment:Root Cause
The
# fmt: skiphandling incomments.pypreserves the entire compound statement header inline when the body contains semicolons. However, forasyncstatements, theASYNCtoken is a sibling of the compound statement inside theasync_stmtwrapper node — it is not a child of thewith_stmt/funcdef. This means the header collection logic in_get_compound_statement_headerwas missing theasynckeyword, causing Black to split it onto its own line during the first pass. On the second pass,asyncalone on a line is not valid Python, resulting in a parse error.Fix
In
_get_compound_statement_header: when the compound statement's parent is anasync_stmt, also include theASYNCtoken in the header leaves.In
_find_compound_statement_context: added a new Case 3 to handle theasync_stmtwrapper structure (async_stmt -> compound_stmt -> simple_stmt).Tests
tests/data/cases/fmtskip10.pyforasync defandasync withwith semicolon bodies and# fmt: skip.