attrs
attrs copied to clipboard
Add decorator for convert
One can have @validator and @default but no @convert. Is this an oversight or by intention? If it's just an oversight, I'd be happy to send a PR for it.
I think it's mostly laziness on my part and a tad of indecisiveness because I'm a bit annoyed by the inconsistency of validator (noun) vs convert (verb). :-/
Just a follow up on this, I could also provide a PR similar to how validator is defined. perhaps as convertor to keep the naming consistent?
Using a toy example, would something like this be accepted?
` thing = attr.ib()
@thing.convertor
def sanitize_thing(self, raw_val):
return raw_val if isinstance(raw_val, int) else int(raw_val)
`
@devdave: I think so. We might have to bikeshed the name, but that's a smaller matter. I suggest you go with converter. Note spelling. :-)
Went with dirt simple
def converter(self, meth):
"""
Decorator that assigns *meth* as the input converter.
Returns *meth* unchanged.
"""
self.convert = meth
return meth
Adjacent, I can't find where to make a basic test for the above method. Personally, at minimum I make a stub to act as pseudo documentation and also locking the signature into a project.
Held off with submitting a PR to keep clutter down.
I’m not sure I can parse this, are you asking where to put the test for the decorator? I’d say put behind the default decorator tests in test_make.py and another one int test_dark_magic.py.
It should also be demonstrated on the examples page.
Obviously not a pull request but initial changes are here. https://github.com/devdave/attrs/tree/pr_240 Is there an ideal PR I can use as a guiding example of what I need to aim for to make sure this would be accepted?
I don't entirely understand hypothesis but get the impression it is a BDD like extension to pytest?
Well the code is fine however you’re taking advantage of the fact that the current bad name is different from your decorator. :). IOW it has tech debt built right in and it would be nice if it could be done properly right away. If you want you can wait or tackle the convert → converter dance first.
@hynek That's not too big of a problem. Starting on refactoring; instance property convert to _converter (similar to _validator), though for the record I am still partial to convertor :)
All tests passing with changes to _CountingAttr, Attribute, and the init building logic https://github.com/devdave/attrs/blob/pr_240/src/attr/_make.py#L1183
Please just open a pull request, it’s really hard to follow along like this. :)
@devdave any chance of a PR?
I think some more refactoring is required to allow the converter decorator to take self, like how default and validator work
Using @devdave changes, it currently only works like this
@thing.converter
def _thing_converter(value):
returned do_conversion(value) if ... else value
So with #1267 merged, what is needed to move this feature forward?
So with #1267 merged, what is needed to move this feature forward?
Well, someone has to implement it.