Skip to content

Conversation

asvarga-sedaro
Copy link

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 of CodegenState
  • the "".join(state.tokens) string of code

Test Plan

  • hatch run test passes
  • 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.

@facebook-github-bot
Copy link

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!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 18, 2024
Copy link

codecov bot commented Jul 30, 2024

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 ⚠️
libcst/_nodes/module.py 57.14% 3 Missing ⚠️
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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@zsol zsol left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please write tests exercising the new code


@add_slots
@dataclass(frozen=False)
class CodegenWriter(CodegenState):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the class hierarchy should be the other way around. CodegenState should specialize CodegenWriter.

Then we wouldn't have to have an awkward default value for writer.

Comment on lines +94 to +95
def pop_trailing_newline(self) -> None:
pass
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is incorrect, code that doesn't end in a trailing newline won't be serialized properly (for example the python module "" will be serialized as "\n")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants