Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
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. |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

This adds an
init_validationflag toattr.sthat 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!
.rstfiles is written using semantic newlines.versionadded,versionchanged, ordeprecateddirectives.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!