<div dir="ltr">I'm normally no big fan of things that take either a class or an instance, but since fields() does this, I think is_dataclass() should to. And that's the name I'd choose. OK on the pseudo-fields.<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Jan 5, 2018 at 11:06 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"><span class="">On 1/5/2018 12:58 PM, Guido van Rossum wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hm. I don't know that people will conclude that checking for a dataclass is an anti-pattern. They'll probably just invent a myriad of different hacks like the one you showed. I recommend making it public.<br>
</blockquote>
<br></span>
I'm trying to track down the original discussion. We got bogged down on whether it worked for classes or instances or both, then we got tied up in naming it (surprise!), then it looks like we decided to just not include it since you could make those decisions for yourself.<br>
<br>
I think the discussion is buried in this thread:<br>
<a href="https://mail.python.org/pipermail/python-dev/2017-November/150966.html" rel="noreferrer" target="_blank">https://mail.python.org/piperm<wbr>ail/python-dev/2017-November/<wbr>150966.html</a><br>
<br>
Which references:<br>
<a href="https://github.com/ericvsmith/dataclasses/issues/99" rel="noreferrer" target="_blank">https://github.com/ericvsmith/<wbr>dataclasses/issues/99</a><br>
<br>
So, ignoring the naming issue, I think if we want to revive it, the question is: should isdataclass() return True on just instances, just classes, or both? And should it ever raise an exception, or just return False?<span class=""><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I still worry a bit about ClassVar and InitVar being potentially useful but I concede I have no use case so I'll drop it.<br>
</blockquote>
<br></span>
IIRC, we decided that we could add a parameter to dataclasses.fields() if we ever wanted to return pseudo-fields. But no one came up with a use case.<br>
<br>
Eric.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
<br>
On Fri, Jan 5, 2018 at 8:43 AM, Eric V. Smith <<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a> <mailto:<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>>> wrote:<br>
<br>
On 1/5/2018 11:24 AM, Guido van Rossum wrote:<br>
<br>
On Fri, Jan 5, 2018 at 5:08 AM, Eric V. Smith<br>
<<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a> <mailto:<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>><br></span><div><div class="h5">
<mailto:<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a> <mailto:<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>>>> wrote:<br>
<br>
On 1/2/2018 12:01 AM, Guido van Rossum wrote:<br>
<br>
Yes, there's a class variable (__dataclass_fields__) that<br>
identifies the parent fields. The PEP doesn't mention<br>
this or<br>
the fact that special methods (like __repr__ and<br>
__init__) can<br>
tell whether a base class is a dataclass. It probably<br>
should<br>
though. (@Eric)<br>
<br>
<br>
I think that's covered in this section:<br>
<a href="https://www.python.org/dev/peps/pep-0557/#inheritance" rel="noreferrer" target="_blank">https://www.python.org/dev/pep<wbr>s/pep-0557/#inheritance</a><br>
<<a href="https://www.python.org/dev/peps/pep-0557/#inheritance" rel="noreferrer" target="_blank">https://www.python.org/dev/pe<wbr>ps/pep-0557/#inheritance</a>><br>
<<a href="https://www.python.org/dev/peps/pep-0557/#inheritance" rel="noreferrer" target="_blank">https://www.python.org/dev/pe<wbr>ps/pep-0557/#inheritance</a><br>
<<a href="https://www.python.org/dev/peps/pep-0557/#inheritance" rel="noreferrer" target="_blank">https://www.python.org/dev/pe<wbr>ps/pep-0557/#inheritance</a>>><br>
<br>
<br>
I was specifically talking about the name and contents of<br>
__dataclass_fields__, which are not documented by the PEP. I<br>
expect it's inevitable that people will be looking at this<br>
(since they can see it in the source code). Or do you recommend<br>
that people use dataclasses.fields() and catch ValueError?<br>
<br>
<br>
The expectation is to use dataclasses.fields(). Both it and<br>
__dataclass_fields__ contain the fields for this class and the<br>
parents. The only difference is the pseudo-fields.<br>
<br>
I can add some words describing .fields() returning which fields are<br>
present.<br>
<br>
I notice that _isdataclass() exists but is private and I don't<br>
recall why.<br>
<br>
<br>
I think the argument was that it's an anti-pattern, and if you<br>
really want to know, just call dataclasses.fields() and catch the<br>
TypeError. I have this in a helper file:<br>
<br>
def isdataclass(obj):<br>
"""Returns True for dataclass classes and instances."""<br>
try:<br>
dataclasses.fields(obj)<br>
return True<br>
except TypeError:<br>
return False<br>
<br>
<br>
(Also now I'm curious what<br>
<br>
the "pseudo-fields" are that fields() ignores, but that's OT.)<br>
<br>
<br>
ClassVar and InitVar "fields". dataclasses.fields() doesn't return them.<br>
<br>
Eric.<br>
<br>
______________________________<wbr>_________________<br>
Python-Dev mailing list<br></div></div>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a> <mailto:<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a>><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-dev</a><br>
<<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailm<wbr>an/listinfo/python-dev</a>><br>
Unsubscribe:<br>
<a href="https://mail.python.org/mailman/options/python-dev/guido%40python.org" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/options/python-dev/guido%<wbr>40python.org</a> <<a href="https://mail.python.org/mailman/options/python-dev/guido%40python.org" rel="noreferrer" target="_blank">https://mail.python.org/mailm<wbr>an/options/python-dev/guido%<wbr>40python.org</a>><br>
<br>
<br>
<br>
<br>
-- <br>
--Guido van Rossum (<a href="http://python.org/~guido" rel="noreferrer" target="_blank">python.org/~guido</a> <<a href="http://python.org/%7Eguido" rel="noreferrer" target="_blank">http://python.org/%7Eguido</a>>)<span class=""><br>
<br>
<br>
______________________________<wbr>_________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-dev</a><br></span>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/eric%2Ba-python-dev%40trueblade.com" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/options/python-dev/eric%2Ba-<wbr>python-dev%40trueblade.com</a><br>
<br>
</blockquote>
<br>
</blockquote></div><br><br clear="all"><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>