Skip to content

Commit 4919645

Browse files
committed
Fixed annotation support.
Signed-off-by: Pavel Kirilin <win10@list.ru>
1 parent c1cbd3b commit 4919645

2 files changed

Lines changed: 18 additions & 25 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,5 @@ def my_function(
362362
pass
363363

364364
```
365+
366+
Also, please note that if you're using `from __future__ import annotations` it won't work for python <= 3.9. Because the `inspect.signature` function doesn't support it. In all future versions it will work as expected.

taskiq_dependencies/graph.py

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import inspect
2+
import sys
23
from collections import defaultdict, deque
34
from graphlib import TopologicalSorter
4-
from typing import (
5-
Any,
6-
Callable,
7-
Dict,
8-
ForwardRef,
9-
List,
10-
Optional,
11-
TypeVar,
12-
get_type_hints,
13-
)
5+
from typing import Any, Callable, Dict, List, Optional, TypeVar, get_type_hints
146

157
from taskiq_dependencies.ctx import AsyncResolveContext, SyncResolveContext
168
from taskiq_dependencies.dependency import Dependency
@@ -115,6 +107,12 @@ def _build_graph(self) -> None: # noqa: C901, WPS210
115107
:raises ValueError: if something happened.
116108
"""
117109
dep_deque = deque([Dependency(self.target, use_cache=True)])
110+
# This is for `from __future__ import annotations` support.
111+
# We need to use `eval_str` argument, because
112+
# signature of the function is a string, not an object.
113+
signature_kwargs: Dict[str, Any] = {}
114+
if sys.version_info >= (3, 10):
115+
signature_kwargs["eval_str"] = True
118116

119117
while dep_deque:
120118
dep = dep_deque.popleft()
@@ -164,38 +162,31 @@ def _build_graph(self) -> None: # noqa: C901, WPS210
164162
# If this is a class, we need to get signature of
165163
# an __init__ method.
166164
hints = get_type_hints(origin.__init__) # noqa: WPS609
167-
sign = inspect.signature(origin.__init__) # noqa: WPS609
165+
sign = inspect.signature(
166+
origin.__init__, # noqa: WPS609
167+
**signature_kwargs,
168+
)
168169
elif inspect.isfunction(dep.dependency):
169170
# If this is function or an instance of a class, we get it's type hints.
170171
hints = get_type_hints(dep.dependency)
171-
sign = inspect.signature(origin) # type: ignore
172+
sign = inspect.signature(origin, **signature_kwargs) # type: ignore
172173
else:
173174
hints = get_type_hints(
174175
dep.dependency.__call__, # type: ignore # noqa: WPS609
175176
)
176-
sign = inspect.signature(origin) # type: ignore
177+
sign = inspect.signature(origin, **signature_kwargs) # type: ignore
177178

178179
# Now we need to iterate over parameters, to
179180
# find all parameters, that have TaskiqDepends as it's
180181
# default vaule.
181182
for param_name, param in sign.parameters.items():
182183
default_value = param.default
183-
annotation = param.annotation
184-
if isinstance(param.annotation, str):
185-
globalns = getattr(origin, "__globals__", {})
186-
annotation = ForwardRef( # type: ignore # noqa: WPS437
187-
param.annotation,
188-
)._evaluate(
189-
globalns,
190-
None,
191-
set(),
192-
)
193-
if hasattr(annotation, "__metadata__"): # noqa: WPS421
184+
if hasattr(param.annotation, "__metadata__"): # noqa: WPS421
194185
# We go backwards,
195186
# because you may want to override your annotation
196187
# and the overriden value will appear to be after
197188
# the original `Depends` annotation.
198-
for meta in reversed(annotation.__metadata__):
189+
for meta in reversed(param.annotation.__metadata__):
199190
if isinstance(meta, Dependency):
200191
default_value = meta
201192
break

0 commit comments

Comments
 (0)