<div dir="ltr"><div class="gmail_default" style="font-size:small">On Thu, Dec 21, 2017 at 7:55 PM, Terry Reedy <span dir="ltr"><<a href="mailto:tjreedy@udel.edu" target="_blank">tjreedy@udel.edu</a>></span> wrote:<br></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 12/21/2017 9:23 AM, Eric V. Smith wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<br>
On 12/21/17 6:25 AM, Sven R. Kunze wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On 21.12.2017 11:22, Terry Reedy wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
<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">
@dataclass<br>
class C:<br>
     a: int # integer field with no default<br>
     b: float = 0.0 # float field with a default<br>
<br>
And the types will be recognized by type checkers such as mypy.<br>
<br>
And I think the non-typed examples should go first in the docs.<br>
</blockquote></blockquote>
<br>
</blockquote>
<br>
I still don't understand why "I don't care" can be defined by "leaving out"<br>
<br>
@dataclass<br>
class C:<br>
     b = 0.0 # float field with a default<br>
</blockquote>
<br>
Because you can't know the order that x and y are defined in this example:<br>
<br>
class C:<br>
     x: int<br>
     y = 0<br>
<br>
'x' is not in C.__dict__, and 'y' is not in C.__annotations__.<br>
</blockquote>
<br></span></blockquote><div> </div><div><div class="gmail_default" style="font-size:small;display:inline">​Solely because, annotations being optional, the interpreter is not allowed to infer from its presence  that an annotated name should be ​allocated an entry in __dict__, and clearly the value associated with it would be problematical.</div></div><div><div class="gmail_default" style="font-size:small;display:inline"><br></div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class=""></span>
I think the understanding problem with this feature arises from two factors: using annotations to define possibly un-initialized slots is non-obvious; a new use of annotations for something other than static typing is a bit of a reversal of the recent pronouncement 'annotations should only be used for static typing'.  Therefore, getting the permanent doc 'right' is important.<br></blockquote><div><br></div><div class="gmail_default" style="font-size:small">​Indeed. So annotations are optional, except where they aren't?​</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The following naively plausible alternative does not work and cannot sensibly be made to work because the bare 'x' in the class scope, as opposed to a similar error within a method, causes NameError before the class is created.<br>
<br>
@dataclass<br>
class C:<br>
    x<br>
    y = 0<br>
<br></blockquote><div><div class="gmail_default" style="font-size:small;display:inline">​Quite. Could this be handled the same way not-yet initilialised slots are? (Pardon my ignornace).</div></div><div><div class="gmail_default" style="font-size:small;display:inline">​</div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I think the doc should explicitly say that uninitialized fields require annotation with something (anything, not necessarily a type) simply to avoid NameError during class creation.  It may not be obvious to some readers why x:'anything' does not also raise NameError, but that was a different PEP, and the dataclass doc could here link to wherever name:annotation in bodies is explained.<span class="HOEnZb"><font color="#888888"><br>
<br></font></span></blockquote><div><br></div><div><div class="gmail_default" style="font-size:small">​This contortion is why I feel a better solution would be desirable. Alas I do not have one to hand.</div><div class="gmail_default" style="font-size:small"><br></div><div class="gmail_default" style="font-size:small">regards</div><div class="gmail_default" style="font-size:small"> Steve​</div><br></div></div></div></div>