attrs icon indicating copy to clipboard operation
attrs copied to clipboard

Add decorator for convert

Open agronholm opened this issue 8 years ago • 14 comments
trafficstars

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.

agronholm avatar Sep 01 '17 11:09 agronholm

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

hynek avatar Sep 24 '17 13:09 hynek

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 avatar Nov 11 '17 14:11 devdave

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

wsanchez avatar Nov 14 '17 00:11 wsanchez

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.

devdave avatar Nov 23 '17 15:11 devdave

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.

hynek avatar Nov 26 '17 14:11 hynek

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?

devdave avatar Nov 27 '17 15:11 devdave

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 avatar Dec 03 '17 16:12 hynek

@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 :)

devdave avatar Dec 03 '17 23:12 devdave

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

devdave avatar Dec 07 '17 07:12 devdave

Please just open a pull request, it’s really hard to follow along like this. :)

hynek avatar Dec 07 '17 13:12 hynek

@devdave any chance of a PR?

hynek avatar Jan 17 '18 12:01 hynek

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

coder6464 avatar Jan 22 '18 11:01 coder6464

So with #1267 merged, what is needed to move this feature forward?

LecrisUT avatar Sep 05 '24 08:09 LecrisUT

So with #1267 merged, what is needed to move this feature forward?

Well, someone has to implement it.

hynek avatar Oct 08 '24 07:10 hynek