Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling of standalone match/case with newlines/comments #2760

Merged
merged 2 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
be formatted. (#2526)
- Speed-up the new backtracking parser about 4X in general (enabled when
`--target-version` is set to 3.10 and higher). (#2728)
- Fix handling of standalone `match()` or `case()` when there is a trailing newline or a
comment inside of the parentheses. (#2760)

### Packaging

Expand Down
4 changes: 4 additions & 0 deletions src/blib2to3/pgen2/parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ def addtoken(self, type: int, value: Text, context: Context) -> bool:
break

next_token_type, next_token_value, *_ = proxy.eat(counter)
if next_token_type in (tokenize.COMMENT, tokenize.NL):
counter += 1
continue

if next_token_type == tokenize.OP:
next_token_type = grammar.opmap[next_token_value]

Expand Down
68 changes: 66 additions & 2 deletions tests/data/pattern_matching_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,52 @@
): print(1)
case c(
very_complex=True,
perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1
perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1,
): print(2)
case a: pass

match(
arg # comment
)

match(
)

match(


)

case(
arg # comment
)

case(
)

case(


)


re.match(
something # fast
)
re.match(



)
match match(


):
case case(
arg, # comment
):
pass

# output

match something:
Expand All @@ -20,8 +62,30 @@
):
print(1)
case c(
very_complex=True, perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1
very_complex=True,
perhaps_even_loooooooooooooooooooooooooooooooooooooong=-1,
):
print(2)
case a:
pass

match(arg) # comment

match()

match()

case(arg) # comment

case()

case()


re.match(something) # fast
re.match()
match match():
case case(
arg, # comment
):
pass