示例#1
0
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
示例#2
0
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
示例#3
0
 def g(dummy: Annotated[Dummy, From(build_dummy)] = None) -> Dummy:
     assert dummy is not None
     return dummy
示例#4
0
 def __init__(self, service1: Provide[Service1],
              service2: Annotated[Service2, From(BuildS2)],
              service3: Provide[Service3]):
     self.service1 = service1
     self.service2 = service2
     self.service3 = service3
示例#5
0
 def get(x: Annotated[Dummy, From(Maker())]):
     return x
示例#6
0
 def main(movie_db: Annotated[MovieDB, From(current_movie_db)]):
     pass
示例#7
0
 def __init__(self, imdb_api: Annotated[ImdbAPI, From(imdb_factory)]):
     self._imdb_api = imdb_api
示例#8
0
    [
        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)