Skip to content
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

Fix Closing dependency resolution #711

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

Conversation

jazzthief
Copy link

@jazzthief jazzthief commented May 22, 2023

Fix issues found when using Closing to implement per-function scope for Resources.

nested_service = providers.Factory(NestedService, factory_service)


class ContainerSingleton(containers.DeclarativeContainer):
Copy link
Author

@jazzthief jazzthief May 24, 2023

Choose a reason for hiding this comment

The 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 provider type is arbitrary - its type doesn't play any role in injections, just needed something for a Resource to depend on.

@@ -92,6 +92,7 @@ def test_function_dependency_kwargs(factory: FactoryService = Closing[Provide["f
return factory


@inject

Choose a reason for hiding this comment

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

Oops, that's on me :D Sorry, bad merge

Copy link
Author

Choose a reason for hiding this comment

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

@federinik No worries :)

@jazzthief
Copy link
Author

I have in mind a few potential test cases that could be added: when there are no resources in the dependency graph; and when there are several different resources.
Apart from that, the change is ready.

@jazzthief jazzthief marked this pull request as ready for review May 24, 2023 13:51
if not isinstance(arg, providers.Provider) or not hasattr(arg, "args"):
continue

if not arg.args and isinstance(arg, providers.Resource):
if isinstance(arg, providers.Resource):
return {str(id(arg)): arg}
Copy link

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?

Suggested change
return {str(id(arg)): arg}
closing_deps |= {str(id(arg)): arg}

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.

None yet

3 participants