Unable to access parameters from configuration.yaml using dependency-injector

285 Views Asked by At

I am unable to load the parameters I have in a configuration.yaml.

containers.py

class Container(containers.DeclarativeContainer):
    config = providers.Configuration(yaml_files=None)

    match_worker = providers.Singleton(Admin,
                                       key=config.key)


def initialize(config_path: Optional[str] = None) -> Container:
    container = Container()
    if config_path:
        container.config.from_yaml(config_path, required=True)
  
    return container

notebook.py

from myNotebook.containers import initialize

container = initialize('../../configuration.yaml')
container.config()

admin.py

from dependency_injector.wiring import inject, Provide

class Admin
    @inject
    def __init__(
            self,
            key: str,
            config: dict = Provide["config"]
    ):
        self._key: str = key
        self._config = config

    def _retrieve(self) -> str:
        a = self._config()
        print(a)
        print(a.stage())

And the first print statement gives:

<dependency_injector.wiring.Provide object at 0x7f62d62574f0>

However, for the second print statement I get an AttributeErrror:

'Provide' object has no attribute 'stage'

I've tried several permutations to access the parameters however to no avail. I'm very new to dependency-injector, I suspect it is something else in the code that is incorrect. How can I resolve this issue?

0

There are 0 best solutions below