Exemplo n.º 1
0
def test_typed(obj, input, getter, type, check):
    wrapped = typed(obj)
    result = wrapped(input)
    if getter:
        result = getter(result)
    assert check(wrapped)
    assert isinstance(result, type)
Exemplo n.º 2
0
def test_typed_varargs(func, args, kwargs, check):
    wrapped = typed(func)
    result = wrapped(*args, **kwargs)

    assert check(result)
Exemplo n.º 3
0
def test_ensure_invalid():
    with pytest.raises(TypeError):
        typed(1)
Exemplo n.º 4
0
    assert not processed
    assert result == "{"


@pytest.mark.parametrize(argnames=("annotation", ),
                         argvalues=[(typing.Any, ), (typing.Union, )])
def test_special_form(annotation):
    param = inspect.Parameter("foo",
                              inspect.Parameter.POSITIONAL_OR_KEYWORD,
                              annotation=annotation)
    assert coerce.should_coerce(param, "foo") is False


@pytest.mark.parametrize(
    argnames=("instance", "attr", "value", "type"),
    argvalues=[(typed(Data)("foo"), "foo", 1, str),
               (typed(NoParams)(), "var", 1, str)],
)
def test_setattr(instance, attr, value, type):
    setattr(instance, attr, value)
    assert isinstance(getattr(instance, attr), type)


def test_register():
    class MyCustomClass:
        def __init__(self, value: str):
            self.value = value

    class MyOtherCustomClass:
        def __init__(self, value: int):
            self.value = value
Exemplo n.º 5
0
def test_forward_ref():
    with pytest.raises(NameError):
        typed(Forward)('ref')
Exemplo n.º 6
0
def test_ensure_default_none():
    assert typed(DefaultNone)().none is None