Instagram / Instagram/LibCST

TypeError on parsing some valid expressions

Open
#1,061 0 comments 0 reactions 0 assignees View on GitHub
bug parsing
Dominant language
Python
Stars
1.9k
Forks
229
PR merge metrics
No merged PRs in 30d

Description

The following code:

```python
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
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 "", 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.