Fix 3678 "problem parsing with
statements"
#4538
Merged
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
This fixes #3678. The fix is done by adding two new functions to
node.py
,is_tuple_containing_star
andis_generator
, which are then added to the checks inmaybe_make_parens_invisible_in_atom
insidelinegen.py
, which is used by the functionremove_with_parens
that Jelle suggested modifying in a comment on #3678Both functions are based on the existing
is_tuple_containing_walrus
. That makes me fairly confident inis_tuple_containing_star
, since it does the same thing asis_tuple_containing_walrus
, just withsyms.namedexpr_test
swapped forsyms.star_expr
. I'm less confident inis_generator
sincefor a in b
being calledsyms.old_comp_for
seems weird and I don't totally understandsyms
/_python_symbols
, but it seems to work.Side note,
_python_symbols
is really hard to work with sincenode.type
prints as an integer and the_python_symbol
variable <--> integer relationship is done viasetattr
, so I ended up having to do[print(x, eval(f"syms.{x}") for x in dir(syms) if x[0:2] != "__"]
to figure out what type generators get.As usual, both names subject to bikeshedding. I chose
is_tuple_containing_star
because it compares withsyms.star_expr
, butis_tuple_containing_unpacking
could also work. I choseis_generator
since the issue is with a generator, though that could be something else since it doesn't match thesyms.old_comp_for
comparison.The tests contain both a top level case and one where the issue is nested, since the nested case caused issues for me while making the fix.
Checklist - did you ...
CHANGES.md
if necessary?