Skip to content

Should invalid code be representable? #287

@Zac-HD

Description

@Zac-HD

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 😅

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions