Enable writing module code directly to file
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:
- the
tokens: List[str]field ofCodegenState - the
"".join(state.tokens)string of code
Test Plan
-
hatch run testpasses - we could add some tests that compare the contents of written files written directly vs indirectly
Notes
-
CodegenState.pop_trailing_newline()isn't supported because the token is already written to the file. this is fixable if desired.
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 [email protected]. Thanks!
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
@@ 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.