Skip to content

Commit 6eb5989

Browse files
authored
Merge pull request #7 from taskiq-python/feature/class-deps
2 parents 823ab8a + 2615cf6 commit 6eb5989

2 files changed

Lines changed: 26 additions & 2 deletions

File tree

taskiq_dependencies/graph.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,13 @@ def _build_graph(self) -> None: # noqa: C901, WPS210
100100
# If this is a class, we need to get signature of
101101
# an __init__ method.
102102
hints = get_type_hints(dep.dependency.__init__) # noqa: WPS609
103-
else:
104-
# If this is function, we get it's type hints.
103+
elif inspect.isfunction(dep.dependency):
104+
# If this is function or an instance of a class, we get it's type hints.
105105
hints = get_type_hints(dep.dependency)
106+
else:
107+
hints = get_type_hints(
108+
dep.dependency.__call__, # type: ignore # noqa: WPS609
109+
)
106110

107111
# Now we need to iterate over parameters, to
108112
# find all parameters, that have TaskiqDepends as it's

tests/test_graph.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,23 @@ def target(info: ParamInfo = Depends()) -> None:
290290
info: ParamInfo = kwargs["info"]
291291
assert info.name == ""
292292
assert info.definition is None
293+
294+
295+
def test_class_based_dependencies() -> None:
296+
"""Tests that if ParamInfo is used on the target, no error is raised."""
297+
298+
class TeClass:
299+
def __init__(self, return_val: str) -> None:
300+
self.return_val = return_val
301+
302+
def __call__(self) -> str:
303+
return self.return_val
304+
305+
def target(class_val: str = Depends(TeClass("tval"))) -> None:
306+
return None
307+
308+
with DependencyGraph(target=target).sync_ctx() as g:
309+
kwargs = g.resolve_kwargs()
310+
311+
info: str = kwargs["class_val"]
312+
assert info == "tval"

0 commit comments

Comments
 (0)