Пример #1
0
def test_magic_methods_arguments():
    arguments = Arguments(arguments=args,
                          has_var_keyword=False,
                          has_var_positional=False,
                          has_self=False)
    assert 'x' in arguments
    assert 'unknown' not in arguments
    assert 3 == len(arguments)
    assert args == tuple(arguments)
    assert "x:int" in repr(arguments)
    assert "y:str =" in repr(arguments)
    assert "z:float" in repr(arguments)

    arguments_var_args = Arguments(arguments=args,
                                   has_var_keyword=False,
                                   has_var_positional=True,
                                   has_self=False)
    assert "x:int" in repr(arguments_var_args)
    assert "y:str =" in repr(arguments_var_args)
    assert "z:float" in repr(arguments_var_args)
    assert "*args" in repr(arguments_var_args)

    arguments_var_kwargs = Arguments(arguments=args,
                                     has_var_keyword=True,
                                     has_var_positional=False,
                                     has_self=False)
    assert "x:int" in repr(arguments_var_kwargs)
    assert "y:str =" in repr(arguments_var_kwargs)
    assert "z:float" in repr(arguments_var_kwargs)
    assert "**kwargs" in repr(arguments_var_kwargs)
Пример #2
0
def test_from_methods(descriptor):
    def f(a, b=None):
        pass

    expected = Arguments.from_callable(f)
    result = Arguments.from_method(descriptor(f))

    for expected_arg, result_arg in zip(expected, result):
        assert expected_arg.name == result_arg.name
        assert expected_arg.has_default == result_arg.has_default
        assert expected_arg.type_hint == result_arg.type_hint
Пример #3
0
def test_extract_auto_provided_arg_dependency(type_hint, expected):
    def f(x: type_hint):
        pass

    arguments = Arguments.from_callable(f)
    assert extract_auto_provided_arg_dependency(arguments[0]) == expected

    def g(x: type_hint = None):
        pass

    arguments = Arguments.from_callable(g)
    assert extract_auto_provided_arg_dependency(arguments[0]) == expected
Пример #4
0
def test_without_self():
    for has_var_keyword, has_var_positional in itertools.product(
        [True, False], [True, False]):
        arguments = Arguments(arguments=args,
                              has_var_keyword=has_var_keyword,
                              has_var_positional=has_var_positional,
                              has_self=True)
        assert tuple(arguments.without_self) == args[1:]
        assert arguments.without_self.has_var_keyword == arguments.has_var_keyword
        assert arguments.without_self.has_var_positional == arguments.has_var_positional

    arguments = Arguments(arguments=args,
                          has_var_keyword=False,
                          has_var_positional=False,
                          has_self=False)
    assert arguments.without_self is arguments
Пример #5
0
def test_getitem():
    arguments = Arguments(arguments=args,
                          has_var_keyword=False,
                          has_var_positional=False,
                          has_self=False)
    assert arguments['x'] is args[0]
    assert arguments[1] is args[1]

    with pytest.raises(TypeError):
        arguments[2.3]
Пример #6
0
def test_unknown_antidote_annotations():
    type_hint = Annotated[Dummy, AntidoteAnnotation()]

    def f(x: type_hint):
        pass

    arguments = Arguments.from_callable(f)
    with pytest.raises(TypeError):
        extract_annotated_arg_dependency(arguments[0])

    with pytest.raises(TypeError):
        extract_annotated_dependency(type_hint)
Пример #7
0
def test_multiple_antidote_annotations():
    type_hint = Annotated[Dummy, Get('dummy'), Get('dummy')]  # noqa: F821

    def f(x: type_hint):
        pass

    arguments = Arguments.from_callable(f)
    with pytest.raises(TypeError):
        extract_annotated_arg_dependency(arguments[0])

    with pytest.raises(TypeError):
        extract_annotated_dependency(type_hint)
Пример #8
0
def test_arguments():
    container = DependencyContainer()
    container.update_singletons(dict(a=12, b=24))

    def f(a, b):
        pass

    arguments = Arguments.from_callable(f)

    @inject(arguments=arguments, use_names=True, container=container)
    def g(**kwargs):
        return kwargs

    assert dict(a=12, b=24) == g()
Пример #9
0
def test_magic_methods_arguments():
    arguments = tuple([
        Argument('x', False, int),
        Argument('y', True, str),
        Argument('z', False, float),
    ])
    args = Arguments(arguments=arguments,
                     has_var_keyword=True,
                     has_var_positional=True)

    assert 'x' in args
    assert 'unknown' not in args
    assert 3 == len(args)
    assert arguments == tuple(args)
Пример #10
0
def test_from_callable(func, expected: Arguments):
    result = Arguments.from_callable(func)
    assert isinstance(result, Arguments)
    assert expected.has_var_positional == result.has_var_positional
    assert expected.has_var_keyword == result.has_var_keyword

    for expected_arg, result_arg in zip(expected, result):
        assert expected_arg.name == result_arg.name
        assert expected_arg.has_default == result_arg.has_default
        assert expected_arg.type_hint == result_arg.type_hint

    for expected_arg in expected:
        assert expected_arg.name in result

        result_arg = result[expected_arg.name]
        assert expected_arg.name == result_arg.name
        assert expected_arg.has_default == result_arg.has_default
        assert expected_arg.type_hint == result_arg.type_hint
Пример #11
0
def test_broken_type_hints_cpy353(monkeypatch):
    monkeypatch.setattr('antidote._internal.argspec.get_type_hints',
                        raiser(Exception))
    Arguments.from_callable(k)
Пример #12
0

d = Dummy()


def lazy(dummy: 'Dummy'):
    pass


@pytest.mark.parametrize('func,expected', [
    pytest.param(f,
                 Arguments(
                     arguments=tuple([
                         Argument('a', False, str),
                         Argument('b', False, None),
                         Argument('c', True, int),
                     ]),
                     has_var_positional=False,
                     has_var_keyword=False,
                 ),
                 id='f'),
    pytest.param(g,
                 Arguments(
                     arguments=tuple([
                         Argument('a', False, list),
                     ]),
                     has_var_positional=True,
                     has_var_keyword=False,
                 ),
                 id='g'),
    pytest.param(h,
Пример #13
0
def test_invalid_callable():
    with pytest.raises(TypeError):
        Arguments.from_callable(object())