Instagram / LibCST

A concrete syntax tree parser and serializer library for Python that preserves many aspects of Python's abstract syntax tree
https://libcst.readthedocs.io/
Other
1.56k stars 192 forks source link

Enable writing module code directly to file #1173

Open asvarga-sedaro opened 4 months ago

asvarga-sedaro commented 4 months ago

Summary

Enables generating code directly into files:

with open('code.py', 'w') as f:
    module.write_code(f)

rather than

code = module.code
with open('code.py', 'w') as f:
    f.write(code)

It does this with a subclass of CodegenState that avoids building in memory:

Test Plan

Notes

facebook-github-bot commented 4 months ago

Hi @asvarga-sedaro!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

codecov[bot] commented 3 months ago

Codecov Report

Attention: Patch coverage is 59.09091% with 9 lines in your changes missing coverage. Please review.

Project coverage is 91.23%. Comparing base (72701e4) to head (8767302). Report is 2 commits behind head on main.

Files Patch % Lines
libcst/_nodes/internal.py 60.00% 6 Missing :warning:
libcst/_nodes/module.py 57.14% 3 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1173 +/- ## ========================================== - Coverage 91.26% 91.23% -0.03% ========================================== Files 261 261 Lines 26877 26898 +21 ========================================== + Hits 24529 24541 +12 - Misses 2348 2357 +9 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.