-
Notifications
You must be signed in to change notification settings - Fork 192
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
Enable writing module code directly to file #1173
base: main
Are you sure you want to change the base?
Conversation
Hi @asvarga-sedaro! Thank you for your pull request and welcome to our community. Action RequiredIn 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. ProcessIn 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 If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks! |
Codecov ReportAttention: Patch coverage is
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. |
There was a problem hiding this 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
@@ -70,6 +71,30 @@ def record_syntactic_position( | |||
yield | |||
|
|||
|
|||
@add_slots | |||
@dataclass(frozen=False) | |||
class CodegenWriter(CodegenState): |
There was a problem hiding this comment.
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
.
def pop_trailing_newline(self) -> None: | ||
pass |
There was a problem hiding this comment.
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"
)
Summary
Enables generating code directly into files:
rather than
It does this with a subclass of
CodegenState
that avoids building in memory:tokens: List[str]
field ofCodegenState
"".join(state.tokens)
string of codeTest Plan
hatch run test
passesNotes
CodegenState.pop_trailing_newline()
isn't supported because the token is already written to the file. this is fixable if desired.