Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Dependency is not defined #198

Closed
j4zzcat opened this Issue Jul 24, 2018 · 4 comments

Comments

Projects
None yet
2 participants
@j4zzcat
Copy link

j4zzcat commented Jul 24, 2018

I wonder why the following code fails?

from   collections import namedtuple
import dependency_injector.containers  as containers
import dependency_injector.providers   as providers

app_Config                = namedtuple( "Config",          [ "dir" ] )
app_ExternalConfigAdapter = namedtuple( "ConfigAdapter",   [ "config", "starting_point" ] )
external_RestApiClient    = namedtuple( "RestApiClient",   [ "config", "version" ] )

class CommonServices( containers.DeclarativeContainer ):
    config = providers.Singleton( app_Config, dir = "/root/.qrsa" )

class ExternalServices( containers.DeclarativeContainer ):
    common_services = providers.DependenciesContainer()

    adapted_config = providers.Factory( app_ExternalConfigAdapter,
        common_services.config,
        "common/nitro"
    )

    external_rest_client = providers.Dependency( instance_of = external_RestApiClient )
    external_rest_client.provided_by(
        providers.Singleton( external_RestApiClient,
            config  = adapted_config,
            version = "9.0"
        )
    )

cs  = CommonServices()
qrs = ExternalServices( common_services = cs )
qrs.adapted_config()

# --- fails here ---
qrs.external_rest_client()

@rmk135 rmk135 self-assigned this Jul 24, 2018

@rmk135 rmk135 added the question label Jul 24, 2018

@rmk135

This comment has been minimized.

Copy link
Member

rmk135 commented Jul 24, 2018

Hi @j4zzcat ,

I will do some debugging later today.

Roman

@rmk135

This comment has been minimized.

Copy link
Member

rmk135 commented Jul 24, 2018

Well, I think that a way to get it working is next:

from   collections import namedtuple
import dependency_injector.containers  as containers
import dependency_injector.providers   as providers

app_Config                = namedtuple( "Config",          [ "dir" ] )
app_ExternalConfigAdapter = namedtuple( "ConfigAdapter",   [ "config", "starting_point" ] )
external_RestApiClient    = namedtuple( "RestApiClient",   [ "config", "version" ] )

class CommonServices( containers.DeclarativeContainer ):
    config = providers.Singleton( app_Config, dir = "/root/.qrsa" )

class ExternalServices( containers.DeclarativeContainer ):
    version = providers.Dependency()
    common_services = providers.DependenciesContainer()

    adapted_config = providers.Factory( app_ExternalConfigAdapter,
        common_services.config,
        "common/nitro"
    )

    external_rest_client = providers.Singleton( external_RestApiClient,
        config  = adapted_config,
        version = version
    )

cs  = CommonServices()
qrs = ExternalServices( common_services = cs , version = "9.0" )
qrs.adapted_config()

print(qrs.external_rest_client())
# RestApiClient(config=ConfigAdapter(config=Config(dir='/root/.qrsa'), starting_point='common/nitro'), version='9.0')

I will also check the behavior of Dependency provider in such case because, generally, your code looks "legal", but I need to debug more to check why it fails - there could be few reasons.

@rmk135

This comment has been minimized.

Copy link
Member

rmk135 commented Jul 24, 2018

@j4zzcat , I've found an issue, thank you very-very much. It was a really "nasty" bug :)

I will publish a fix in few hours or so.

@rmk135 rmk135 added the bug label Jul 24, 2018

rmk135 added a commit that referenced this issue Jul 24, 2018

@rmk135

This comment has been minimized.

Copy link
Member

rmk135 commented Jul 24, 2018

Hi @j4zzcat,

Issue has been fixed and new release 3.12.3 has been published to PyPi.

U're welcome to upgrade.

Thanks again for reporting the issue!

Regards,
Roman

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.