From 2aead00d014f57b97e61a181ffcc6edc4523c057 Mon Sep 17 00:00:00 2001 From: keakon Date: Wed, 18 Dec 2024 18:27:42 +0800 Subject: [PATCH 1/3] support discovering pyc/pyd/so modules --- taskiq/cli/utils.py | 15 ++++++++++----- tests/cli/test_utils.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/taskiq/cli/utils.py b/taskiq/cli/utils.py index aa3e591..827c10e 100644 --- a/taskiq/cli/utils.py +++ b/taskiq/cli/utils.py @@ -6,8 +6,6 @@ from pathlib import Path from typing import Any, Generator, List, Sequence, Union -from taskiq.utils import remove_suffix - logger = getLogger("taskiq.worker") @@ -91,9 +89,16 @@ def import_tasks( discovered_modules = set() for glob_pattern in pattern: for path in Path().glob(glob_pattern): - discovered_modules.add( - remove_suffix(str(path), ".py").replace(os.path.sep, "."), - ) + if path.is_file(): + if path.suffix in (".py", ".pyc", ".pyd", ".so"): + # remove all suffixes + prefix = path.name.partition(".")[0] + discovered_modules.add( + str(path.with_name(prefix)).replace(os.path.sep, ".") + ) + # ignore other files + else: + discovered_modules.add(str(path).replace(os.path.sep, ".")) modules.extend(list(discovered_modules)) import_from_modules(modules) diff --git a/tests/cli/test_utils.py b/tests/cli/test_utils.py index ebc85a8..0eae670 100644 --- a/tests/cli/test_utils.py +++ b/tests/cli/test_utils.py @@ -1,3 +1,4 @@ +from pathlib import Path from unittest.mock import patch from taskiq.cli.utils import import_tasks @@ -41,3 +42,31 @@ def test_import_tasks_no_discover() -> None: import_tasks(modules, "tests/**/test_utils.py", False) assert modules == ["taskiq.tasks"] mock.assert_called_with(modules) + + +def test_import_tasks_non_py_list_pattern() -> None: + modules = ["taskiq.tasks"] + with patch("taskiq.cli.utils.import_from_modules", autospec=True) as mock: + pathes = ( + Path("tests/test1.so"), + Path("tests/cli/test2.cpython-313-darwin.so"), + ) + for path in pathes: + path.touch() + + try: + import_tasks(modules, ["tests/**/test_utils.py", "tests/**/*.so"], True) + assert set(modules) == { + "taskiq.tasks", + "tests.test_utils", + "tests.cli.test_utils", + "tests.test1", + "tests.cli.test2", + } + mock.assert_called_with(modules) + finally: + for path in pathes: + try: + path.unlink() + except FileNotFoundError: + pass From 3620dec5ac0eaf041e736707d37c3bbad0db67df Mon Sep 17 00:00:00 2001 From: keakon Date: Fri, 20 Dec 2024 13:35:53 +0800 Subject: [PATCH 2/3] fix lint error --- tests/cli/test_utils.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/cli/test_utils.py b/tests/cli/test_utils.py index 0eae670..bf8651d 100644 --- a/tests/cli/test_utils.py +++ b/tests/cli/test_utils.py @@ -1,3 +1,4 @@ +from contextlib import suppress from pathlib import Path from unittest.mock import patch @@ -66,7 +67,5 @@ def test_import_tasks_non_py_list_pattern() -> None: mock.assert_called_with(modules) finally: for path in pathes: - try: + with suppress(FileNotFoundError): path.unlink() - except FileNotFoundError: - pass From efb348846a97e383d7d0ee6e98081fea2121cadb Mon Sep 17 00:00:00 2001 From: keakon Date: Wed, 25 Dec 2024 09:18:05 +0800 Subject: [PATCH 3/3] add trailing comma --- taskiq/cli/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/taskiq/cli/utils.py b/taskiq/cli/utils.py index 827c10e..22c15ae 100644 --- a/taskiq/cli/utils.py +++ b/taskiq/cli/utils.py @@ -94,7 +94,7 @@ def import_tasks( # remove all suffixes prefix = path.name.partition(".")[0] discovered_modules.add( - str(path.with_name(prefix)).replace(os.path.sep, ".") + str(path.with_name(prefix)).replace(os.path.sep, "."), ) # ignore other files else: