Instagram / LibCST

A concrete syntax tree parser and serializer library for Python that preserves many aspects of Python's abstract syntax tree
https://libcst.readthedocs.io/
Other
1.57k stars 192 forks source link

Empty brackets around the `case` of a `match-case` are incorrectly parsed as a pair of square brackets #1096

Closed sirosen closed 10 months ago

sirosen commented 10 months ago

This is an oddity which results in data not roundtripping through libcst accurately. It also makes it impossible to know what the original file contents were.

If a match-case is used and the case is an unparenthesized, unbracketed expression, libcst parses this to have lbracket and rbracket incorrectly populated.

Here's a reproducer:

x = (1, 2)
match x:
    case 1, 2:
        print("x matches (1, 2)")
    case _:
        print("x does not match (1, 2)")

Attempting to simply load and dump this module produces case [1, 2]:.

Inspecting the results of parse_module, I find a parse shaped like...

pattern=MatchList(...,
  lbracket=LeftSquareBracket(
    whitespace_after=SimpleWhitespace(
      value='',
    ),
  ),
  rbracket=RightSquareBracket(
    whitespace_before=SimpleWhitespace(
      value='',
    ),
  ),
  lpar=[],
  rpar=[],
)
zsol commented 10 months ago

Whoa this is pretty bad

zsol commented 10 months ago

Thank you for the report!