-
-
Notifications
You must be signed in to change notification settings - Fork 322
Fix Closing
dependency resolution
#711
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
Closed
jazzthief
wants to merge
11
commits into
ets-labs:develop
from
GodelTech:702-nested-resource-resolution
Closed
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5118340
Allow Closing to detect dependent resources passed as kwargs too #636
federinik f3d7e68
Use of update operator (|=) as per https://peps.python.org/pep-0584/
federinik 5cdf114
Add tests for nested deps resolution
jazzthief d00afd9
Fix resolution
jazzthief 43a14df
Merge remote-tracking branch 'origin-godeltech/702-nested-resource-re…
federinik 3cdba79
Merge pull request #1 from federinik/master
jazzthief 027ccfa
Restructure conditionals, incorporate kwargs check
jazzthief 2dabfaa
Add test container with element after resource
jazzthief 2ca44a0
Use parametrized fixture for containers
jazzthief 6494256
Restore removed `inject` marker
jazzthief c013154
Use `Singleton` provider; clean up formatting
jazzthief File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,17 +2,24 @@ | |
from dependency_injector.wiring import inject, Provide, Closing | ||
|
||
|
||
class Singleton: | ||
pass | ||
|
||
|
||
class Service: | ||
init_counter: int = 0 | ||
shutdown_counter: int = 0 | ||
dependency: Singleton = None | ||
|
||
@classmethod | ||
def reset_counter(cls): | ||
cls.init_counter = 0 | ||
cls.shutdown_counter = 0 | ||
|
||
@classmethod | ||
def init(cls): | ||
def init(cls, dependency: Singleton = None): | ||
if dependency: | ||
cls.dependency = dependency | ||
cls.init_counter += 1 | ||
|
||
@classmethod | ||
|
@@ -25,17 +32,49 @@ def __init__(self, service: Service): | |
self.service = service | ||
|
||
|
||
class NestedService: | ||
def __init__(self, factory_service: FactoryService): | ||
self.factory_service = factory_service | ||
|
||
|
||
def init_service(): | ||
service = Service() | ||
service.init() | ||
yield service | ||
service.shutdown() | ||
|
||
|
||
def init_service_with_singleton(singleton: Singleton): | ||
service = Service() | ||
service.init(singleton) | ||
yield service | ||
service.shutdown() | ||
|
||
|
||
class Container(containers.DeclarativeContainer): | ||
|
||
service = providers.Resource(init_service) | ||
factory_service = providers.Factory(FactoryService, service) | ||
factory_service_kwargs = providers.Factory( | ||
FactoryService, | ||
service=service | ||
) | ||
nested_service = providers.Factory(NestedService, factory_service) | ||
|
||
|
||
class ContainerSingleton(containers.DeclarativeContainer): | ||
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. Added another container to cover a case where a resource is not at the end of the dependency graph. A |
||
|
||
singleton = providers.Singleton(Singleton) | ||
service = providers.Resource( | ||
init_service_with_singleton, | ||
singleton | ||
) | ||
factory_service = providers.Factory(FactoryService, service) | ||
factory_service_kwargs = providers.Factory( | ||
FactoryService, | ||
service=service | ||
) | ||
nested_service = providers.Factory(NestedService, factory_service) | ||
|
||
|
||
@inject | ||
|
@@ -44,5 +83,21 @@ def test_function(service: Service = Closing[Provide["service"]]): | |
|
||
|
||
@inject | ||
def test_function_dependency(factory: FactoryService = Closing[Provide["factory_service"]]): | ||
def test_function_dependency( | ||
factory: FactoryService = Closing[Provide["factory_service"]] | ||
): | ||
return factory | ||
|
||
|
||
@inject | ||
def test_function_dependency_kwargs( | ||
factory: FactoryService = Closing[Provide["factory_service_kwargs"]] | ||
): | ||
return factory | ||
|
||
|
||
@inject | ||
ZipFile marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_function_nested_dependency( | ||
nested: NestedService = Closing[Provide["nested_service"]] | ||
): | ||
return nested |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Hello @jazzthief. Fix not working for several Resources. Maybe something like this?