-
-
Notifications
You must be signed in to change notification settings - Fork 373
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
Add init_validation flag #233
Conversation
init_validation controls whether validation runs during __init__. By default it is True, which is the current behavior. If it is set to False then validators will not be run.
Codecov Report
@@ Coverage Diff @@
## master #233 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 9 9
Lines 603 603
Branches 132 132
=====================================
Hits 603 603
Continue to review full report at Codecov.
|
By the way, I can add more documentation if that is helpful. Initially I wanted to confirm that this is an okay change to make before investing time in that. |
I don’t know if it’s me but it seems really weird to me to define validators on a class and tell that class to not run the at the same time? The reason why the setting is currently out of the class is that you may want to disable them under certain conditions that are none of the class’ business… |
I want explicit control over when the validators run. Validation can sometimes be expensive depending on the class. Because the validators run automatically in There are two approaches I could think of: 1) Add a |
I see – I have never considered validators something that has any value outside |
I think there's a good argument for running validation when setting an attribute. In both the init and setter case, though, it seems like skipping validation should be a thing you decide at init/set time, rather than at class definition time, though… no? Deciding this on this class-level still a global setting, though a narrower one. |
@hynek Enumerating the validation options like that makes sense to me. Seems these would be defined as:
@wsanchez I agree there's a good argument for at least making it configurable (as above) whether validation should be run automatically when setting an attribute. Regarding what you said about deciding at init/set time, I think it's reasonable to have an option to configure at the class level. If you have a class where don't want validation to run most of the time, then it's easier to be able to set it at the class level. This is how I coded my class with the Having the ability to decide at init/set time could be useful too though in addition to having the class level configurability. I can think of a couple potentially useful methods for controlling attribute validation.
However with I would prefer a |
Here's my thought process on this issue. At first I was confused since what's the point of defining validators but never running them, but then I got reminded by the PR comment that we can run validation manually. This feature slipped my mind completely. :) Then I realized I have a similar situation at work; I load a bunch of data from an Excel file into my attrs classes, validate everything, and serialize the classes into Redis. Once the data is in Redis, it can be loaded without validation; validation is an unnecessary overhead at that point. So this interests me on a practical level. (Site note: this problem is solvable currently by sticking your validators somewhere else [like the metadata] and writing your own version of My gut reaction towards this proposed solution was negative, however. A more elegant solution is probably possible and we should brainstorm first. The question of running validators on set is interesting. This requires special descriptors; finally removing deprecated What I think would be really interesting is exposing some of the attrs machinery for generating This is one of the ways we could support generating static factory methods. The normal We already have another issue open (#187) dealing with reworking the global validation switch; again a testament to the complexity of our init handling code. This issue could be resolved by exposing some of our |
Ehm |
This is the same situation I am in and what inspired this PR. I have already validated the data and serialized into a DB, so any further validation after is unnecessary and wasteful.
Yea what I basically want is a way to customize how |
So just to be clear before I leave on my vacation: I’m not opposed to this feature, but I’m opposed to |
Hi, |
I haven't had time to continue working on this. If you are interested you could pick it up. |
Any word on this? I have a use case that could benefit from this PR. |
Actually, #750 makes this trivial to implement. |
I think given the age and overall comment, I'm gonna close this. I personally don't think it makes sense to make our validators "manual". I 100% support the use-case (as does Tin), I just don't think of attrs's validators worth it given much more superior solutions out there like cattrs. It's always difficult to delineate when to use ours vs when to use a more powerful framework and that's why I've let this PR linger this long so gracelessly. I'm gonna stick a pole in the ground and say from our side, our validators are for when you want to run them always. For use-cases like tests etc, there's always the way to disable them temporarily (e.g. using the brand new #859). Sorry everyone who's disappointed! |
This adds an
init_validation
flag toattr.s
that controls whether validators are run at the end of the__init__()
method. This allows one to choose when validation should be run, which can be useful in some situations.Pull Request Check List
This is just a reminder about the most common mistakes. Please make sure that you tick all appropriate boxes. But please read our contribution guide at least once, it will save you unnecessary review cycles!
.rst
files is written using semantic newlines.versionadded
,versionchanged
, ordeprecated
directives.CHANGELOG.rst
.If you have any questions to any of the points above, just submit and ask! This checklist is here to help you, not to deter you from contributing!