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

Another puzzling issue #200

Closed
j4zzcat opened this Issue Jul 25, 2018 · 3 comments

Comments

Projects
None yet
2 participants
@j4zzcat
Copy link

j4zzcat commented Jul 25, 2018

Hi, I wonder why the following snippet breaks?

import logging
import dependency_injector.containers  as dic
import dependency_injector.providers   as dip
from collections import namedtuple

GuBar = namedtuple( "GuBar", [ "product_id" ] )
Milk  = namedtuple( "Milk",  [ "product_id" ] )

class CommonServices( dic.DeclarativeContainer ):
    logger  = dip.Singleton( logging.Logger, name = None )
    
    staples = dip.Callable(
        lambda id: (
            dip.Singleton( logging.Logger, name = id )(),
            dip.Singleton( GuBar, product_id = id )(),
            dip.Singleton( Milk, product_id = id )()
            # more products that need id for initialization
        )
    )

ShoppingCart = namedtuple( "ShoppingCart", [ "staples" ] )
class AgentServices( dic.DeclarativeContainer ):
    common_services = dip.DependenciesContainer()

    shopping_cart = dip.Factory( ShoppingCart, staples = dip.Factory( common_services.staples, id = "1234" ) )

# Test
cs = CommonServices()
x  = dip.Factory( cs.staples, "x" )
cs.staples( id = "y" )

ags = AgentServices( common_services = cs )

ags.shopping_cart() # <-- Fails here

@rmk135 rmk135 self-assigned this Jul 26, 2018

@rmk135 rmk135 added the question label Jul 26, 2018

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

@rmk135

This comment has been minimized.

Copy link
Member

rmk135 commented Jul 26, 2018

Hi @j4zzcat ,

Mostly the same reason as it was at #198 , but now in different place. I call what you do here "chained providers" (providers.Factory(providers.Callable(...))), but, frankly speaking, I haven't paid to much attention to it yet, so some things could be buggy. Sorry about that.

Fixed in 3.12.4.

Thanks,
Roman

@rmk135 rmk135 added the bug label Jul 26, 2018

@rmk135 rmk135 closed this Jul 26, 2018

@j4zzcat

This comment has been minimized.

Copy link
Author

j4zzcat commented Jul 26, 2018

Thank you @rmk135 for the super-quick response!

@rmk135

This comment has been minimized.

Copy link
Member

rmk135 commented Jul 26, 2018

You're welcome :) Thanks for posting the issue - it really helps to make Dependency Injector better!

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.