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

How to restrict DependenciesContainer type?

Open chbndrhnns opened this issue 2 years ago • 4 comments

I am trying to re-create an onion-layer architecture with the dependency injector library. Here is a snippet:

from dependency_injector import containers, providers

class DbAdapter:

  def get_all(self):
    return []

class Repo:
  def __init__(self, adapter):
    self._adapter = adapter


class Adapters(containers.DeclarativeContainer):
    db_adapter = providers.Singleton(
        DbAdapter
    )


class Repositories(containers.DeclarativeContainer):
    adapters: Adapters = containers.DependenciesContainer()

    repo = providers.Factory(
        Repo, adapter=adapters.db_adapter
    )

Can I make dependency-injector check the provided container for adapters and reject invalid types?

chbndrhnns avatar Sep 20 '21 08:09 chbndrhnns

According to my experiences with the library, this seems not possible. Is it something you see as valuable to have, @rmk135?

chbndrhnns avatar Oct 21 '21 08:10 chbndrhnns

Hi @chbndrhnns ,

Thanks for sharing the use case. Yes, I think you can be something like:

class Repositories(containers.DeclarativeContainer):
    adapters: Adapters = containers.DependenciesContainer(instanceof= Adapters)

    repo = providers.Factory(
        Repo, adapter=adapters.db_adapter
    )

I'll add it to the backlog.

rmk135 avatar Nov 08 '21 01:11 rmk135

Your example is not working for me as containers (line 2) does not seem to have a DependenciesContainer. In version 4.37, only providers has DependenciesContainer.

Also, when I try to run Repositories() from the snippet below, it fails:

class Repositories(containers.DeclarativeContainer):
    adapters: Adapters = providers.DependenciesContainer(instance_of=Adapters)

    repo = providers.Factory(Repo, adapter=adapters.db_adapter)

Error:

src/dependency_injector/containers.pyx:742: in dependency_injector.containers.DeclarativeContainer.__new__
    ???
src/dependency_injector/containers.pyx:392: in dependency_injector.containers.DynamicContainer.load_config
    ???
src/dependency_injector/containers.pyx:193: in traverse
    ???
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

>   ???
E   AttributeError: type object 'Adapters' has no attribute 'related'

src/dependency_injector/providers.pyx:4813: AttributeError

chbndrhnns avatar Nov 08 '21 15:11 chbndrhnns

Yeah, same issue for me.

rowan-maclachlan avatar Mar 24 '22 19:03 rowan-maclachlan

@rmk135 i am coming back to this issue now and then. Do you have any idea what to do?

chbndrhnns avatar Oct 18 '22 19:10 chbndrhnns