New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multiple alternatives validator #1214
Labels
Comments
FTR, it's already possible to do that using only from attrs.validator import instance_of, deep_iterable, and_, not_
@define
class C:
x: A | B | tuple[int, ...] = field(
validator= not_(and_(
not_(instance_of((A, B))),
not_(deep_iterable(instance_of(int)))))) But the error message is not very informative: >>> @define
... class C:
... x = field(validator=not_( and_( not_( instance_of((A, B)) ), not_( deep_iterable(instance_of(int)) ) ) ))
...
>>> C(A())
C(x=A())
>>> C(B())
C(x=B())
>>> C(())
C(x=())
>>> C((1, 2, 3))
C(x=(1, 2, 3))
>>> C((1, 2, 3, '4'))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<attrs generated init __main__.C>", line 5, in __init__
File "<REDACTED>/lib/python3.12/site-packages/attr/validators.py", line 670, in __call__
raise ValueError(
ValueError: ("not_ validator child '_AndValidator(_validators=(<not_ validator wrapping <instance_of validator for type (<class '__main__.A'>, <class '__main__.B'>)>, capturing (<class 'ValueError'>, <class 'TypeError'>)>, <not_ validator wrapping <deep_iterable validator for iterables of <instance_of validator for type <class 'int'>>>, capturing (<class 'ValueError'>, <class 'TypeError'>)>))' did not raise a captured error", Attribute(name='x', default=NOTHING, validator=<not_ validator wrapping _AndValidator(_validators=(<not_ validator wrapping <instance_of validator for type (<class '__main__.A'>, <class '__main__.B'>)>, capturing (<class 'ValueError'>, <class 'TypeError'>)>, <not_ validator wrapping <deep_iterable validator for iterables of <instance_of validator for type <class 'int'>>>, capturing (<class 'ValueError'>, <class 'TypeError'>)>)), capturing (<class 'ValueError'>, <class 'TypeError'>)>, repr=True, eq=True, eq_key=None, order=True, order_key=None, hash=None, init=True, metadata=mappingproxy({}), type=None, converter=None, kw_only=False, inherited=False, on_setattr=None, alias='x'), _AndValidator(_validators=(<not_ validator wrapping <instance_of validator for type (<class '__main__.A'>, <class '__main__.B'>)>, capturing (<class 'ValueError'>, <class 'TypeError'>)>, <not_ validator wrapping <deep_iterable validator for iterables of <instance_of validator for type <class 'int'>>>, capturing (<class 'ValueError'>, <class 'TypeError'>)>)), (1, 2, 3, '4'), (<class 'ValueError'>, <class 'TypeError'>)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'd like to validate that a field can be of several types, one of which is an iterator.
I can use
instance_of
to check the outer typetuple
, but I can't usedeep_iterable
to validate that they are indeed the element typeint
.Since we already have validators
and_
andnot_
, it may make sense to also addor_
, so that I can represent this withFor comparison, this is how I'm writing a custom validator
The text was updated successfully, but these errors were encountered: