Ivan Ivanyuk <ivan.ivan...@gmail.com> 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 
wondering if it was possible to make it work without changing the property 
decorator behaviour. Is there a way at all to preserve the default value on the 
class with @property even before dataclass starts processing it? 

 An example from that mail thread to workaround this:

 from dataclasses import dataclass, field
 
 def set_property():
     Container.x = property(Container.get_x, Container.set_x)
     return 30
 
 @dataclass
 class Container:
     x: int = field(default_factory=set_property)
 
     def get_x(self) -> int:
         return self._x
 
     def set_x(self, z: int):
         if z > 1:
             self._x = z
         else:
             raise ValueError

set_property can also be made a class method and referenced like this:
 x: int = field(default_factory=lambda: Container.set_property())

Is it possible that this kind of behaviour can be made one of standard flows 
for the field() function and dataclasses module can generate a function like 
this and set it on the class during processing?
 Or maybe it's better to extend @property decorator to update property object 
with default value which can be used later by the dataclass?

----------
nosy: +iivanyuk

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39247>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to