python-dependency-injector icon indicating copy to clipboard operation
python-dependency-injector copied to clipboard

Alias providers using Delegate and DependenciesContainer

Open gael-ft opened this issue 1 month ago • 0 comments

My own container depends on an other container that provides a ThreadSafeSingleton. I would like my own container to provide this singleton as well, like a alias to the singleton. E.g.

from dependency_injector import containers, providers

# Containers I can't update
class NestedOtherContainer(containers.DeclarativeContainer):
    a_client = providers.ThreadSafeSingleton(...)

class OtherContainer(containers.DeclarativeContainer):
    clients = providers.DependenciesContainer()


# my container
class MyContainer(containers.DeclarativeContainer):
    other_container = providers.DependenciesContainer()
    a_alias = ???

I tried several things using Delegate like

a_alias = providers.Delegate(other_container.clients.a_client)

a_alias = providers.Delegate(other_container.clients.provided.a_client)

# etc.

But none of them works when MyContainer(other_container=...).a_alias(), getting things like AttributeGetter or MethodCaller.

Any idea on how to do this kind of aliasing ?

gael-ft avatar Oct 27 '25 17:10 gael-ft