Skip to content

Clarification - Converters vs on_setattr / Convert automatically when setting  #835

Closed

Description

@danieldjewell

attrs version: 21.2.0

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.)

  1. 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?

Activity

  1. wsanchez commented on Aug 23, 2021

    @wsanchez

    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.

  2. hynek commented on Aug 29, 2021

    @hynek
    Member

    I'm not sure where to put narrative information…should we open a new chapter for it?

  3. MarkoBox commented on Oct 27, 2021

    @MarkoBox

    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

  4. hynek commented on Oct 28, 2021

    @hynek
    Member

    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?

  5. bitranox commented on Nov 7, 2021

    @bitranox

    @hynek
    I am also complaining ! ;-)
    I would definitely need the converters also on_setattr !

    @attr.define(slots=True)
    class MyDataClass(object):
        my_value=attr.ib(default=0, converter=int)
    
    >>> my_data=MyDataClass()
    >>> my_data.my_value='1'
    >>> assert 1 == my_data.my_value
    

    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

  6. modified the milestones: import attrs, 21.3.0 on Nov 22, 2021
  7. added a commit that references this issue on Dec 13, 2021
    55cda74
  8. hynek commented on Dec 13, 2021

    @hynek
    Member

    please see #886

  9. added a commit that references this issue on Dec 13, 2021
    077ace4
  10. bitranox commented on Dec 13, 2021

    @bitranox

    Dankeschön !

  11. added a commit that references this issue on Dec 13, 2021
    fedc570
  12. added a commit that references this issue on Dec 14, 2021
    679e4b4
  13. added 2 commits that reference this issue on Dec 28, 2021
  14. added a commit that references this issue on Dec 29, 2021
  15. added a commit that references this issue on Jan 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions