Skip to content

Commit 2aead00

Browse files
committed
support discovering pyc/pyd/so modules
1 parent ae6b214 commit 2aead00

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

taskiq/cli/utils.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
from pathlib import Path
77
from typing import Any, Generator, List, Sequence, Union
88

9-
from taskiq.utils import remove_suffix
10-
119
logger = getLogger("taskiq.worker")
1210

1311

@@ -91,9 +89,16 @@ def import_tasks(
9189
discovered_modules = set()
9290
for glob_pattern in pattern:
9391
for path in Path().glob(glob_pattern):
94-
discovered_modules.add(
95-
remove_suffix(str(path), ".py").replace(os.path.sep, "."),
96-
)
92+
if path.is_file():
93+
if path.suffix in (".py", ".pyc", ".pyd", ".so"):
94+
# remove all suffixes
95+
prefix = path.name.partition(".")[0]
96+
discovered_modules.add(
97+
str(path.with_name(prefix)).replace(os.path.sep, ".")
98+
)
99+
# ignore other files
100+
else:
101+
discovered_modules.add(str(path).replace(os.path.sep, "."))
97102

98103
modules.extend(list(discovered_modules))
99104
import_from_modules(modules)

tests/cli/test_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
from unittest.mock import patch
23

34
from taskiq.cli.utils import import_tasks
@@ -41,3 +42,31 @@ def test_import_tasks_no_discover() -> None:
4142
import_tasks(modules, "tests/**/test_utils.py", False)
4243
assert modules == ["taskiq.tasks"]
4344
mock.assert_called_with(modules)
45+
46+
47+
def test_import_tasks_non_py_list_pattern() -> None:
48+
modules = ["taskiq.tasks"]
49+
with patch("taskiq.cli.utils.import_from_modules", autospec=True) as mock:
50+
pathes = (
51+
Path("tests/test1.so"),
52+
Path("tests/cli/test2.cpython-313-darwin.so"),
53+
)
54+
for path in pathes:
55+
path.touch()
56+
57+
try:
58+
import_tasks(modules, ["tests/**/test_utils.py", "tests/**/*.so"], True)
59+
assert set(modules) == {
60+
"taskiq.tasks",
61+
"tests.test_utils",
62+
"tests.cli.test_utils",
63+
"tests.test1",
64+
"tests.cli.test2",
65+
}
66+
mock.assert_called_with(modules)
67+
finally:
68+
for path in pathes:
69+
try:
70+
path.unlink()
71+
except FileNotFoundError:
72+
pass

0 commit comments

Comments
 (0)