Commit be40bc5c authored by Xavier Barbosa's avatar Xavier Barbosa

added injector.partial

parent dda5b2ee
Pipeline #1046 failed with stage
in 115 minutes and 41 seconds
......@@ -172,5 +172,17 @@ Closing callback can be registered::
assert foo.closed == True
Partial::
@annotate('foo', 'bar')
def fun(foo, bar):
return {'foo': foo,
'bar': bar}
partial = services.partial(fun)
assert (yield from partial()) == {'foo': 'I am foo',
'bar': 'I am bar'}
.. _asyncio: https://pypi.python.org/pypi/asyncio
.. _jeni: https://pypi.python.org/pypi/jeni
......@@ -7,6 +7,7 @@ from abc import ABCMeta
from collections import ChainMap
from contextlib import contextmanager
from contextvars import ContextVar
from functools import wraps
from inspect import signature, unwrap
from itertools import chain
from types import MappingProxyType
......@@ -168,8 +169,8 @@ class Injector(metaclass=ABCMeta):
def apply(self, *args, **kwargs) -> asyncio.Future:
with self.auto():
func, *args = args # type: ignore
func = unwrap(func)
anno = ANNOTATIONS.get(func)
orig = unwrap(func)
anno = ANNOTATIONS.get(orig)
if anno:
return self.do_apply(func, anno, args, kwargs)
fut: asyncio.Future = asyncio.Future()
......@@ -196,6 +197,18 @@ class Injector(metaclass=ABCMeta):
return asyncio.create_task(run(args, kwargs))
def partial(self, func):
orig = unwrap(func)
anno = ANNOTATIONS.get(orig)
if anno:
@wraps(func)
def parted(*args, **kwargs):
return self.do_apply(func, anno, args, kwargs)
return parted
return func
@contextmanager
def auto(self):
token = current_injector_var.set(self)
......
......@@ -167,3 +167,26 @@ async def test_descriptor_2_not_decorated(services):
toto = Toto()
with pytest.raises(LookupError):
toto.cache
@pytest.mark.asyncio
async def test_partial_sync_async(services):
@services.factory("foo")
def foo_factory():
return "I am foo"
@annotate("foo")
async def fun(foo):
return {"foo": foo}
partial = services.partial(fun)
assert await partial() == {"foo": "I am foo"}
@pytest.mark.asyncio
async def test_partial_outsider(services):
async def fun():
return {"foo": "bar"}
partial = services.partial(fun)
assert await partial() == {"foo": "bar"}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment