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)
def test_typed_varargs(func, args, kwargs, check): wrapped = typed(func) result = wrapped(*args, **kwargs) assert check(result)
def test_ensure_invalid(): with pytest.raises(TypeError): typed(1)
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
def test_forward_ref(): with pytest.raises(NameError): typed(Forward)('ref')
def test_ensure_default_none(): assert typed(DefaultNone)().none is None