Skip to content
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

Pass the attribute name to the converter? (3 arg converters) #1108

Open
YAmikep opened this issue Feb 28, 2023 · 4 comments
Open

Pass the attribute name to the converter? (3 arg converters) #1108

YAmikep opened this issue Feb 28, 2023 · 4 comments

Comments

@YAmikep
Copy link

YAmikep commented Feb 28, 2023

This is not a bug per se but more an improvement.

From my understanding, converters are passed only the value today.
Would it be possible to pass the attribute name as well?

The motivation behind this is because as you try to convert/parse data in the converter, you end up adding some validation as well and it would be helpful to know for what attribute it is run for to have a better error message. (e.g.: f"{attribute} value cannot be converted to a date")

In the example below, the converter tries to convert the input into a date object.
This can fail and it would make sense to raise a validation error at that point but it would help to know the attribute this is for.

Thoughts?

import attr
from datetime import date


def convert_to_date(value):
    try:
        dt_obj = ... # convert value
    exception:
        raise ValueError('Cannot convert to date') # would like  f"{attribute} value cannot be converted  to a date" instead
    
    return dt_obj


@attr.s(kw_only=True)
class Params:
    day1: date = attr.ib(converter=convert_to_date)
    day2: date = attr.ib(converter=convert_to_date)
    day3: date = attr.ib(converter=convert_to_date)
    day4: date = attr.ib(converter=convert_to_date)
    day5: date = attr.ib(converter=convert_to_date)
    day6: date = attr.ib(converter=convert_to_date)
    day7: date = attr.ib(converter=convert_to_date)
    
@chrysle
Copy link
Contributor

chrysle commented Feb 28, 2023

I think this could be a useful feature.

But I don't see another way than passing the attribute's name by hand. It should of course be possible to configure the converter callable to accept some parameters (if it doesn't work already, have you tried that?).

@YAmikep
Copy link
Author

YAmikep commented Feb 28, 2023

What do you mean "by hand"?

The validators are called with instance, attribute, value by attrs.
Could attrs call the converters with the same or maybe without the instance?

At the moment, only the value is passed to the converters.

@hynek
Copy link
Member

hynek commented Mar 1, 2023

I have started to work on three-arg converters before my vacation based on #404. But it’s a bit tricky to get it right without a performance penalty.

@tclose
Copy link

tclose commented Mar 2, 2023

3 arg converters would be great, I'm often wanting to write generic converters that know the type of the attribute they need to convert to

@YAmikep YAmikep changed the title Pass the attribute name to the converter? Pass the attribute name to the converter? (3 arg converters) Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants