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

Use libcst for applying suggestions #360

Open
JelleZijlstra opened this issue Dec 22, 2021 · 1 comment
Open

Use libcst for applying suggestions #360

JelleZijlstra opened this issue Dec 22, 2021 · 1 comment

Comments

@JelleZijlstra
Copy link
Contributor

LibCST would be a better fit for applying changes to the code, like the suggested types from #358. Currently, we have a mechanism that can replace an AST node and decompile it with ast-decompiler, but that's fragile and doesn't preserve comments.

The obvious option would be to use LibCST instead of ast.parse for the whole type checker. However, I'm worried that that will kill performance: running https://github.com/JelleZijlstra/autotyping on our codebase is noticeably slower than running pyanalyze, and pyanalyze has to do a lot more work itself in addition to parsing and running visitors.

Possible approaches:

  • Only when we try to apply a suggestion, use libcst to parse the file and change it.
  • Generate a json file with suggestions that can then be picked up by a libcst codemod and applied. I'd probably use autotyping for this.

Either way, we'd use the line and column number to match up ast and libcst nodes.

@JelleZijlstra
Copy link
Contributor Author

I decided on option 2, implemented in #361 and JelleZijlstra/autotyping#25.

It still needs a more convenient interface, but it works:

$ python -m pyanalyze --json-output failures.json pyanalyze/node_visitor.py -v -e suggested_return_type -e suggested_parameter_type
$ python -m libcst.tool codemod autotyping.AutotypeCommand pyanalyze/node_visitor.py --pyanalyze-report failures.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant