class Between(Comparable, Annotable): value = IsInt lower = Optional(IsInt, default=0) upper = Optional(IsInt, default=None) def __equals__(self, other): return self.args == other.args
def Argument(validator, default=EMPTY): """Argument constructor Parameters ---------- validator : Union[Callable[[arg], coerced], Type, Tuple[Type]] Function which handles validation and/or coercion of the given argument. default : Union[Any, Callable[[], str]] In case of missing (None) value for validation this will be used. Note, that default value (except for None) must also pass the inner validator. If callable is passed, it will be executed just before the inner, and itsreturn value will be treaded as default. """ if isinstance(validator, Validator): pass elif isinstance(validator, type): validator = _InstanceOf(validator) elif isinstance(validator, tuple): assert all_of(validator, type) validator = _InstanceOf(validator) elif isinstance(validator, Validator): validator = validator elif callable(validator): validator = _ValidatorFunction(validator) else: raise TypeError( 'Argument validator must be a callable, type or ' 'tuple of types, given: {}'.format(validator) ) if default is EMPTY: return validator else: return Optional(validator, default=default)
def test_signature_inheritance(): class IntBinop(Annotable): left = IsInt right = IsInt class FloatAddRhs(IntBinop): right = IsFloat class FloatAddClip(FloatAddRhs): left = IsFloat clip_lower = Optional(IsInt, default=0) clip_upper = Optional(IsInt, default=10) class IntAddClip(FloatAddClip, IntBinop): pass assert IntBinop.__signature__ == Signature([ Parameter('left', validator=IsInt), Parameter('right', validator=IsInt), ]) assert FloatAddRhs.__signature__ == Signature([ Parameter('left', validator=IsInt), Parameter('right', validator=IsFloat), ]) assert FloatAddClip.__signature__ == Signature([ Parameter('left', validator=IsFloat), Parameter('right', validator=IsFloat), Parameter('clip_lower', validator=Optional(IsInt, default=0)), Parameter('clip_upper', validator=Optional(IsInt, default=10)), ]) assert IntAddClip.__signature__ == Signature([ Parameter('left', validator=IsInt), Parameter('right', validator=IsInt), Parameter('clip_lower', validator=Optional(IsInt, default=0)), Parameter('clip_upper', validator=Optional(IsInt, default=10)), ])
class Between(Annotable): value = IsInt lower = Optional(IsInt, default=0) upper = Optional(IsInt, default=None)
class Between(ValueOp, ConditionalOp): min = IsInt max = IsInt how = Optional(IsStr, default="strict")
class ConditionalOp(Annotable): where = Optional(IsBool, default=False)
class Sum(VersionedOp, Reduction): where = Optional(IsBool, default=False)
class Op(Annotable): arg = Optional(InstanceOf(tuple), default=default)
class Beta(Alpha): c = IsInt d = Optional(IsInt, default=0) e = IsInt
class NoHooves(Farm): horses = Optional(IsInt, default=0) goats = Optional(IsInt, default=0) donkeys = Optional(IsInt, default=0)
class FloatAddClip(FloatAddRhs): left = IsFloat clip_lower = Optional(IsInt, default=0) clip_upper = Optional(IsInt, default=10)
def test_optional_argument(default, expected): validator = Optional(lambda x: x, default=default) assert validator(None) == expected
IsInt = InstanceOf(int) @pytest.mark.parametrize( ('default', 'expected'), [(None, None), (0, 0), ('default', 'default'), (lambda: 3, 3)], ) def test_optional_argument(default, expected): validator = Optional(lambda x: x, default=default) assert validator(None) == expected @pytest.mark.parametrize( ('validator', 'value', 'expected'), [ (Optional(identity, default=None), None, None), (Optional(identity, default=None), 'three', 'three'), (Optional(identity, default=1), None, 1), (Optional(identity, default=lambda: 8), 'cat', 'cat'), (Optional(identity, default=lambda: 8), None, 8), (Optional(int, default=11), None, 11), (Optional(int, default=None), None, None), (Optional(int, default=None), 18, 18), (Optional(str, default=None), 'caracal', 'caracal'), ], ) def test_valid_optional(validator, value, expected): assert validator(value) == expected @pytest.mark.parametrize(