<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Dec 8, 2017 at 3:44 PM, Eric V. Smith <span dir="ltr"><<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 12/8/2017 1:28 PM, Raymond Hettinger wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Dec 7, 2017, at 12:47 PM, Eric V. Smith <<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>> wrote:<br>
<br>
On 12/7/17 3:27 PM, Raymond Hettinger wrote:<br>
...<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I'm looking for guidance or workarounds for two issues that have arisen.<br>
<br>
First, the use of default values seems to completely preclude the use of __slots__.  For example, this raises a ValueError:<br>
<br>
    class A:<br>
        __slots__ = ['x', 'y']<br>
        x: int = 10<br>
        y: int = 20<br>
</blockquote>
<br>
Hmm, I wasn't aware of that. I'm not sure I understand why that's an error. Maybe it could be fixed?<br>
</blockquote>
<br>
The way __slots__ works is that the type() metaclass automatically assigns member-objects to the class variables 'x' and 'y'.  Member objects are descriptors that do the actual lookup.<br>
<br>
So, I don't think the language limitation can be "fixed".  Essentially, we're wanting to use the class variables 'x' and 'y' to hold both member objects and a default value.<br>
</blockquote>
<br></span>
Thanks. I figured this out after doing some research. Here's a thread "__slots__ and default values" from 14+ years ago from some guy named Hettinger:<br>
<a href="https://mail.python.org/pipermail/python-dev/2003-May/035575.html" rel="noreferrer" target="_blank">https://mail.python.org/piperm<wbr>ail/python-dev/2003-May/035575<wbr>.html</a><br>
<br>
As to whether we add slots=True to @dataclasses, I'll let Guido decide.<br>
<br>
The code already exists as a separate decorator here: <a href="https://github.com/ericvsmith/dataclasses/blob/master/dataclass_tools.py#L3" rel="noreferrer" target="_blank">https://github.com/ericvsmith/<wbr>dataclasses/blob/master/datacl<wbr>ass_tools.py#L3</a>, if you want to play with it.<br>
<br>
Usage:<br>
<br>
>>> @add_slots<br>
... @dataclass<br>
... class A:<br>
...     x: int = 10<br>
...     y: int = 20<br>
...<br>
>>> a = A()<br>
>>> a<br>
A(x=10, y=20)<br>
>>> a.x = 15<br>
>>> a<br>
A(x=15, y=20)<br>
>>> a.z = 30<span class=""><br>
Traceback (most recent call last):<br>
  File "<stdin>", line 1, in <module><br></span>
AttributeError: 'A' object has no attribute 'z'<br>
<br>
Folding it in to @dataclass is easy enough. On the other hand, since it just uses the dataclasses public API, it's not strictly required to be in @dataclass.<span class=""><br></span></blockquote><div><br></div><div>Let's do it. For most people the new class is an uninteresting implementation detail; for the rest we can document clearly that it is special.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The second issue is that the different annotations give different signatures than would produced for manually written classes.  It is unclear what the best practice is for where to put the annotations and their associated docstrings.<br>
</blockquote>
<br>
I don't have any suggestions here.<br>
</blockquote>
<br>
I'm hoping the typing experts will chime in here.  The question is straight-forward.  Where should we look for the signature and docstring for constructing instances?  Should they be attached to the class, to __init__(), or to __new__() when it used.<br>
<br>
It would be nice to have an official position on that before, it gets set in stone through arbitrary choices made by pycharm, pydoc, mypy, typing.NamedTuple, and dataclasses.dataclass.<br>
</blockquote>
<br></span>
I'm not sure I see why this would relate specifically to typing, since I don't think they'd inspect docstrings. But yes, it would be good to come to an agreement.<span class="HOEnZb"></span><br clear="all"></blockquote></div></div><div class="gmail_extra"><br></div><div class="gmail_extra">I don't recall in detail what all these tools and classes do with docstrings. Maybe if someone summarizes the status quo and explains how PEP 557 changes that it will be simple to decide.<br></div><div class="gmail_extra"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div></div>