class FooConfigA(object): @autoargs @validate_io(a=minlen(1)) def __init__( self, a # type: str ): pass
def function_input_builtin_stdlib(value): from valid8 import validate_arg from valid8.validation_lib import instance_of, minlen @validate_arg('s', instance_of(str), minlen(1), str.islower) def my_function(s): pass my_function(value)
def class_field_builtin_stdlib(value): from valid8 import validate_field from valid8.validation_lib import instance_of, minlen @validate_field('s', instance_of(str), minlen(1), str.islower) class Foo: def __init__(self, s): self.s = s Foo(value)
def test_numpy_nan_like_lengths(): """ Test that a strange int length with bad behaviour is correctly handled """ class NanInt(int): """ An int that behaves like numpy NaN (comparison always returns false) """ def __le__(self, other): return False def __lt__(self, other): return False def __gt__(self, other): return False def __ge__(self, other): return False nanlength = NanInt() class Foo: def __len__(self): return nanlength f = Foo() if isinstance(len(f), NanInt): # in current version of python this does not happen, but the test is ready for future evolutions with pytest.raises(TooShort) as exc_info: minlen(0)(f) with pytest.raises(TooLong) as exc_info: maxlen(10)(f) with pytest.raises(LengthNotInRange) as exc_info: length_between(0, 10)(f)
class HouseConfiguration(object): @autoargs @validate_io(name=minlen(1), surface=gts(0), nb_floors=between(1, 100, open_right=True)) def __init__(self, name: str, surface: Real, nb_floors: Optional[Integral] = 1, with_windows: Boolean = False): pass # -- overriden setter for surface for custom validation or other things @setter_override def surface(self, surface): print('Set surface to {}'.format(surface)) self.toto = 'done' self._surface = surface
class PartsOfTheAbove: @validate_io(a=gt(1), c=minlen(1)) def __init__(self, a: Integral, b: Boolean, c: Optional[List[str]] = None): pass
def test_minlen(): """ tests that the minlen() function works """ assert minlen(1)(['a']) with pytest.raises(TooShort): minlen(1)([])