Skip to content

Added FastAPI dependency support. #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions taskiq_dependencies/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from taskiq_dependencies.ctx import AsyncResolveContext, SyncResolveContext
from taskiq_dependencies.dependency import Dependency

try:
from fastapi.params import Depends as FastapiDepends # noqa: WPS433
except ImportError:
FastapiDepends = None # type: ignore


class DependencyGraph:
"""Class to build dependency graph from a function."""
Expand Down Expand Up @@ -103,6 +108,19 @@ def _build_graph(self) -> None: # noqa: C901, WPS210
# find all parameters, that have TaskiqDepends as it's
# default vaule.
for param_name, param in sign.parameters.items():
default_value = param.default

# This is for FastAPI integration. So you can
# use Depends from taskiq mixed with fastapi's dependencies.
if FastapiDepends is not None and isinstance( # noqa: WPS337
default_value,
FastapiDepends,
):
default_value = Dependency(
dependency=default_value.dependency,
use_cache=default_value.use_cache,
)

# We check, that default value is an instance of
# TaskiqDepends.
if not isinstance(param.default, Dependency):
Expand Down