Skip to content

Commit

Permalink
fixup! refactor(utils/decorators): rewrite remove task decorator to u…
Browse files Browse the repository at this point in the history
…se ast
  • Loading branch information
josix committed Oct 25, 2024
1 parent 606b2ea commit 1ec8a0a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 20 deletions.
14 changes: 7 additions & 7 deletions tests/utils/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def test_task_decorator_using_source(decorator: TaskDecorator):
def f():
return ["some_task"]

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


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

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


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

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


def test_skip_if_and_run_if():
Expand All @@ -79,7 +79,7 @@ def test_skip_if_and_run_if():
def f():
return "hello world"

assert parse_python_source(f) == 'def f():\n return "hello world"\n'
assert parse_python_source(f) == "def f():\n return 'hello world'"


def test_run_if_and_skip_if():
Expand All @@ -89,7 +89,7 @@ def test_run_if_and_skip_if():
def f():
return "hello world"

assert parse_python_source(f) == 'def f():\n return "hello world"\n'
assert parse_python_source(f) == "def f():\n return 'hello world'"


def test_skip_if_allow_decorator():
Expand All @@ -102,7 +102,7 @@ def non_task_decorator(func):
def f():
return "hello world"

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


def test_run_if_allow_decorator():
Expand All @@ -115,7 +115,7 @@ def non_task_decorator(func):
def f():
return "hello world"

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


def parse_python_source(task: Task, custom_operator_name: str | None = None) -> str:
Expand Down
21 changes: 8 additions & 13 deletions tests/utils/test_python_virtualenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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\n@task.virtualenv\n@bar\ndef f():\nimport funcsigs"
py_source = "@foo\n@task.virtualenv\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\n@task.virtualenv()\n@bar\ndef f():\nimport funcsigs"
py_source = "@foo\n@task.virtualenv()\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"

0 comments on commit 1ec8a0a

Please sign in to comment.