Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with whitespace_after_* not fixable #1118

Open
AryazE opened this issue Mar 16, 2024 · 2 comments
Open

Issue with whitespace_after_* not fixable #1118

AryazE opened this issue Mar 16, 2024 · 2 comments
Labels
bug Something isn't working parsing Converting source code into CST nodes

Comments

@AryazE
Copy link

AryazE commented Mar 16, 2024

I have to transform (using a libcst.CSTTransformer) some 3rd party code, which might contain statements like assert("foo") == "foo".
Although this is a valid python code, I get the error libcst._nodes.base.CSTValidationError: Must have at least one space after 'assert'.
I cannot fix this in the leave_Assert hook, as it is called after validation, and I cannot fix it in the visit_Assert hook, as the object node is frozen.
Currently, the only option I can think of is to do a regex search and replace before LibCST to add the missing whitespace and then feed it to my transformer.
Is there any easier way to do this, preferably within LibCST?

@zsol zsol added bug Something isn't working parsing Converting source code into CST nodes labels May 15, 2024
@kiri11
Copy link
Contributor

kiri11 commented Jul 26, 2024

Seems like duplicate of #1095
UPDATE: Okay, maybe it's not the same, just similar. Here is assert, and there is while.
UPDATE2: I was wrong, this is not an issue with the validator, I was just trying to add a node without parentheses.

@kiri11
Copy link
Contributor

kiri11 commented Jul 26, 2024

@AryazE I had a similar issue. However, the LibCST validator is working correctly here by preventing me from generating incorrect code.

I fixed my code and might help you if you share what are you trying to do. I guess you did the same mistake by adding a new node without parentheses. LibCST does not automatically add whitespace or parentheses for you.

Here is what I did: just copied the parentheses from the node I was trying to replace:

return new_node.with_changes(
    lpar=updated_node.lpar,
    rpar=updated_node.rpar
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working parsing Converting source code into CST nodes
Projects
None yet
Development

No branches or pull requests

3 participants