question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using of configuration provider with some singleton providers

See original GitHub issue

Hi, I’ve a problem with wiring function and I don’t understand where I’m wrong. My classes are that:

ConfigurationContainer.py

from dependency_injector import containers, providers

class ConfigContainer(containers.DeclarativeContainer):
    config = providers.Configuration()

DependencyContainer.py

from dependency_injector import containers, providers

from src.DialogflowInteraction import DialogflowInteraction
from src.QA.ContextRetriever.VeryBasicContextRetriever import \
    VeryBasicContextRetriever
from src.QA.QA import QA


class BotDependencyFactory(containers.DeclarativeContainer):
    verybasiccontextretriever = providers.Singleton(VeryBasicContextRetriever)
    q_a = providers.Singleton(QA)
    dialogflowinteraction = providers.Singleton(DialogflowInteraction)

Bot.py

import logging
from uuid import uuid1
import sys
from dependency_injector.wiring import Provide

from src.ConfigurationContainer import ConfigContainer
from src.DependencyContainer import BotDependencyFactory



class Bot():
    
    def __init__(
        self, 
        threshold=Provide[ConfigContainer.config.general.threshold.as_float()],
        q_a = Provide[BotDependencyFactory.q_a],
        context_retriever = Provide[BotDependencyFactory.verybasiccontextretriever],
        dialogflowinteraction = Provide[BotDependencyFactory.dialogflowinteraction]
        ):
        self.threshold = threshold
        self.QA = q_a
        self.context_retriever = context_retriever
        self.df_bot = dialogflowinteraction

app.py

import logging
import os
import sys

from src.ConfigurationContainer import ConfigContainer
from src.DependencyContainer import BotDependencyFactory
from src.bot import Bot


def configure_env():
    logging.basicConfig(level=logging.INFO)
    config = ConfigContainer()
    config.config.from_ini('config.ini')
    logging.info('configuration completed')
    return config

if __name__ == '__main__':
    config = configure_env()
    config.wire(modules=[sys.modules[__name__]])
    logging.info('Configuration container wired successfully')
    bot_dependency_factory = BotDependencyFactory()
    bot_dependency_factory.wire(modules=[sys.modules[__name__]])
    bot = Bot()    

When I try to run main method returns this exception

INFO:root:configuration completed
Traceback (most recent call last):
  File "C:\Users\Federico\anaconda3\envs\deepcleverbot\lib\site-packages\dependency_injector\wiring.py", line 111, in _resolve_provider
    return self._map[original]
KeyError: <dependency_injector.providers.Singleton(<class 'src.QA.QA.QA'>) at 0x256758fd630>

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "c:/Users/Federico/Desktop/DeepCleverBot/bot/app.py", line 24, in <module>
    config.wire(modules=[sys.modules[__name__]])
  File "src/dependency_injector/containers.pyx", line 195, in dependency_injector.containers.DynamicContainer.wire
  File "C:\Users\Federico\anaconda3\envs\deepcleverbot\lib\site-packages\dependency_injector\wiring.py", line 162, in wire
    _patch_fn(member, method_name, method, providers_map)
  File "C:\Users\Federico\anaconda3\envs\deepcleverbot\lib\site-packages\dependency_injector\wiring.py", line 193, in _patch_fn
    injections = _resolve_injections(fn, providers_map)
  File "C:\Users\Federico\anaconda3\envs\deepcleverbot\lib\site-packages\dependency_injector\wiring.py", line 218, in _resolve_injections
    provider = providers_map.resolve_provider(marker.provider)
  File "C:\Users\Federico\anaconda3\envs\deepcleverbot\lib\site-packages\dependency_injector\wiring.py", line 55, in resolve_provider
    return self._resolve_provider(provider)
  File "C:\Users\Federico\anaconda3\envs\deepcleverbot\lib\site-packages\dependency_injector\wiring.py", line 113, in _resolve_provider
    raise Exception('Unable to resolve original provider')
Exception: Unable to resolve original provider

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
rmk135commented, Nov 6, 2020

@iskorini , so what you should do is wiring of the QA package.


from src import QA
...
config.wire(modules=[sys.modules[__name__]], packages=[QA])

Or the same, but with a module:


from src.QAimport QA
...
config.wire(modules=[sys.modules[__name__], QA])

Injections will not work until you wire the container with needed modules and packages.

1reaction
rmk135commented, Nov 5, 2020

@iskorini

Fixed in 4.3.5. See #320 .

Thanks for reporting the issue. You’re welcome to upgrade to a patched version.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configuration provider - Dependency Injector
Configuration provides configuration options to the other providers. This page demonstrates how to use Configuration provider to inject the dependencies, ...
Read more >
Option patterns with custom configuration provider in .NET
I was wondering how to write a custom configuration provider with IOptionsMonitor that means, some value will be loaded after a DI configuration...
Read more >
Options pattern in ASP.NET Core - Microsoft Learn
Discover how to use the options pattern to represent groups of related settings in ASP.NET Core apps.
Read more >
Configuring dependency providers - Angular
The Creating and injecting services topic describes how to use classes as dependencies. Besides classes, you can also use other values such as...
Read more >
Configuration raises AttributeError when provider is called #358
Singleton (MyService, config=config.myservice) class Container(containers.DeclarativeContainer): config = providers.Configuration() services ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found