[issue39247] dataclass defaults and property don't work together

2021-10-21 Thread Thomas
Thomas added the comment: > An example of multiple descriptors would be to have: > @cached_property > @property > def expensive_calc(self): > #Do something expensive That's decorator chaining. The example you gave is not working code (try to return something from expensive_calc and

[issue39247] dataclass defaults and property don't work together

2021-10-21 Thread Thomas
Thomas added the comment: Just to rephrase, because the explanation in my last message can be ambiguous: At dataclass construction time (when the @dataclass decorator inspects and enhances the class): for field in fields: if descriptor := getattr(field, 'descriptor'):

[issue39247] dataclass defaults and property don't work together

2021-10-21 Thread Michael Robellard
Michael Robellard added the comment: An example of multiple descriptors would be to have: @cached_property @property def expensive_calc(self): #Do something expensive -- ___ Python tracker

[issue39247] dataclass defaults and property don't work together

2021-10-21 Thread Thomas
Thomas added the comment: Agreed on everything but that last part, which I'm not sure I understand: > If we allow descriptor to accept an iterable as well you could have multiple > descriptors just like normal. Could you give an example of what you mean with a regular class? I've had a bit

[issue39247] dataclass defaults and property don't work together

2021-10-21 Thread Michael Robellard
Michael Robellard added the comment: I can confirm that Juan Arrivillaga (juanpa.arrivillaga) workaround does work. Given that it works, then wouldn't it be relatively trivial to do what Thomas701 suggests and add a descriptor parameter to fields. Then apply the descriptor after all the

[issue39247] dataclass defaults and property don't work together

2021-10-21 Thread Thomas
Thomas added the comment: Scratch that last one, it leads to problem when mixing descriptors with actual default values: @dataclass class Foo: bar = field(default=some_descriptor) # technically this is a descriptor field without a default value or at the very least, the dataclass

[issue39247] dataclass defaults and property don't work together

2021-10-21 Thread Thomas
Thomas added the comment: Thinking a little more about this, maybe a different solution would be to have default values be installed at the class level by default without being overwritten in the init, as is the case today. default_factory should keep being set in the init as is the case

[issue39247] dataclass defaults and property don't work together

2021-10-21 Thread Thomas
Thomas added the comment: Hello everyone, A quick look on SO and Google + this python issue + this blog post and its comments: https://florimond.dev/en/posts/2018/10/reconciling-dataclasses-and-properties-in-python/ show that this is still a problem where dataclass users keep hitting a

[issue39247] dataclass defaults and property don't work together

2021-06-05 Thread Benjamin Lee
Benjamin Lee added the comment: > I'm not sure "alias" feels quite right, as it only applies to __init__ (if > I'm understanding it correctly). Maybe `init_alias` might be a better name. In any case, this would support private variables in dataclasses. --

[issue39247] dataclass defaults and property don't work together

2021-06-05 Thread Michael Robellard
Michael Robellard added the comment: The sample I uploaded doesn't do any processing, but the use case originally had some logic inside the property getter/setter, would the alias idea allow for that? The purpose of the property is to add some logic to compute the value if it has not

[issue39247] dataclass defaults and property don't work together

2021-06-05 Thread Eric V. Smith
Eric V. Smith added the comment: > _uploaded_by: str = dataclasses.field(alias="uploaded_by", default=None, > init=False) That's an interesting idea. I'll play around with it. I'm not sure "alias" feels quite right, as it only applies to __init__ (if I'm understanding it correctly).

[issue39247] dataclass defaults and property don't work together

2021-06-05 Thread Benjamin Lee
Benjamin Lee added the comment: Would this issue not be trivially resolved if there was a way to specify alias in the dataclasses field? I.e.: _uploaded_by: str = dataclasses.field(alias="uploaded_by", default=None, init=False) Ultimately, the main goal is to make it so that the generated

[issue39247] dataclass defaults and property don't work together

2020-06-18 Thread Ivan Ivanyuk
Ivan Ivanyuk added the comment: Was there some solution in progress here? We would like to use dataclasses and seems this problem currently limits their usefulness to us. We recently came upon the same behaviour https://mail.python.org/pipermail/python-list/2020-June/897502.html and I was

[issue39247] dataclass defaults and property don't work together

2020-04-15 Thread Juan Arrivillaga
Juan Arrivillaga added the comment: But when would you want to have a descriptor as an instance attribute? Descriptors must be in the class dictionary to work: https://docs.python.org/3/reference/datamodel.html#implementing-descriptors I suppose, you could want some container class of

[issue39247] dataclass defaults and property don't work together

2020-01-10 Thread Eric V. Smith
Eric V. Smith added the comment: > During this processing of fields, couldn't you just special case > property/descriptor objects? What if you want the field to be a descriptor? I think the best way of handling this would be to use some sentinel value for the default, and if found look up

[issue39247] dataclass defaults and property don't work together

2020-01-10 Thread Juan Arrivillaga
Juan Arrivillaga added the comment: Actually, couldn't the following be a workaround, just set the property on the class after the class definition: import dataclasses import typing @dataclasses.dataclass class FileObject: uploaded_by:typing.Optional[None]=None def

[issue39247] dataclass defaults and property don't work together

2020-01-10 Thread Juan Arrivillaga
Juan Arrivillaga added the comment: So, after glancing at the source code: https://github.com/python/cpython/blob/ce54519aa09772f4173b8c17410ed77e403f3ebf/Lib/dataclasses.py#L869 During this processing of fields, couldn't you just special case property/descriptor objects? -- nosy:

[issue39247] dataclass defaults and property don't work together

2020-01-07 Thread Eric V. Smith
Eric V. Smith added the comment: Your code basically becomes similar to this: sentinel = object() class FileObject: _uploaded_by: str = None uploaded_by = None def __init__(self, uploaded_by=sentinel): if uploaded_by is sentinel: self.uploaded_by =

[issue39247] dataclass defaults and property don't work together

2020-01-07 Thread Eric V. Smith
Change by Eric V. Smith : -- assignee: -> eric.smith ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe:

[issue39247] dataclass defaults and property don't work together

2020-01-07 Thread Karthikeyan Singaravelan
Change by Karthikeyan Singaravelan : -- nosy: +eric.smith ___ Python tracker ___ ___ Python-bugs-list mailing list Unsubscribe:

[issue39247] dataclass defaults and property don't work together

2020-01-07 Thread Michael Robellard
New submission from Michael Robellard : I ran into a strange issue while trying to use a dataclass together with a property. I have it down to a minumum to reproduce it: import dataclasses @dataclasses.dataclass class FileObject: _uploaded_by: str = dataclasses.field(default=None,