quora / pyanalyze

A Python type checker
Apache License 2.0
339 stars 35 forks source link

Use libcst for applying suggestions #360

Open JelleZijlstra opened 2 years ago

JelleZijlstra commented 2 years ago

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:

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

JelleZijlstra commented 2 years ago

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