-
-
Notifications
You must be signed in to change notification settings - Fork 374
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
Create validators not tied to attributes #647
Comments
This seems like something for |
This may be something to think about in the context of the |
@wsanchez : What is the @hynek : @attr.s
class C(object):
x = attr.ib()
y = attr.ib()
@multi_validator(['x', 'y'])
def x_smaller_than_y(self, x, y):
if x >= y:
raise ValueError("'x' has to be smaller than 'y'!") |
@shashankchaudhry Sorry I should have included a pointer. See #645. Since we are considering validation when attributes are assigned to, then even a validator not specific to a given attribute would probably need to be triggered when you set either any or relevant-to-the-validator attributes. So a solution that only work in the context of the init cycle is probably going to be unsatisfactory. |
I’m starting to remember that at some point we had a “validate the whole instance” conversation but it got turned down? |
Currently if I need to add a validator to check a condition that affects two attributes, I have to make it a validator of one of them. For example:
This feels a little unintuitive because this validator is not really a validator on just 'x'. It should be a validator on 'x' and 'y'. It made me wonder if the order of writing x and y in the class matters, in case the validator is run right after an attribute is set.
I wonder what folks think about a generic validator of this form:
The text was updated successfully, but these errors were encountered: