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

Fails to parse parenthesized with statement without trailing comma #855

Closed
andersk opened this issue Jan 18, 2023 · 3 comments · Fixed by #861
Closed

Fails to parse parenthesized with statement without trailing comma #855

andersk opened this issue Jan 18, 2023 · 3 comments · Fixed by #861
Labels
bug Something isn't working parsing Converting source code into CST nodes

Comments

@andersk
Copy link

andersk commented Jan 18, 2023

#584 added parenthesized with statements to the native parser, but they’re only accepted when a trailing comma is present. Python accepts them with or without a trailing comma.

$ pip freeze
libcst==0.4.9
mypy-extensions==0.4.3
PyYAML==6.0
typing-inspect==0.8.0
typing_extensions==4.4.0

$ export LIBCST_PARSER_TYPE=native

$ python3 -c 'import libcst; libcst.parse_statement("with (a as a, b as b,): pass")'

$ python3 -c 'import libcst; libcst.parse_statement("with (a as a, b as b): pass")'
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/tmp/v/lib/python3.10/site-packages/libcst/_parser/entrypoints.py", line 138, in parse_statement
    result = _parse(
  File "/tmp/v/lib/python3.10/site-packages/libcst/_parser/entrypoints.py", line 55, in _parse
    return parse(source_str)
libcst._exceptions.ParserSyntaxError: Syntax Error @ 1:1.
parser error: error at 1:20: expected *

with (a as a, b as b): pass
^

Cc @stroxler

charliermarsh pushed a commit to astral-sh/ruff that referenced this issue Jan 18, 2023
This is slightly buggy due to Instagram/LibCST#855; it will complain `[ERROR] Failed to fix nested with: Failed to extract CST from source` when trying to fix nested parenthesized `with` statements lacking trailing commas. But presumably people who write parenthesized `with` statements already knew that they don’t need to nest them.

Signed-off-by: Anders Kaseorg <[email protected]>
@zsol zsol added bug Something isn't working parsing Converting source code into CST nodes labels Jan 24, 2023
@jakkdl
Copy link
Contributor

jakkdl commented Feb 14, 2023

I'm hitting this as well in https://github.com/Zac-HD/flake8-trio
It additionally also fails with newlines:

nvm, I didn't set the parser type.

@stroxler
Copy link
Contributor

Thanks for the bug reports - I'll try to tackle this in the next week.

@jakkdl
Copy link
Contributor

jakkdl commented Feb 15, 2023

oh I'm sorry, mine is due to not having done LIBCST_PARSER_TYPE=native

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parsing Converting source code into CST nodes
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants