<div dir="ltr">On Wed, Dec 20, 2017 at 5:29 PM, Eric V. Smith <span dir="ltr"><<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">There is definitely a passive bias towards using types with dataclasses in that the Eric (the author) doesn't appear to want an example without them in the pep/docs.<br>
<span class="gmail-"></span><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
I'm not sure what such an example would look like. Do you mean without annotations? </blockquote></span></blockquote><div><br></div><div>IIUC, there is not way to make a dataclass without annotations, yes? That it, using annotations to determine the fields is the one and only way the decorator works. So it's impossible to give an example without annotations, yes?<br></div><div>Â </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="gmail-"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Or do you mean without specifying the "correct" type, like:<br>
<br>
@dataclass<br>
class C:<br>
   x: int = 'hello world'<br></blockquote></span></blockquote><div><br></div><div>It may be a good idea to have an example like that in the docs (but probably not the PEP) to make it clear that the type is not used in any way at run time.<br><br></div><div>But I don't think that anyone is suggesting that would be a recommended practice.<br><br></div><div>I suggest that it be clear in the docs, and ideally in the PEP, that the dataclass decorator is using the *annotation" syntax, and that the the only relevant part it uses is that an annotation exists, but the value of the annotation is essentially (completely?) ignored. So we should have examples like:<br><br></div><div>@dataclass<br></div><div>class C:<br></div><div>   a: ... # field with no default<br></div><div>   b: ... = 0 # filed with a default value<br><br></div><div>Then maybe:<br><br></div><div>@dataclass<br></div><div>class C:<br></div><div>   a: "the a parameter" # field with no default<br></div><div>   b: "another, different parameter" = 0.0 # field with a default<br><br></div><div>Then the docs can go to say that if the user wants to specify a type for use with a static type checking pre-processor, they can do it like so:<br><br><div>@dataclass<br></div><div>class C:<br></div><div>   a: int # integer field with no default<br></div>   b: float = 0.0 # float field with a default<br><br></div><div>And the types will be recognized by type checkers such as mypy.<br></div><div><br></div><div>And I think the non-typed examples should go first in the docs.<br></div><div><br></div><div>This is completely analogous to how all the other parts of python are taught. Would anyone suggest that the very first example of a function definition that a newbie sees would be:<br><br></div><div>def func(a: int, b:float = 0.0):<br></div><div>   body_of_function<br></div><div><br></div><div>Then, _maybe_ way down on the page, you mention that oh, by the way, those types are completely ignored by Python. And not even give any examples without types?<br><br></div><br><span class="gmail-"></span>>Â
Re-reading my post you referenced, is it just an example using typing.Any?<br><br></div><div class="gmail_quote">I actually think that is exactly the wrong point -- typing.Any is still using type hinting -- it's an explicit way to say, "any type will do", but it's only relevant if you are using a type checker. We really need examples for folks that don't know or care about type hinting at all.<br><br></div><div class="gmail_quote">typing.Any is for use by people that are explicitly adding type hinting, and should be discussed in type hinting documentation.<br><br>>Â I'm okay with that in the docs, I just didn't want to focus on it in the PEP. I want the PEP to only <br>> have the one reference to typing, for typing.ClassVar. I figure the people reading the PEP can <br>> extrapolate to all of the possible uses for annotations that they don't need to see a typing.Any <br>> example.<br><br></div><div class="gmail_quote">no they don't, but they DO need to see examples without type hints at all.<br><br></div><div class="gmail_quote">-Chris<br></div><br>-- <br><div class="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></div>