コード例 #1
0
def test_bind_a_non_subclass_raises_typeerror() -> None:
    lifetime = Lifetimes(Lifetime.TRANSIENT)
    instances = Instances()
    bindings = Bindings()
    factory_args = FactoryArgs()
    dependencies = Dependencies()
    backend = ConfigBackend(bindings, lifetime, instances, factory_args, dependencies)
    context = Config(backend)
    with pytest.raises(TypeError) as e:
        context.bind(MyBaseClass, NotASubclass)
    assert "{NotASubclass} must be a subclass of {MyBaseClass}".format(
        NotASubclass=NotASubclass, MyBaseClass=MyBaseClass
    ) in str(e)
コード例 #2
0
def configure_stacked_binding_and_lifetime(config: Config):
    config.bind(SB1, SB2)
    config.lifetime(SB2, Lifetime.SINGLETON)
    config.bind(SB2, SB3)
コード例 #3
0
 def configure(config: Config):
     config.bind(T, foo)
コード例 #4
0
def configure_lifetime_with_context(config: Config):
    config.bind(T, foo)
    config.arguments(foo, a=1, where=UseT1)
    config.arguments(foo, a=2, where=UseT2)
    config.lifetime(foo, Lifetime.SINGLETON, where=UseT2)
    config.lifetime(foo, where=UseT3, lifetime=Lifetime.SINGLETON)
コード例 #5
0
def configure_callable_with_context(config: Config):
    config.bind(T, foo)
    config.arguments(foo, a=1, where=UseT1)
    config.arguments(foo, a=2, where=UseT2)
コード例 #6
0
def configure_binding_with_context(config: Config):
    config.bind(F, F1, where=UseF1)
    config.bind(F, F2, where=UseF2)
コード例 #7
0
def configure_bind_singleton(config: Config):
    config.bind(MyInterface, MyImplementation)
    config.lifetime(MyImplementation, Lifetime.SINGLETON)
コード例 #8
0
def configure_callable(config: Config):
    config.bind(Transient, get_my_Transient)
    config.arguments(get_my_Transient, a=42)
コード例 #9
0
def configure_bind_hierarchy(ctx: Config):
    ctx.bind(H1, H2)
    ctx.bind(H2, H3)
コード例 #10
0
def configure_bind(ctx: Config):
    ctx.bind(MyInterface, MyImplementation)