Instagram/LibCST

TypeError on parsing some valid expressions

DRMacIver opened this issue · 0 comments

The following code:

import libcst

libcst.parse_module('() if 0 else(lambda:())')

Raises the following error:

Traceback (most recent call last):
  File "/root/cstbreaker.py", line 5, in <module>
    libcst.parse_module('() if 0 else(lambda:())')
  File "/root/.pyenv/versions/3.11.6/lib/python3.11/site-packages/libcst/_parser/entrypoints.py", line 109, in parse_module
    result = _parse(
             ^^^^^^^
  File "/root/.pyenv/versions/3.11.6/lib/python3.11/site-packages/libcst/_parser/entrypoints.py", line 55, in _parse
    return parse(source_str)
           ^^^^^^^^^^^^^^^^^
  File "<string>", line 12, in __init__
  File "/root/.pyenv/versions/3.11.6/lib/python3.11/site-packages/libcst/_nodes/base.py", line 117, in __post_init__
    self._validate()
  File "/root/.pyenv/versions/3.11.6/lib/python3.11/site-packages/libcst/_nodes/expression.py", line 2553, in _validate
    and not self.orelse._safe_to_use_with_word_operator(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/root/.pyenv/versions/3.11.6/lib/python3.11/site-packages/libcst/_nodes/expression.py", line 2115, in _safe_to_use_with_word_operator
    return super()._safe_to_use_with_word_operator(position)

This example is sensitive to whitespace. For example, inserting a space between the else and the opening bracket causes it to start working (or at least not crash, I've not checked if the parse is correct).

This is running with libcst 1.1.0 on Python 3.11.6