Пример #1
0
def test_injector_kwonly_def_error():
    injector = Injector()

    def get_kwargs1():
        pass

    def get_kwargs2(*, xyz):
        pass

    def get_kwargs3(*, type):
        pass

    with pytest.raises(ProvideError) as exc:
        injector.provide(KwOnly(get_kwargs1))
    exc.match("Callable must have kwonly arguments")

    with pytest.raises(ProvideError) as exc:
        injector.provide(KwOnly(get_kwargs2))
    exc.match(
        "Keyword argument resolver function muts have 'name' keyword only argument"
    )

    with pytest.raises(ProvideError) as exc:
        injector.provide(KwOnly(get_kwargs3))
    exc.match(
        "Keyword argument resolver function muts have 'name' keyword only argument"
    )

    class X:
        pass

    with pytest.raises(ProvideError) as exc:
        injector.provide(KwOnly(X))
    exc.match("Argument must be callable.")
Пример #2
0
def test_injector_clone():
    class A:
        pass

    def fn(i: Injector, kwarg: A):
        assert isinstance(kwarg, A)
        assert kwarg is ainst
        assert i[A] is kwarg
        assert i is not injector
        i[A] = "another"
        return injector.exec(fn2)

    def fn2(i: Injector):
        assert i[A] is ainst
        return "OK"

    injector = Injector()
    injector.provide(A)

    ainst = injector[A] = injector[A]

    def get_kw(a: A, *, name: str, type: type):
        if name == "kwarg":
            return a
        else:
            raise NoKwOnly()

    injectable = injector.provide(fn, provide=[KwOnly(get_kw)])
    assert injectable(injector) == "OK"
    assert injector[fn] == "OK"
Пример #3
0
def test_injector_own_kwonly():
    injector = Injector()

    class Config(dict):
        def __init__(self):
            super().__init__(some_key="OK")

    def get_kwarg(config: Config, *, name, type):
        assert name == "some_key"
        assert type is str
        return config[name]

    def fn(*, some_key: str):
        assert some_key == "OK"
        return "NICE"

    injector.provide(Config)
    injector.provide(fn, provide=[KwOnly(get_kwarg)])

    assert injector.get(fn) == "NICE"

    with pytest.raises(InjectError) as exc:
        assert injector.exec(fn) == "NICE"
    exc.match(
        "Not found suitable value for: <ValueResolver name=some_key id=<class 'str'>>"
    )
Пример #4
0
def test_injector_kwonly_error():
    injector = Injector()

    class Config(dict):
        def __init__(self):
            super().__init__(some_key="OK")

    def get_kwarg(config: Config, *, name, type, xx):
        pass

    def fn(*, some_key: str):
        pass

    injector.provide(Config)
    injector.provide(KwOnly(get_kwarg))

    with pytest.raises(InjectError) as exc:
        injector.exec(fn)
    exc.match("^Not found suitable value for")
Пример #5
0
def test_injector_kwonly2():
    injector = Injector()

    class Config(dict):
        def __init__(self):
            super().__init__(some_key="OK")

        def __call__(self, *, name, type):
            assert name == "some_key"
            assert type is str
            return self[name]

    def fn(*, some_key: str):
        assert some_key == "OK"
        return "NICE"

    injector.provide(KwOnly(Config()))

    assert injector.exec(fn) == "NICE"
Пример #6
0
def test_injector_kwonly():
    injector = Injector()

    class Config(dict):
        def __init__(self):
            super().__init__(some_key="OK")

    def get_kwarg(config: Config, *, name, type):
        assert name == "some_key"
        assert type is str
        return config[name]

    def fn(*, some_key: str):
        assert some_key == "OK"
        return "NICE"

    injector.provide(Config)
    injector.provide(KwOnly(get_kwarg))

    assert injector.exec(fn) == "NICE"
Пример #7
0
def test_injectable_call1():
    injector = Injector()

    def kw(x: "Y", *, name, type):
        pass

    class C:
        pass

    class B:
        pass

    class A:
        c: C
        b: B

        def __init__(self, c: C, d=1, *, kwonly=None):
            pass

    injectable = injector.provide(A, provide=[KwOnly(kw), C, B])
    print(injectable)
    exit(0)
    assert isinstance(injectable(injector), A)
Пример #8
0
 def decorator(fn: OnErrorHandler) -> OnErrorHandler:
     handler = Injectable(fn, provide=[KwOnly(get_error)])
     _SERVER_ERROR.insert(0, (error, handler))
     return fn