Skip to content

Commit 28d8f2c

Browse files
committed
fixup! refactor(utils/decorators): rewrite remove task decorator to use ast
1 parent 606b2ea commit 28d8f2c

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

tests/utils/test_decorators.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def test_task_decorator_using_source(decorator: TaskDecorator):
4949
def f():
5050
return ["some_task"]
5151

52-
assert parse_python_source(f, "decorator") == 'def f():\n return ["some_task"]\n'
52+
assert parse_python_source(f, "decorator") == "def f():\n return ['some_task']"
5353

5454

5555
@pytest.mark.parametrize("decorator", DECORATORS, indirect=["decorator"])
@@ -59,7 +59,7 @@ def test_skip_if(decorator: TaskDecorator):
5959
def f():
6060
return "hello world"
6161

62-
assert parse_python_source(f, "decorator") == 'def f():\n return "hello world"\n'
62+
assert parse_python_source(f, "decorator") == "def f():\n return 'hello world'"
6363

6464

6565
@pytest.mark.parametrize("decorator", DECORATORS, indirect=["decorator"])
@@ -69,7 +69,7 @@ def test_run_if(decorator: TaskDecorator):
6969
def f():
7070
return "hello world"
7171

72-
assert parse_python_source(f, "decorator") == 'def f():\n return "hello world"\n'
72+
assert parse_python_source(f, "decorator") == "def f():\n return 'hello world'"
7373

7474

7575
def test_skip_if_and_run_if():
@@ -79,7 +79,7 @@ def test_skip_if_and_run_if():
7979
def f():
8080
return "hello world"
8181

82-
assert parse_python_source(f) == 'def f():\n return "hello world"\n'
82+
assert parse_python_source(f) == "def f():\n return 'hello world'"
8383

8484

8585
def test_run_if_and_skip_if():
@@ -89,7 +89,7 @@ def test_run_if_and_skip_if():
8989
def f():
9090
return "hello world"
9191

92-
assert parse_python_source(f) == 'def f():\n return "hello world"\n'
92+
assert parse_python_source(f) == "def f():\n return 'hello world'"
9393

9494

9595
def test_skip_if_allow_decorator():
@@ -102,7 +102,7 @@ def non_task_decorator(func):
102102
def f():
103103
return "hello world"
104104

105-
assert parse_python_source(f) == '@non_task_decorator\ndef f():\n return "hello world"\n'
105+
assert parse_python_source(f) == "@non_task_decorator\ndef f():\n return 'hello world'"
106106

107107

108108
def test_run_if_allow_decorator():
@@ -115,7 +115,7 @@ def non_task_decorator(func):
115115
def f():
116116
return "hello world"
117117

118-
assert parse_python_source(f) == '@non_task_decorator\ndef f():\n return "hello world"\n'
118+
assert parse_python_source(f) == "@non_task_decorator\ndef f():\n return 'hello world'"
119119

120120

121121
def parse_python_source(task: Task, custom_operator_name: str | None = None) -> str:

tests/utils/test_preexisting_python_virtualenv_decorator.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@
2222

2323
class TestExternalPythonDecorator:
2424
def test_remove_task_decorator(self):
25-
py_source = "@task.external_python(use_dill=True)\ndef f():\nimport funcsigs"
25+
py_source = "@task.external_python(use_dill=True)\ndef f(): ...\nimport funcsigs"
2626
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.external_python")
27-
assert res == "def f():\nimport funcsigs"
27+
assert res == "def f():\n ...\nimport funcsigs"
2828

2929
def test_remove_decorator_no_parens(self):
30-
py_source = "@task.external_python\ndef f():\nimport funcsigs"
30+
py_source = "@task.external_python\ndef f(): ...\nimport funcsigs"
3131
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.external_python")
32-
assert res == "def f():\nimport funcsigs"
32+
assert res == "def f():\n ...\nimport funcsigs"
3333

3434
def test_remove_decorator_nested(self):
35-
py_source = "@foo\n@task.external_python\n@bar\ndef f():\nimport funcsigs"
35+
py_source = "@foo\n@task.external_python\n@bar\ndef f(): ...\nimport funcsigs"
3636
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.external_python")
37-
assert res == "@foo\n@bar\ndef f():\nimport funcsigs"
37+
assert res == "@foo\n@bar\ndef f():\n ...\nimport funcsigs"
3838

39-
py_source = "@foo\n@task.external_python()\n@bar\ndef f():\nimport funcsigs"
39+
py_source = "@foo\n@task.external_python()\n@bar\ndef f(): ...\nimport funcsigs"
4040
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.external_python")
41-
assert res == "@foo\n@bar\ndef f():\nimport funcsigs"
41+
assert res == "@foo\n@bar\ndef f():\n ...\nimport funcsigs"

tests/utils/test_python_virtualenv.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,25 +116,20 @@ def test_should_create_virtualenv_with_extra_packages(self, mock_execute_in_subp
116116
mock_execute_in_subprocess.assert_called_with(["/VENV/bin/pip", "install", "apache-beam[gcp]"])
117117

118118
def test_remove_task_decorator(self):
119-
py_source = "@task.virtualenv(use_dill=True)\ndef f():\nimport funcsigs"
119+
py_source = "@task.virtualenv(use_dill=True)\ndef f(): ...\nimport funcsigs"
120120
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.virtualenv")
121-
assert res == "def f():\nimport funcsigs"
121+
assert res == "def f():\n ...\nimport funcsigs"
122122

123123
def test_remove_decorator_no_parens(self):
124-
py_source = "@task.virtualenv\ndef f():\nimport funcsigs"
124+
py_source = "@task.virtualenv\ndef f(): ...\nimport funcsigs"
125125
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.virtualenv")
126-
assert res == "def f():\nimport funcsigs"
127-
128-
def test_remove_decorator_including_comment(self):
129-
py_source = "@task.virtualenv\ndef f():\n# @task.virtualenv\nimport funcsigs"
130-
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.virtualenv")
131-
assert res == "def f():\n# @task.virtualenv\nimport funcsigs"
126+
assert res == "def f():\n ...\nimport funcsigs"
132127

133128
def test_remove_decorator_nested(self):
134-
py_source = "@foo\n@task.virtualenv\n@bar\ndef f():\nimport funcsigs"
129+
py_source = "@foo\n@task.virtualenv\n@bar\ndef f(): ...\nimport funcsigs"
135130
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.virtualenv")
136-
assert res == "@foo\n@bar\ndef f():\nimport funcsigs"
131+
assert res == "@foo\n@bar\ndef f():\n ...\nimport funcsigs"
137132

138-
py_source = "@foo\n@task.virtualenv()\n@bar\ndef f():\nimport funcsigs"
133+
py_source = "@foo\n@task.virtualenv()\n@bar\ndef f(): ...\nimport funcsigs"
139134
res = remove_task_decorator(python_source=py_source, task_decorator_name="@task.virtualenv")
140-
assert res == "@foo\n@bar\ndef f():\nimport funcsigs"
135+
assert res == "@foo\n@bar\ndef f():\n ...\nimport funcsigs"

0 commit comments

Comments
 (0)