Inject dependency into a class attr
Hello,
Sorry if this question already has been answered somewhere but I went through the doc extensively and couldn't find anything.
I would like to inject a dependency for a class attribute, but couldn't find any way of doing it.
Something similar from python-inject:
class User(object):
cache = inject.attr(Cache)
Thanks for your help
Hello @brunopereira27 ,
You can do it with Factory provider:
cache_provider = providers.Singleton(Cache)
user_factory = providers.Factory(User)
user.add_attributes(cache=cache_provider)
user = user_factory()
assert isinstance(user.cache, Cache)
You didn't find it because I didn't document that. I'm sorry about that. I already have a backlog item to update documentation.
The same works with Singleton provider btw.
Thanks for your quick answer!
As a consequence, I cannot provide a class attribute is that class is not created through a factory or as a singleton? Isn't there a way to provide a factory to the init function of a class?
Few things:
- You asked:
Isn't there a way to provide a factory to the init function of a class?
Yes. You can use .provider attribute when specify an injection:
cache = providers.Singleton(Cache)
user_factory = providers.Factory(
User,
cache=cache.provider, # This will inject cache provider instead of Cache instance
)
user = user_factory()
asseert user.cache_provider is cache_provider
assert isinstance(user.cache(), Cache)
- The closest feature to the initial your initial example is wiring. With wiring you can make an injection into functions and methods. Here is how initial example will look like with wiring:
class User(object):
def __init__(self, cache=Provider[Container.cache]):
...
You also need to have a container and call container.wire(). You can read more about this here: https://python-dependency-injector.ets-labs.org/wiring.html
Current wiring can't make attribute injections, but I could upgrade it. Here is a prototype how it would look like:
class User(object):
cache = Provider[Container.cache]
Also you could use string identifiers to do not pull a container:
class User(object):
cache = Provider['cache']
I started working on it. Will probably take a few more days.
How does it look to you?
I started working on it. Will probably take a few more days.
Done in version 4.28.0. Docs: https://python-dependency-injector.ets-labs.org/wiring.html#making-injections-into-modules-and-class-attributes
Closing this issue. Feel free to comment if anything.