-
Notifications
You must be signed in to change notification settings - Fork 14.3k
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
refactor(utils/decorators): rewrite remove task decorator to use ast #43383
Open
josix
wants to merge
2
commits into
apache:main
Choose a base branch
from
josix:refactor/remove_task_decorator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+54
−64
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,20 +22,20 @@ | |
|
||
class TestExternalPythonDecorator: | ||
def test_remove_task_decorator(self): | ||
py_source = "@task.external_python(use_dill=True)\ndef f():\nimport funcsigs" | ||
py_source = "@task.external_python(use_dill=True)\ndef f(): ...\nimport funcsigs" | ||
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.external_python") | ||
assert res == "def f():\nimport funcsigs" | ||
assert res == "def f():\n ...\nimport funcsigs" | ||
|
||
def test_remove_decorator_no_parens(self): | ||
py_source = "@task.external_python\ndef f():\nimport funcsigs" | ||
py_source = "@task.external_python\ndef f(): ...\nimport funcsigs" | ||
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.external_python") | ||
assert res == "def f():\nimport funcsigs" | ||
assert res == "def f():\n ...\nimport funcsigs" | ||
|
||
def test_remove_decorator_nested(self): | ||
py_source = "@foo\[email protected]_python\n@bar\ndef f():\nimport funcsigs" | ||
py_source = "@foo\[email protected]_python\n@bar\ndef f(): ...\nimport funcsigs" | ||
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.external_python") | ||
assert res == "@foo\n@bar\ndef f():\nimport funcsigs" | ||
assert res == "@foo\n@bar\ndef f():\n ...\nimport funcsigs" | ||
|
||
py_source = "@foo\[email protected]_python()\n@bar\ndef f():\nimport funcsigs" | ||
py_source = "@foo\[email protected]_python()\n@bar\ndef f(): ...\nimport funcsigs" | ||
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.external_python") | ||
assert res == "@foo\n@bar\ndef f():\nimport funcsigs" | ||
assert res == "@foo\n@bar\ndef f():\n ...\nimport funcsigs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,25 +116,20 @@ def test_should_create_virtualenv_with_extra_packages(self, mock_execute_in_subp | |
mock_execute_in_subprocess.assert_called_with(["/VENV/bin/pip", "install", "apache-beam[gcp]"]) | ||
|
||
def test_remove_task_decorator(self): | ||
py_source = "@task.virtualenv(use_dill=True)\ndef f():\nimport funcsigs" | ||
py_source = "@task.virtualenv(use_dill=True)\ndef f(): ...\nimport funcsigs" | ||
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.virtualenv") | ||
assert res == "def f():\nimport funcsigs" | ||
assert res == "def f():\n ...\nimport funcsigs" | ||
|
||
def test_remove_decorator_no_parens(self): | ||
py_source = "@task.virtualenv\ndef f():\nimport funcsigs" | ||
py_source = "@task.virtualenv\ndef f(): ...\nimport funcsigs" | ||
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.virtualenv") | ||
assert res == "def f():\nimport funcsigs" | ||
|
||
def test_remove_decorator_including_comment(self): | ||
py_source = "@task.virtualenv\ndef f():\n# @task.virtualenv\nimport funcsigs" | ||
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.virtualenv") | ||
assert res == "def f():\n# @task.virtualenv\nimport funcsigs" | ||
assert res == "def f():\n ...\nimport funcsigs" | ||
|
||
def test_remove_decorator_nested(self): | ||
py_source = "@foo\[email protected]\n@bar\ndef f():\nimport funcsigs" | ||
py_source = "@foo\[email protected]\n@bar\ndef f(): ...\nimport funcsigs" | ||
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.virtualenv") | ||
assert res == "@foo\n@bar\ndef f():\nimport funcsigs" | ||
assert res == "@foo\n@bar\ndef f():\n ...\nimport funcsigs" | ||
|
||
py_source = "@foo\[email protected]()\n@bar\ndef f():\nimport funcsigs" | ||
py_source = "@foo\[email protected]()\n@bar\ndef f(): ...\nimport funcsigs" | ||
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.virtualenv") | ||
assert res == "@foo\n@bar\ndef f():\nimport funcsigs" | ||
assert res == "@foo\n@bar\ndef f():\n ...\nimport funcsigs" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'm unsure if it is fine to remove the comments after executing
ast.unparse
. If it is not an allowed behavior, we might not be able to adopt ast as a solution, since it would not preserve comments.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 it would be better to use libcst https://pypi.org/project/libcst/ - which we alredy consider to use for our "upgrade check" tooling -> #41641 (comment)
this way we could preserve comments, line numbers etc. I think the important thing we might want to keep here is debuggability and particularly line number references, and with AST we will likely loose it - not only the comments.
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.
cc: @jscheffl @uranusjr @ashb @kaxil @tirkarthi ?
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 have no expertise in this matter so I can not judge this.
In general I'd favor is we refactor to ensure pytests are staying the same. I am also not sure why the line feed was removed in the other tests. Is this a compatibility limitation or just a cleanup?
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.
It's the difference of AST vs. CST.
(AST) Abstract Syntax Tree - > is the tree of syntax of Python code that contains only the "meaningful" tokens - that represent a "runtime" python bytecode (with stripped out comments and other irrelevant source code tokens - such as punctuations, idents, parentheses etc.) - https://en.wikipedia.org/wiki/Abstract_syntax_tree
(CST) Concrete Syntax Tree -> is the tree of syntax of Python code that represent the
source code
- not Python runtime bytecode (CST includes EOL characters and comments, indents and parentheses and punctuation marks) - but also all other non-runtime tokens - https://en.wikipedia.org/wiki/Parse_treeThere 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.
(that's a bit simplified definition of course - but I think it describes the difference well).
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.
If we're doing code modification instead of checking only, we probably should go with libcst I think 🤔
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 happened to share this topic at PyCon Taiwan this year. If anyone is interested, this was the slide I used. The content around page 67 is the most relevant.
https://speakerdeck.com/leew/unleash-the-chaos-developing-a-linter-for-un-pythonic-code?slide=67
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.
Yeah, I think it's better to leave dag authors' code information as much as possible in the rendered
script.py
of virtualenv operator, ideally the unit tests should not be changed, let me try LibCST instead of AST here.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.
If you need any example, I used it to check default_deferrable here https://github.com/apache/airflow/blob/main/scripts/ci/pre_commit/check_deferrable_default.py