[Python-ideas] Re: dataclass field argument to allow converting value on init

2022-07-02 Thread Dexter Hill
That's clever, I'm a fan of that syntax. Quick question on it though - If you provide a `default` or `default_factory` to `field`, as well as `init_using`, how would that be handled? I'm thinking, `default_factory` would be made mutually exclusive, so you couldn't use it but `default` would just

[Python-ideas] Re: dataclass field argument to allow converting value on init

2022-07-01 Thread Dexter Hill
Steve Jorgensen wrote: > Would we want something more general that could deal with cases where the > input does not have a 1-to-1 mapping to the field that differ only, perhaps, > in type hint? What if we want 1 argument to initializes 2 properties or vice > verse, etc.? That's definitely an imp

[Python-ideas] Re: dataclass field argument to allow converting value on init

2022-06-28 Thread Dexter Hill
Ah right I see what you mean. In my example I avoided the use of `__init__` and specifically `__post_init__` as (and it's probably a fairly uncommon use case), in my actual project, `__post_init__` is defined on a base class, and inherited by all other classes, and I wanted to avoid overriding `

[Python-ideas] Re: dataclass field argument to allow converting value on init

2022-06-25 Thread Dexter Hill
Do you mind providing a little example of what you mean? I'm not sure I 100% understand what your use of `__post_init__` is. In my mind, it would be something like: ```py @dataclass class Foo: x: str = field(init=int, converter=chr) # which converts to class Foo: def __init__(self, x: in

[Python-ideas] Re: dataclass field argument to allow converting value on init

2022-06-24 Thread Dexter Hill
I don't mind that solution although my concern is whether it would be confusing to have `init` have two different purposes depending on the argument. And, if `__post_init__` was overrided, which I would say it commonly is, that would mean the user would have to manually do the conversion, as wel

[Python-ideas] Re: dataclass field argument to allow converting value on init

2022-06-24 Thread Dexter Hill
Didn't think about `attrs` but yes, the converter functionality at a glance looks exactly like I'd imagined it would function in dataclasses. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@pyth

[Python-ideas] Re: dataclass field argument to allow converting value on init

2022-06-23 Thread Dexter Hill
Interesting point, it's not something I thought of. One solution as mentioned by Simão, and what I had in mind, is to pull the type from the first parameter of the function. We know that the function is always going to have minumum 1 parameter, and the value is always passed as the first argumen

[Python-ideas] dataclass field argument to allow converting value on init

2022-06-22 Thread Dexter Hill
The idea is to have a `default_factory` like argument (either in the `field` function, or a new function entirely) that takes a function as an argument, and that function, with the value provided by `__init__`, is called and the return value is used as the value for the respective field. For exa