Instagram/LibCST

Should invalid code be representable?

Zac-HD opened this issue · 2 comments

Hi there! I've been working on Zac-HD/hypothesmith#2, generating random CSTs in order to turn them back in to Python source code and ruin someone's day help test parsers, autoformatters, and so on. LibCST has been fantastic for this - thanks so much!

In the process, I've encountered two cases where Hypothesmith generated a valid CST and could convert this to source code, but calling compile on the result gave a SyntaxError:

from libcst import *

import_node = Import(
    names=[ImportAlias(name=Attribute(value=Float(value="0."), attr=Name(value="A")))],
)
print(Module([import_node]).code)  # `import 0..A` is invalid syntax

try_node = Try(
    body=SimpleStatementSuite(body=[]),
    handlers=[
        ExceptHandler(body=SimpleStatementSuite(body=[])),
        ExceptHandler(body=SimpleStatementSuite(body=[])),
    ])
print(Module([try_node]).code)  # two `except:` clauses is invalid syntax

In the first case, the float 0. needs to be in parens. For the second, Try._validate would need to reject multiple except: handlers, or handlers where except: is present but not the last handler listed.

I don't know whether these odd behaviour will be considered a bug, since my use-case is quite unusual and I can work around them fairly easily, but it seemed worth reporting. If you do want to have _validate reject all invalid code though, I can definitely help find those cases 😅

zsol commented

As one of the main usecases of LibCST is to be able to construct source code, I think it should catch these kinds of errors. Bug or not, IMO it's worth addressing this :)

Those cases can be rejected by updating corresponding _validate functions and include the examples as test cases. We can use this thread to collect those cases and anyone who is interested in fixing can submit PRs.