-
Notifications
You must be signed in to change notification settings - Fork 47
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
added resolve_all to container #114
base: develop
Are you sure you want to change the base?
Changes from all commits
c412e44
3f60ccf
656e198
86cfa07
4cc10ba
c5da559
1d296a8
02c77ea
8b72ef7
f28e6e5
1ff8e4a
4755a51
139b2c6
430c5ea
2bda6b4
6fe7807
114dc47
5da13ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,7 @@ where = ["src"] | |
|
||
[project] | ||
name = "dishka" | ||
version = "0.1" | ||
version = "0.8.0" | ||
readme = "README.md" | ||
authors = [ | ||
{ name = "Andrey Tikhonov", email = "[email protected]" }, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
from collections.abc import Callable | ||
from contextlib import suppress | ||
from threading import Lock | ||
from typing import Any, Optional, TypeVar | ||
from typing import Any, Iterable, Literal, Optional, TypeVar, overload | ||
|
||
from dishka.entities.component import DEFAULT_COMPONENT, Component | ||
from dishka.entities.key import DependencyKey | ||
|
@@ -9,6 +10,7 @@ | |
from .dependency_source import FactoryType | ||
from .exceptions import ( | ||
ExitError, | ||
NoContextValueError, | ||
NoFactoryError, | ||
) | ||
from .provider import BaseProvider | ||
|
@@ -107,6 +109,45 @@ def get( | |
with lock: | ||
return self._get_unlocked(key) | ||
|
||
@overload | ||
def resolve_all(self, components: None = None) -> None: ... | ||
@overload | ||
def resolve_all(self, components: Literal[True]) -> None: ... | ||
@overload | ||
def resolve_all(self, components: Iterable[Component]) -> None: ... | ||
|
||
def resolve_all(self, components: Any = None) -> None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need specify components? What's the use case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we need to call parent container method as well? |
||
""" | ||
Resolve all container dependencies in the current scope for the given | ||
components. | ||
|
||
Examples: | ||
>>> container.resolve_all() | ||
Resolve all dependencies for the default component. | ||
|
||
>>> container.resolve_all(True) | ||
Resolve all dependencies for all components. | ||
|
||
>>> container.resolve_all(['component1', 'component2']) | ||
Resolve dependencies for 'component1' and 'component2'. | ||
""" | ||
if not components: | ||
|
||
def component_check(k: DependencyKey) -> bool: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get rid of nested functions |
||
return k.component == DEFAULT_COMPONENT | ||
elif components is True: | ||
|
||
def component_check(k: DependencyKey) -> bool: | ||
return True | ||
else: | ||
|
||
def component_check(k: DependencyKey) -> bool: | ||
return k.component in components | ||
|
||
for key in filter(component_check, self.registry.factories): | ||
with suppress(NoContextValueError): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? |
||
self._get_unlocked(key) | ||
|
||
def _get_unlocked(self, key: DependencyKey) -> Any: | ||
if key in self.context: | ||
return self.context[key] | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the purpose of that?