[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

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

2022-07-01 Thread Steve Jorgensen
Dexter Hill wrote: > 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,

[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

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

2022-06-29 Thread Paul Bryan
I think we are saying the same thing. I don't think this would require another parameter if type checkers are capable of inferring the type from the parameter to the function to be called on init. On Wed, 2022-06-29 at 21:26 +, Steve Jorgensen wrote: > Paul Bryan wrote: > > Could the type

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

2022-06-29 Thread Steve Jorgensen
Paul Bryan wrote: > Could the type hint for the __init__ parameter be inferred from the > (proposed) init_fn's own parameter type hint itself? > On Tue, 2022-06-28 at 16:39 +, Steve Jorgensen wrote: I think I was already suggesting that possibility "an optional argument to `InitFn` or maybe

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

2022-06-28 Thread Paul Bryan
Could the type hint for the __init__ parameter be inferred from the (proposed) init_fn's own parameter type hint itself? On Tue, 2022-06-28 at 16:39 +, Steve Jorgensen wrote: > Dexter Hill wrote: > > Ah right I see what you mean. In my example I avoided the use of > > `__init__` and

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

2022-06-28 Thread Steve Jorgensen
Dexter Hill wrote: > 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

[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 Steve Jorgensen
Dexter Hill wrote: > 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 >

[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:

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

2022-06-25 Thread Steve Jorgensen
Dexter Hill wrote: > 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

[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

[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] Re: dataclass field argument to allow converting value on init

2022-06-24 Thread Steve Jorgensen
Steve Jorgensen wrote: > Simão Afonso wrote: > > On 2022-06-23 17:35:59, Steve Jorgensen wrote: > > What if, instead, the `init` parameter could accept either a boolean > > (as it does now) or a type? When given a type, that would mean that to > > created the property and accept the argument but

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

2022-06-24 Thread Steve Jorgensen
Simão Afonso wrote: > On 2022-06-23 17:35:59, Steve Jorgensen wrote: > > What if, instead, the `init` parameter could accept either a boolean > > (as it does now) or a type? When given a type, that would mean that to > > created the property and accept the argument but pass the argument ti > >

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

2022-06-24 Thread Simão Afonso
On 2022-06-23 17:35:59, Steve Jorgensen wrote: > What if, instead, the `init` parameter could accept either a boolean > (as it does now) or a type? When given a type, that would mean that to > created the property and accept the argument but pass the argument ti > `__post_init__` rather than using

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

2022-06-23 Thread Thomas Kehrenberg
This is what attrs' converter functionality is, right? https://www.attrs.org/en/stable/init.html#converters ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org

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

2022-06-23 Thread Steve Jorgensen
Sorry for typos. ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at

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

2022-06-23 Thread Steve Jorgensen
Dexter Hill wrote: > 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

[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

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

2022-06-23 Thread Simão Afonso
On 2022-06-23 00:03:03, Paul Bryan wrote: > What type hint will be exposed for the __init__ parameter? Clearly, > it's not a `str` type in your example; you're passing it an `int` value > in your example. Presumably to overcome this, you'd need yet another > `field` function parameter to provide

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

2022-06-23 Thread Paul Bryan
What type hint will be exposed for the __init__ parameter? Clearly, it's not a `str` type in your example; you're passing it an `int` value in your example. Presumably to overcome this, you'd need yet another `field` function parameter to provide the type hint for the `__init__` param? On Wed,