-
-
Notifications
You must be signed in to change notification settings - Fork 644
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
[Call-by-name] Replaced ast
with libcst
in migration code
#21089
[Call-by-name] Replaced ast
with libcst
in migration code
#21089
Conversation
In subsequent PRs (or, as part of subsequent migrations), I'll add some more Overall, less code, more functionality |
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.
I read through it, but my understanding of the call by name conversion is superficial, so someone else needs to review this in more depth. From what I can tell, LGTM.
FWIW the end result should be "same as before, except with some bugs fixed". Most of the re-factoring was what was required to switch from AST to libcst. The only "novelty" was one |
@@ -167,115 +176,6 @@ def _create_migration_plan( | |||
|
|||
return sorted(items, key=lambda x: (x["filepath"], x["function"])) | |||
|
|||
def _create_replacements_for_file( |
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.
Not needed, handled by libcst transformer
logging.error(f"TokenError in {file}: {e}") | ||
return [] | ||
|
||
def _perform_replacements_on_file(self, file: Path, replacements: list[Replacement]): |
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.
Not needed, handled by libcst transformer
|
||
def contains_comments(self, source_code: str) -> bool: |
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.
Not needed, handled by libcst. ast
wipes out comments by default, cst
keeps them
@@ -9,6 +9,7 @@ chevron==0.14.0 | |||
fasteners==0.16.3 | |||
freezegun==1.2.1 | |||
ijson==3.2.3 | |||
libcst==1.4.0 |
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.
do we want to guard this from leaking into pants runtime (beyond the migration code)?
Lines 31 to 44 in 777c282
__dependents_rules__( | |
( # Only the explorer server may depend on these libraries | |
( | |
"[fastapi]", | |
"[starlette]", | |
"[strawberry-graphql]", | |
"[uvicorn]", | |
), | |
"pants-plugins/pants_explorer/server/**", | |
"!*", | |
), | |
# Free for all on the rest | |
("*", "*"), | |
) |
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.
I think libcst is an excellent utility for migrations, of which this will not be the last. Allowing broader use of it seems like a plus to me. So I don't think we should restrict this dependency with dependency visibility rules.
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.
I was feeling this could be useful as well, raised the question to get a conscious decision for it though, as there's been an on-going effort to reduce the number of 3rd party dependencies (albeit controversial at times) in #19282, so it would be good to have a clear record on the motivation for each dependency and it's intended use, I think.
Pulling these from source comments to main:
I agree :)
I also agree :) The motivation here is that libcst will be used in some I agree, overall, that we would like to reduce dependencies - or visibility them out. I wonder if pex's new PEP723 support (or |
This PR addresses several
ast
-centric implementation problems. Using a concrete syntax tree allows us to do fancier migrations, and not have to write code to handle silly things like trailing commas or nested comments.High level,
libcst
is amazing. All of the most complex code in the migration was replaced by a better transformer.ast
)implicitly
usage (closes [Call-by-name] Handle implicitly cases better #21072) - for example, functions that take no arguments no longer have animplicitly
attachedrule
-decorated async functions) CAN BE correctly transformed (right now, the rule engine itself doesn't seem to pick these up)The
implicitly
work done in this PR will eventually get pulled out into a custom linter/type-checker, as outside of this migration - we'll want to flag when users add new code with unnecessary implicitly usage.Closes #21040