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)
Example #2
0
def create_container(
    configure: Optional[Callable[[Config], None]] = None,
    default_lifetime=Lifetime.TRANSIENT,
    dependencies: Optional[List[object]] = None,
) -> StaticContainer:
    """
    Use this function to create a DI container.

    :param configure:
    :param default_lifetime:
    :param dependencies:
    :return:
    """
    if configure is None:
        configure = _default_config
    if dependencies is None:
        dependencies = []
    backend = _create_backend(default_lifetime)
    resolver = _create_resolver(backend)
    container = StaticContainer(resolver=resolver)
    configure(Config(backend=backend))
    _resolve_dependencies(backend, dependencies)
    return container
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_instance_with_context(config: Config):
    config.instance(T, t1_instance, where=UseT1)
    config.instance(T, t2_instance, where=UseT2)
def configure_bind(ctx: Config):
    ctx.bind(MyInterface, MyImplementation)
def configure_factory_class_instance_method(config: Config):
    config.arg_factory(NeedsInt, a_int=ProvidesInt(MyInt()).get_int)
def configure_parameter_and_factory(config: Config):
    config.arguments(ParameterAndFactory, number=42, text="42")
    config.arg_factory(ParameterAndFactory, text=get_text)
def configure_arguments(config: Config):
    config.arguments(MyArg, arg1="foobar")
 def configure(config: Config):
     config.bind(T, foo)
def configure_callable(config: Config):
    config.bind(Transient, get_my_Transient)
    config.arguments(get_my_Transient, a=42)
def configure_instance(config: Config):
    config.instance(MyInstance, MY_INSTANCE_INSTANCE)
def configure_singleton(config: Config):
    config.arguments(Transient, a=42, b=10.0)
    config.lifetime(Transient, Lifetime.SINGLETON)
def configure_transient(config: Config):
    config.arguments(Transient, a=42, b=10.0)
def configure_lifetime(config: Config):
    config.lifetime(MySingleton, Lifetime.SINGLETON)
    config.lifetime(MyTransient, Lifetime.TRANSIENT)
def configure_bind_hierarchy(ctx: Config):
    ctx.bind(H1, H2)
    ctx.bind(H2, H3)
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(config: Config):
     config.dependency(int)
def configure_bind_singleton(config: Config):
    config.bind(MyInterface, MyImplementation)
    config.lifetime(MyImplementation, Lifetime.SINGLETON)
def configure_stacked_binding_and_lifetime(config: Config):
    config.bind(SB1, SB2)
    config.lifetime(SB2, Lifetime.SINGLETON)
    config.bind(SB2, SB3)
def configure_dependency(config: Config):
    config.dependency(Dependecy)
def configure_arg_factory(config: Config):
    config.arg_factory(ArgFactory, name=factory)
    config.arguments(NameProvider, name="foobar")
def configure_binding_with_context(config: Config):
    config.bind(F, F1, where=UseF1)
    config.bind(F, F2, where=UseF2)
def configure_factory_class_static(config: Config):
    config.arg_factory(NeedsInt, a_int=ProvidesInt.get_int_static)
def configure_configure_transient_with_context(config: Config):
    config.arguments(T, a=1, where=UseT1)
    config.arguments(T, a=2, where=UseT2)
def configure_factory_not_created_class_instance_method(config: Config):
    config.arg_factory(NeedsInt, a_int=ProvidesInt.get_int)
def configure_singleton_with_context(config: Config):
    config.arguments(T, a=1, where=UseT1)
    config.arguments(T, a=2, where=UseT2)
    config.lifetime(T, Lifetime.SINGLETON, where=UseT1)
    config.lifetime(T, Lifetime.SINGLETON, where=UseT2)