Skip to content

Commit

Permalink
2.4.0 (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancetnik authored Feb 20, 2024
1 parent 7884ccb commit 10c7545
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 38 deletions.
1 change: 0 additions & 1 deletion docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ hide:
<img alt="GitHub" src="https://img.shields.io/github/license/Lancetnik/FastDepends?color=%23007ec6">
</a>


FastDepends - FastAPI Dependency Injection system extracted from FastAPI and cleared of all HTTP logic.
This is a small library which provides you with the ability to use lovely FastAPI interfaces in your own
projects or tools.
Expand Down
9 changes: 9 additions & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ theme:
- content.tabs.link
- content.code.copy
- content.code.annotate
- navigation.top
- navigation.footer
i18n:
prev: 'Previous'
next: 'Next'
Expand All @@ -51,6 +53,13 @@ plugins:
- search
- markdownextradata:
data: data
- minify:
minify_html: true
minify_js: true
minify_css: true
htmlmin_opts:
remove_comments: true
cache_safe: true

markdown_extensions: # do not reorder
- toc:
Expand Down
16 changes: 8 additions & 8 deletions fast_depends/core/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,6 @@ def __init__(
self.positional_args = tuple(positional_args or ())
self.response_model = response_model

self.dependencies = dependencies or {}
self.extra_dependencies = extra_dependencies or ()
self.custom_fields = custom_fields or {}

for name in chain(self.dependencies.keys(), self.custom_fields.keys()):
params.pop(name, None)
self.params = params

self.use_cache = use_cache
self.cast = cast
self.is_async = (
Expand All @@ -182,6 +174,10 @@ def __init__(
is_generator or is_gen_callable(call) or is_async_gen_callable(call)
)

self.dependencies = dependencies or {}
self.extra_dependencies = extra_dependencies or ()
self.custom_fields = custom_fields or {}

sorted_dep: List["CallModel[..., Any]"] = []
flat = self.flat_dependencies
for calls in flat.values():
Expand All @@ -191,6 +187,10 @@ def __init__(
(i, len(i.sorted_dependencies)) for i in sorted_dep if i.use_cache
)

for name in chain(self.dependencies.keys(), self.custom_fields.keys()):
params.pop(name, None)
self.params = params

def _solve(
self,
/,
Expand Down
66 changes: 38 additions & 28 deletions fast_depends/use.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Optional,
Sequence,
Union,
cast,
overload,
)

Expand Down Expand Up @@ -77,7 +78,10 @@ def inject(
pydantic_config: Optional[ConfigDict] = None,
dependency_overrides_provider: Optional[Any] = dependency_provider,
wrap_model: Callable[[CallModel[P, T]], CallModel[P, T]] = lambda x: x,
) -> Union[Callable[P, T], _InjectWrapper[P, T],]:
) -> Union[
Callable[P, T],
_InjectWrapper[P, T],
]:
decorator = _wrap_inject(
dependency_overrides_provider=dependency_overrides_provider,
wrap_model=wrap_model,
Expand Down Expand Up @@ -178,7 +182,7 @@ def injected_wrapper(*args: P.args, **kwargs: P.kwargs) -> T:


class solve_async_gen:
iter: Optional[AsyncIterator[Any]]
_iter: Optional[AsyncIterator[Any]]

def __init__(
self,
Expand All @@ -193,27 +197,30 @@ def __init__(
self.overrides = overrides

def __aiter__(self) -> "solve_async_gen":
self.iter = None
self._iter = None
self.stack = AsyncExitStack()
return self

async def __anext__(self) -> Any:
if self.iter is None:
if self._iter is None:
stack = self.stack = AsyncExitStack()
await self.stack.__aenter__()
self.iter: AsyncIterator[Any] = (
await self.call.asolve(
*self.args,
stack=stack,
dependency_overrides=self.overrides,
cache_dependencies={},
nested=False,
**self.kwargs,
)
).__aiter__()
self._iter = cast(
AsyncIterator[Any],
(
await self.call.asolve(
*self.args,
stack=stack,
dependency_overrides=self.overrides,
cache_dependencies={},
nested=False,
**self.kwargs,
)
).__aiter__(),
)

try:
r = await self.iter.__anext__()
r = await self._iter.__anext__()
except StopAsyncIteration as e:
await self.stack.__aexit__(None, None, None)
raise e
Expand All @@ -222,7 +229,7 @@ async def __anext__(self) -> Any:


class solve_gen:
iter: Optional[Iterator[Any]]
_iter: Optional[Iterator[Any]]

def __init__(
self,
Expand All @@ -237,27 +244,30 @@ def __init__(
self.overrides = overrides

def __iter__(self) -> "solve_gen":
self.iter = None
self._iter = None
self.stack = ExitStack()
return self

def __next__(self) -> Any:
if self.iter is None:
if self._iter is None:
stack = self.stack = ExitStack()
self.stack.__enter__()
self.iter: AsyncIterator[Any] = iter(
self.call.solve(
*self.args,
stack=stack,
dependency_overrides=self.overrides,
cache_dependencies={},
nested=False,
**self.kwargs,
)
self._iter = cast(
Iterator[Any],
iter(
self.call.solve(
*self.args,
stack=stack,
dependency_overrides=self.overrides,
cache_dependencies={},
nested=False,
**self.kwargs,
)
),
)

try:
r = next(self.iter)
r = next(self._iter)
except StopIteration as e:
self.stack.__exit__(None, None, None)
raise e
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies = [
[project.optional-dependencies]
test = [
"coverage[toml]>=7.2.0,<8.0.0",
"pytest>=7.4.0,<8",
"pytest>=8.0.0,<9",
"dirty-equals>=0.7.0,<0.8",
]

Expand Down

0 comments on commit 10c7545

Please sign in to comment.