def test_annotation_support(getter: Callable[[object], object]): class Maker: def __rmatmul__(self, other): return 'maker' world.test.singleton({A: A(), 'a': A(), 'maker': A()}) assert getter(Annotated[A, object()]) is world.get(A) assert getter(Annotated[A, Get('a')]) is world.get('a') # noqa: F821 assert getter(Annotated[A, From(Maker())]) is world.get('maker') with pytest.raises(TypeError): getter(Annotated[A, Get('a'), Get('a')]) # noqa: F821 with pytest.raises(TypeError): getter(Annotated[A, FromArg(lambda a: a)]) # noqa: F821
def test_multiple_antidote_annotations(injector): class Dummy: pass class Maker: def __rmatmul__(self, other): return 'dummy' annotations = [ Get('dummy'), From(Maker()), FromArg(lambda arg: arg.name), ] for (a, b) in itertools.combinations(annotations, 2): with pytest.raises(TypeError): @injector def custom_annotated(x: Annotated[Dummy, a, b]): return x
def g(dummy: Annotated[Dummy, From(build_dummy)] = None) -> Dummy: assert dummy is not None return dummy
def __init__(self, service1: Provide[Service1], service2: Annotated[Service2, From(BuildS2)], service3: Provide[Service3]): self.service1 = service1 self.service2 = service2 self.service3 = service3
def get(x: Annotated[Dummy, From(Maker())]): return x
def main(movie_db: Annotated[MovieDB, From(current_movie_db)]): pass
def __init__(self, imdb_api: Annotated[ImdbAPI, From(imdb_factory)]): self._imdb_api = imdb_api
[ pytest.param(type_hint, expected, id=str(type_hint).replace('typing.', '').replace( f"{__name__}.", "")) for type_hint, expected in [ (Annotated[Dummy, object()], None), (Dummy, None), (str, None), (T, None), (Union[str, Dummy], None), (Union[str, Dummy, int], None), (Optional[Union[str, Dummy]], None), (Callable[..., Dummy], None), (Provide[Dummy], Dummy), (Annotated[Dummy, From(Maker())], Maker), (Annotated[Dummy, FromArg(lambda arg: arg.name * 2)], 'xx'), (Annotated[Dummy, Get('something')], 'something'), # noqa: F821 ] ]) def test_extract_explicit_arg_dependency(type_hint, expected): def f(x: type_hint): pass arguments = Arguments.from_callable(f) assert extract_annotated_arg_dependency(arguments[0]) == expected def g(x: type_hint = None): pass arguments = Arguments.from_callable(g)