Skip to content

Commit c1cbd3b

Browse files
committed
Added string annotations support.
Signed-off-by: Pavel Kirilin <win10@list.ru>
1 parent dde944d commit c1cbd3b

1 file changed

Lines changed: 22 additions & 3 deletions

File tree

taskiq_dependencies/graph.py

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

615
from taskiq_dependencies.ctx import AsyncResolveContext, SyncResolveContext
716
from taskiq_dependencies.dependency import Dependency
@@ -171,12 +180,22 @@ def _build_graph(self) -> None: # noqa: C901, WPS210
171180
# default vaule.
172181
for param_name, param in sign.parameters.items():
173182
default_value = param.default
174-
if hasattr(param.annotation, "__metadata__"): # noqa: WPS421
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
175194
# We go backwards,
176195
# because you may want to override your annotation
177196
# and the overriden value will appear to be after
178197
# the original `Depends` annotation.
179-
for meta in reversed(param.annotation.__metadata__):
198+
for meta in reversed(annotation.__metadata__):
180199
if isinstance(meta, Dependency):
181200
default_value = meta
182201
break

0 commit comments

Comments
 (0)