Open
Description
Here's a quick mockup of the idea.
The idea is to provide a special token VAL that constructs validators using magic methods so you can write:
@attr.s
class Test:
positive = attr.ib(validator=[VAL.isa(int, float), VAL > 0.0])
nonempty = attr.ib(validator=[VAL.len() >= 1])
And, of course, it can report the expression in the exception it raises.
Things to support in a real version:
__getitem__
to check keys__getattr__
to allow property and method checks- the other operators, especially
in
and such __call__
validates in the mockup, butattr.ib
should tell the object to convert itself to a validator
I'd like to see what the community thinks before putting together a proper PR.