-
-
Notifications
You must be signed in to change notification settings - Fork 326
Make Closing marker working with providers that contains a resource as argument #633
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
Comments
I'd like to see this as well. More in general it looks like resources used as a dependency to another provider are not encapsulated with the Closing. I've found that if you include the initial resource in your method you're decorating with @Inject then it does work, but that seems like an unreasonable public interface |
@kiriharu until it's merged, it can be monkey patched with the following code from typing import Any, Callable
from dependency_injector import providers, wiring
from dependency_injector.wiring import ProvidersMap, _patched_registry, Provide, Provider
# This backports/monkeypatches the dependency injector to allow `Closing` on dependent resources.
# It is extremely important that it is imported BEFORE dependency_injector is used. This can be
# removed once https://github.com/ets-labs/python-dependency-injector/pull/636 is merged and released.
def _locate_dependent_closing_args(provider: providers.Provider) -> dict[str, providers.Provider]:
if not hasattr(provider, "args"):
return {}
closing_deps = {}
for arg in provider.args:
if not isinstance(arg, providers.Provider) or not hasattr(arg, "args"):
continue
if not arg.args and isinstance(arg, providers.Resource):
return {str(id(arg)): arg}
else:
closing_deps += _locate_dependent_closing_args(arg)
return closing_deps
def _bind_injections(fn: Callable[..., Any], providers_map: ProvidersMap) -> None:
patched_callable = _patched_registry.get_callable(fn)
if patched_callable is None:
return
for injection, marker in patched_callable.reference_injections.items():
provider = providers_map.resolve_provider(marker.provider, marker.modifier)
if provider is None:
continue
if isinstance(marker, Provide):
patched_callable.add_injection(injection, provider)
elif isinstance(marker, Provider):
if isinstance(provider, providers.Delegate):
patched_callable.add_injection(injection, provider)
else:
patched_callable.add_injection(injection, provider.provider)
if injection in patched_callable.reference_closing:
patched_callable.add_closing(injection, provider)
deps = _locate_dependent_closing_args(provider)
for key, dep in deps.items():
patched_callable.add_closing(key, dep)
wiring._bind_injections = _bind_injections |
Amazing feature! Thanks to everybody involved and special thanks to @StummeJ for the PR! |
Published on PyPI in |
I would really like to use this new feature in my project, but I inject everything as kwargs and they seemd to be ignored, so I made a small change to the new feature to cover this case too, would you mind reviewing it? (Sorry for the double PR, I used a wrong GitHub account and had to remake everything from the beginning). There's just a little problem: I wasn't able to run tests on my changes (despite I wrote them) and I didn't find any documentation on how to run them, could you lend me a hand on this? Thank you |
@federinik I think I just used the pytest command. In any regards, I use VSCode with the python test runner in there too. It also looks like there's a |
Hi @Stoom, I can confirm I've been able to run tests via All tests passed btw |
Hi @rmk135, any news? I really, REALLY need this in my project! 😄 Thx |
Hey @federinik, Does the PR that you provided above contain complete implementation of what you need? |
Hi @rmk135, yes, I should be fine with the PR content, as you can see it was just a small detail, while the core of the feature was kinda complete. Thank you |
Hi @rmk135, If yes, please tell me what to update the PR. If no, please either merge it or tell me you won't and what's the reason. Thank you |
Hi!
Is it possible to make the Closing marker work with all types providers that contain a resource inside? For example, in the following code, the Closing marker does not work:
At this moment it will work if we add a separate resource argument to handler:
The text was updated successfully, but these errors were encountered: