You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Clarification - Converters vs on_setattr / Convert automatically when setting #835
Using the documentation on Converters it was not immediately clear that converters only work on instantiation of a new attrs-enhanced class. (Or I totally missed something.)
Is this the intended function? That the type conversion only happens on class instantiation and not when setting an attribute? (e.g. o.x = "2" in the example in the documentation)
If yes: Perhaps the documentation could be clarified to explain this and also provide a clear example of using on_setattr to also run the converter
If no: Perhaps there's a bug because setting an attribute when a converter is specified does not work
And further: from a logical design standpoint, it seems that if you're specifying a converter to be run at class instantiation [which means all values on new instances are run through that converter....] what circumstances would you not want that to run when setting attributes? Part of the purpose of converters is to ensure consistent typing and normalization (from the documentation: "Finally, sometimes you may want to normalize the values coming in. For that attrs comes with converters."). Given that, it seems strange that you could break that normalization by simply setting an attribute.
TL;DR: Converters only run on class instantiation. Shouldn't they be run when setting an attribute as well?
Marking as documentation because on_setattr seems to lack narrative docs, and running converters and validators on set as well as init time is probably a common enough use case to warrant some text.
I just ran into this behavior that validators are run on instantiation only.
Please find time to add this to https://www.attrs.org/en/stable/examples.html#validators as a warning. Its behavior is fine as it is as long as its documented.
As far as documenting on_setattr is concerned doing it bellow validators on same page would be nice.
I have tried to implement it the same way i did validator (by using decorator example) but ended up with TypeError: 'NoneType" object is not callable. In the end i just used standard python way to define setter via decorator.
Thanks
I have added blurbs in 95b70bd, although I hope that by the next release, the examples will be rewritten using NG APIs.
As for the original issue – I'm willing to break b/w compat here since the APIs are new and the impact is limited to few people. I honestly don't remember why I chose to only run validators – maybe because people kept complaining about it and I just…forgot about converters?
Activity
wsanchez commentedon Aug 23, 2021
Marking as documentation because
on_setattrseems to lack narrative docs, and running converters and validators on set as well as init time is probably a common enough use case to warrant some text.hynek commentedon Aug 29, 2021
I'm not sure where to put narrative information…should we open a new chapter for it?
MarkoBox commentedon Oct 27, 2021
I just ran into this behavior that validators are run on instantiation only.
Please find time to add this to https://www.attrs.org/en/stable/examples.html#validators as a warning. Its behavior is fine as it is as long as its documented.
As far as documenting on_setattr is concerned doing it bellow validators on same page would be nice.
I have tried to implement it the same way i did validator (by using decorator example) but ended up with TypeError: 'NoneType" object is not callable. In the end i just used standard python way to define setter via decorator.
Thanks
hynek commentedon Oct 28, 2021
I have added blurbs in 95b70bd, although I hope that by the next release, the examples will be rewritten using NG APIs.
As for the original issue – I'm willing to break b/w compat here since the APIs are new and the impact is limited to few people. I honestly don't remember why I chose to only run validators – maybe because people kept complaining about it and I just…forgot about converters?
bitranox commentedon Nov 7, 2021
@hynek
I am also complaining ! ;-)
I would definitely need the converters also on_setattr !
better yesterday then tomorrow ;-), i just needed to write ugly and slow own converters -
it would have saved me a lot of effort it that would work.
I just came here to put a question why it is not working - and landed at this thread.
yours sincerely, und Grüße nach Berlin
bitranox, Vienna
NG: convert on setattr by default
hynek commentedon Dec 13, 2021
please see #886
NG: convert on setattr by default
bitranox commentedon Dec 13, 2021
Dankeschön !
NG: convert on setattr by default
NG: convert on setattr by default (#886)
Merge #465 #466
Merge #465 #467
⬆️ Bump attrs from 21.2.0 to 21.3.0 (#18)
Merge #3746 #3747
Merge #465 #467