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)
def configure_stacked_binding_and_lifetime(config: Config): config.bind(SB1, SB2) config.lifetime(SB2, Lifetime.SINGLETON) config.bind(SB2, SB3)
def configure(config: Config): config.bind(T, foo)
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)
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)
def configure_binding_with_context(config: Config): config.bind(F, F1, where=UseF1) config.bind(F, F2, where=UseF2)
def configure_bind_singleton(config: Config): config.bind(MyInterface, MyImplementation) config.lifetime(MyImplementation, Lifetime.SINGLETON)
def configure_callable(config: Config): config.bind(Transient, get_my_Transient) config.arguments(get_my_Transient, a=42)
def configure_bind_hierarchy(ctx: Config): ctx.bind(H1, H2) ctx.bind(H2, H3)
def configure_bind(ctx: Config): ctx.bind(MyInterface, MyImplementation)