-
-
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
[RFC] __setattr__ hooks #645
Comments
I agree not doing validation on attribute set but doing them in As for the implementation, it'd be ideal if we generated a smart Not really sure what you mean with the hook proposal. Don't we have basically two hook frameworks already (validators and converters)? Both of them seem inadequate for frozen attributes though, because they run on |
I mean, we could flesh out validators so they would know if they are being run in the If I was designing this system right now, I'd probably want to use a middleware pattern (kind of like aiohttp's middleware, where it's a pipeline of functions). |
(1) When I first started using Attrs I found the lack of a validate-on-set option a bit strange but over time have become used to the validate-after-init approach ultimately still stopping execution when validators fail. So I, personally, dont have a strong preference. (2) This is a feature I would LOVE to have. To date I've been using my own kludge to get this more or less working but I'd much rather have this implemeted professionally. Would a class with all attributes frozen singly be functionally equivalent to a frozen class? Presumably not because you can add attributes to a nonfrozen class? What I would really like to have is the ability to have a class functionally frozen with only attributes marked as not-frozen remaining mutable. PS: Thank you for this amazing package. Using Attrs has resulted in a step change in the quality and productivty of my Python coding. |
@Tinche the question really is how to expose the functionality without painting ourselves into a corner. Right now I can think of three reasons the user might want to tinker with
I guess that could be achieved using As for the implementation itself I suspect there’s a cut off point where a dict is faster than a long if-then-else but that remains to be benchmarked. |
Ah #622 is an example. |
Please y'all have a look at #660. |
There's two things that people keep asking for:
Those two features have something in common: they require
attrs
to write a__setattr__
method.I actually had 1 done when I implemented validators but I took it out again, because I didn't want to tamper with
__setattr__
too. But it totally makes sense to expect that validators run there too.Now that argument has gone away thanks to frozen classes and
attrs
is in the__setattr__
business. So it feels like the right thing to do, to implement it and make it default for Operationimport attrs
(I hope this is legit the last part of the puzzle).To allow for 2 too, I would suggest to add a hook called
on_setattr
(better names welcome) that takes a callable that is called with the instance, the attribute definition, and the new value.To solve 2, the implementation would look like
Open questions:
on_setattr
attributes in a frozen class (incl inheritance)and
like validator/converter do? They would need to work as a chain, returning values for the next one.The text was updated successfully, but these errors were encountered: