<div dir="ltr">Sorry about the email mangling -- I do a lot of my listserve work on the bus on an iPhone, with the built -in mail client -- and it REALLY sucks for doing interspersed email replying -- highly encouraging the dreaded top posting...<div><br></div><div>But anyway, I think both Steve and I were expressing concerns about "Typing Creep". Typing should always be optional in Python, and while this PEP does keep it optional, Steve's point was that the code in the standard library serves not only as a library, but as examples of how to write "robust" python code.</div><div><br></div><div>The rest of this note is me -- I'm not pretending ot speak for Steve.</div><div><br></div><div>Reading the PEP, this text makes me uneasy:</div><div><br></div><div><span style="color:rgb(68,68,68);font-family:SourceSansProRegular,Arial,sans-serif;font-size:15px;background-color:rgb(249,249,249)">"A </span><tt class="gmail-docutils gmail-literal" style="box-sizing:border-box;color:rgb(68,68,68);background-color:rgb(249,249,249)">field</tt><span style="color:rgb(68,68,68);font-family:SourceSansProRegular,Arial,sans-serif;font-size:15px;background-color:rgb(249,249,249)"> is defined as any variable identified in</span><tt class="gmail-docutils gmail-literal" style="box-sizing:border-box;color:rgb(68,68,68);background-color:rgb(249,249,249)">__annotations__</tt><span style="color:rgb(68,68,68);font-family:SourceSansProRegular,Arial,sans-serif;font-size:15px;background-color:rgb(249,249,249)">. That is, a variable that has a type annotation."</span><br></div><div><span style="color:rgb(68,68,68);font-family:SourceSansProRegular,Arial,sans-serif;font-size:15px;background-color:rgb(249,249,249)"><br></span></div><div><span style="color:rgb(68,68,68);font-family:SourceSansProRegular,Arial,sans-serif;font-size:15px;background-color:rgb(249,249,249)">And if I understand the rest of the PEP, while typing itself is optional, the use of type Annotation is not -- it is exactly what's being used to generate the fields the user wants.</span></div><div><span style="color:rgb(68,68,68);font-family:SourceSansProRegular,Arial,sans-serif;font-size:15px;background-color:rgb(249,249,249)"><br></span></div><div><span style="color:rgb(68,68,68);font-family:SourceSansProRegular,Arial,sans-serif;font-size:15px;background-color:rgb(249,249,249)">And the examples are all using typing -- granted, primarily the built in types, but still:</span></div><div><br></div><br><font face="monospace, monospace">@dataclass<br>class C:<br> a: int # 'a' has no default value<br> b: int = 0 # assign a default value for 'b'<br></font><br><div><span style="color:rgb(68,68,68);font-family:SourceSansProRegular,Arial,sans-serif;font-size:15px;background-color:rgb(249,249,249)"><br></span></div><div><span style="background-color:rgb(249,249,249)"><font color="#444444" face="SourceSansProRegular, Arial, sans-serif"><span style="font-size:15px">This sure LOOKS like typing is required. It also makes me nervous because, as I understand it, the types aren't actually used in the implementation (presumable they would be by mypy and the like?). So I think for folks that aren't using typing and a type checker in their development process, it would be pretty confusing that this means and what it actually does. Particularly folks that are coming from a background of a statically typed language.</span></font></span></div><div><span style="background-color:rgb(249,249,249)"><font color="#444444" face="SourceSansProRegular, Arial, sans-serif"><span style="font-size:15px"><br></span></font></span></div><div><span style="background-color:rgb(249,249,249)"><font color="#444444" face="SourceSansProRegular, Arial, sans-serif"><span style="font-size:15px">Then I see:</span></font></span></div><br>"""<br>Field objects describe each defined field.<div>...</div><div>Its documented attributes are:<br><br>name: The name of the field.<br>type: The type of the field.<br>...<br>"""<div><br></div><div>So again, typing looks to be pretty baked in to the whole concept.</div><div><br></div><div>and then:</div><br>"""<br>One place where dataclass actually inspects the type of a field is to determine if a field is a class variable as defined in PEP 526. <br>"""<div><br></div><div>and</div><div><br></div><div>"""<br>The other place where dataclass inspects a type annotation is to determine if a field is an init-only variable. It does this by seeing if the type of a field is of type dataclasses.InitVar.<br>"""</div><br>"""<br>Data Classes will raise a TypeError if it detects a default parameter of type list, dict, or set.<br>"""<div><br></div><div>So: it seems that type hinting, while not required to use Data Classes, is very much baked into the implementation an examples.</div><div><br></div><div>As I said -- this makes me uneasy -- It's a very big step that essentially promotes the type hinting to a new place in Python -- you will not be able to use a standard library class without at least a little thought about types and typing.</div><div><br></div><div>I note this:</div><div><br></div><div>"""<br>This discussion started on python-ideas [9] and was moved to a GitHub repo [10] for further discussion. As part of this discussion, we made the decision to use PEP 526 syntax to drive the discovery of fields.<br>"""</div><div><br></div><div>I confess I only vaguely followed that discussion -- in fact, mostly I thought that the concept of Data Classes was a good one, and was glad to see SOMETHING get implemented, and didn't think I had much to contribute to the details of how it was done. So these issues may have already been raised and considered, so carry on.</div><div><br></div><div>But:</div><div><br></div>NOTE: from PEP 526:</div><div><br></div><div>"Python will remain a dynamically typed language, and the authors have no desire to ever make type hints mandatory, even by convention. "<div><br></div><div>The Data Classes implementation is not making it mandatory by any means, but it is making it a more "standard" part of the language that can not simply be ignored anymore. And it seems some features of dataclasses can only be accessed via actual typing, in addition to the requirement of type annotations.</div><div><br></div><div>If nothing else, the documentation should make it very clear that the typing aspects of Data Classes is indeed optional, and preferably give some untyped examples, something like:</div><div><br></div><div><span style="font-family:monospace,monospace">@dataclass</span><br style="font-family:monospace,monospace"><span style="font-family:monospace,monospace">class C:</span><br style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"> a: None # 'a' has no default value</span><br style="font-family:monospace,monospace"><span style="font-family:monospace,monospace"> b: None = 0 # assign a default value for 'b'</span><br></div><div><br></div><div><br></div><div>If, in fact, that would be the way to do it.</div><div><br></div><div>-Chris</div><div><br></div><div><br></div><div><br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Dec 15, 2017 at 3:22 AM, 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">On 12/15/2017 5:56 AM, Steve Holden wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Mon, Dec 11, 2017 at 5:10 PM, Chris Barker - NOAA Federal <<a href="mailto:chris.barker@noaa.gov" target="_blank">chris.barker@noaa.gov</a> <mailto:<a href="mailto:chris.barker@noaa.gov" target="_blank">chris.barker@noaa.gov</a>><wbr>> wrote:<br>
</blockquote>
...<span class=""><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">
However, typing is not currently imported by dataclasses.py.<br>
</blockquote></blockquote>
><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
And there you have an actual reason besides my uneasiness :-)<br>
<br>
- CHB<br>
<br>
hmm...<br>
</blockquote>
<br></span>
[Agreed with Antoine on the MUA and quoting being confusing.]<br>
<br>
The only reason typing isn't imported is performance. I hope that once PEP 560 is complete this will no longer be an issue, and dataclasses will always import typing. But of course typing will still not be needed for most uses of @dataclass or make_dataclass(). This is explained in the PEP.<span class="HOEnZb"><font color="#888888"><br>
<br>
Eric.<br>
<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><br>Christopher Barker, Ph.D.<br>Oceanographer<br><br>Emergency Response Division<br>NOAA/NOS/OR&R (206) 526-6959 voice<br>7600 Sand Point Way NE (206) 526-6329 fax<br>Seattle, WA 98115 (206) 526-6317 main reception<br><br><a href="mailto:Chris.Barker@noaa.gov" target="_blank">Chris.Barker@noaa.gov</a></div>
</div>