Skip to content

Feature: Add resource type parameter to init and shutdown resources using specialized providers #858

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

amoncusir
Copy link

@amoncusir amoncusir commented Feb 21, 2025

Objective

Enable the initialization and shutdown of resources by specifying their type. This allows you to create logical groups of resources and handle them by type, similar to applying resource scoping.

Changes

  • New Argument: Added an optional argument to both Container.init_resources and Container.shutdown_resources to specify which resource type(s) should be processed.
  • Tests: Added tests to ensure that resource scoping works correctly, including scenarios where dependent resources belong to different types.
  • Docs: Added documentation on resource to explain how can be used

…sources` methods to provide a more granular way to initialize the desired resources and add the possibility to scope that ones.
…nd shutdown_resources using the resource_type argument and how can scope the resources
@amoncusir amoncusir marked this pull request as ready for review February 22, 2025 18:51
Copy link

@colo-o colo-o left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty useful!

@wadinj
Copy link

wadinj commented Apr 25, 2025

@ZipFile any changes to make this merge?

Copy link
Contributor

@ZipFile ZipFile left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey! Sorry for delay. I have mixed feeling about this change, since this is not a proper scoping implementation, but rather a workaround. Either way, I think we can accept it for now, since introducing real scopes would probably require some serious re-engeneering on our part.

I've left some comments to the code + we need to add support for passing provider class in the Lifespan from dependency_injector.ext.starlette.

Comment on lines +60 to +61
def init_resources(self, resource_type: Type[Resource]=None) -> Optional[Awaitable]: ...
def shutdown_resources(self, resource_type: Type[Resource]=None) -> Optional[Awaitable]: ...
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
def init_resources(self, resource_type: Type[Resource]=None) -> Optional[Awaitable]: ...
def shutdown_resources(self, resource_type: Type[Resource]=None) -> Optional[Awaitable]: ...
def init_resources(self, resource_type: Type[Resource[Any]] = Resource) -> Optional[Awaitable[None]]: ...
def shutdown_resources(self, resource_type: Type[Resource[Any]] = Resource) -> Optional[Awaitable[None]]: ...

@@ -333,11 +333,11 @@ class DynamicContainer(Container):
self.wired_to_modules.clear()
self.wired_to_packages.clear()

def init_resources(self):
def init_resources(self, resource_type=providers.Resource):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need issubclass check for resource_type arg.

@@ -346,7 +346,7 @@ class DynamicContainer(Container):
if futures:
return asyncio.gather(*futures)

def shutdown_resources(self):
def shutdown_resources(self, resource_type=providers.Resource):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need issubclass check for resource_type arg.

@amoncusir
Copy link
Author

Hey! Sorry for delay. I have mixed feeling about this change, since this is not a proper scoping implementation, but rather a workaround. Either way, I think we can accept it for now, since introducing real scopes would probably require some serious re-engeneering on our part.

I've left some comments to the code + we need to add support for passing provider class in the Lifespan from dependency_injector.ext.starlette.

Yep! Exactly, it's a workaround. When I started with this, my first approach was to refact the current Resource class, but as you said, requieres a serious re-engeneering and this small change just do the work and provide a way to init indpendenent resources.

@ZipFile ZipFile changed the base branch from master to develop June 3, 2025 12:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants