<div dir="ltr">Sorry for double-emailing, but I just remembered that PyQt already has at least one example of a repr that switches constructors depending on the object's values:<div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-family:monospace"><span style="color:rgb(0,0,0)">>>> QtCore.QPoint(0, 0)<br>
</span></span><span style="font-family:monospace">PyQt6.QtCore.QPoint()<br>
</span><span style="font-family:monospace">>>> QtCore.QPoint(0, 1)<br>
</span><span style="font-family:monospace">PyQt6.QtCore.QPoint(0, 1)</span></blockquote><div><br></div><div>So it wouldn't be too crazy to do something similar for QColor too, in my opinion.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Apr 4, 2023 at 9:17 PM RoadrunnerWMC <<a href="mailto:roadrunnerwmc@gmail.com">roadrunnerwmc@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">For what it's worth, I'd also like this. The core geometry classes (QPoint, QRect, QLine, etc. and their float versions) have custom reprs, and I've definitely been bitten by wrongly assuming that QColor has one when debugging in the past.<div><br></div><div>That said, it should use the same syntax as those other classes, instead of what QGIS's monkeypatch does:<div><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace">>>> from PyQt6 import QtCore, QtGui, QtWidgets<br>>>> app = QtWidgets.QApplication([])<br>>>><br>>>> QtCore.QPoint(1, 2)<br>PyQt6.QtCore.QPoint(1, 2)<br>>>> QtCore.QRectF(1, 2, 3, 4)<br>PyQt6.QtCore.QRectF(1.0, 2.0, 3.0, 4.0)<br>>>><br>>>> QtGui.QColor(1, 2, 3, 4)</font> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace"><PyQt6.QtGui.QColor object at 0x7fad4120b840></font></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace">>>></font></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace">>>> # Showing *hypothetical* new QColor reprs from here on.</font></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace">>>></font></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace">>>> QtGui.QColor(1, 2, 3, 4)<br>PyQt6.QtGui.QColor(1, 2, 3, 4)<br>>>> # since alpha defaults to 255, perhaps it could be omitted if it has that value:<br>>>> QtGui.QColor(1, 2, 3, 255)<br>PyQt6.QtGui.QColor(1, 2, 3)</font></blockquote><br></div><div>Returning "a string that would yield an object with the same value when passed to eval()" <a href="https://docs.python.org/3/library/functions.html#repr" target="_blank">is good style</a>, so this output format makes sense.</div><div><br></div><div>There's a problem, though: showing the components as 8-bit integers would be misleading, since QColor uses higher precision (16-bit integers) internally:</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace">>>> QtGui.QColor.fromRgbF(0.5, 0.5, 0.5)<br>PyQt6.QtGui.QColor(128, 128, 128)<br>>>> QtGui.QColor.fromRgbF(0.5, 0.5, 0.501)<br>PyQt6.QtGui.QColor(128, 128, 128)<br>>>> QtGui.QColor.fromRgbF(0.5, 0.5, 0.5) == QtGui.QColor.fromRgbF(0.5, 0.5, 0.5)<br>True<br>>>> QtGui.QColor.fromRgbF(0.5, 0.5, 0.5) == QtGui.QColor.fromRgbF(0.5, 0.5, 0.501)<br>False</font></blockquote><div><br></div><div>As a debugging aid, this would arguably be worse than no repr at all, in some cases.</div><div><br></div><div>The float constructor lets you specify more accurate values, but since the components aren't <i>stored</i> as floats, the rounding makes it kind of gross:</div></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace">>>> QtGui.QColor.fromRgbF(0.5, 0.5, 0.501)<br></font><span style="font-family:monospace">PyQt6.</span><font face="monospace">QtGui.QColor.fromRgbF(0.5000076293945312, 0.5000076293945312, 0.5009994506835938)</font><br></blockquote><div><br></div><div>You could use the constructor that takes 16-bit integers:<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace">>>> QtGui.QColor.fromRgbF(0.5, 0.5, 0.501)<br></font><span style="font-family:monospace">PyQt6.</span><font face="monospace">QtGui.QColor.fromRgba64(32768, 32768, 32833)</font></blockquote><div><br></div><div>But that's kind of hard to read at a glance, which is not great given that this is supposed to help with debugging.</div><div><br></div><div>I think what I'd do, personally, is have it dynamically choose which way to repr itself based on whether all the channels match what Qt would choose for 8-bit input values (aka <font face="monospace">channel * 0x101</font>):</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace">>>> QtGui.QColor.fromRgba64(32382, 32639, 32896)<br></font><span style="font-family:monospace">PyQt6.</span><font face="monospace">QtGui.QColor(126, 127, 128)<br>>>> QtGui.QColor.fromRgba64(32382, 32639, 32897)<br></font><span style="font-family:monospace">PyQt6.</span><font face="monospace">QtGui.QColor.fromRgba64(32382, 32639, 32897)</font></blockquote><div><br></div><div>Shrug. Just my two cents, I guess :)<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Apr 4, 2023 at 5:40 PM Nyall Dawson <<a href="mailto:nyall.dawson@gmail.com" target="_blank">nyall.dawson@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi list!<br>
<br>
I'm wondering if it would be possible to add a __repr__ for the QColor<br>
class. PyQt doesn't set one itself, but it's VERY useful for debugging<br>
purposes.<br>
<br>
Downstream we patch one in using this approach:<br>
<a href="https://github.com/qgis/QGIS/blob/574d1ea47b921ff9733f75b577e38034bae74793/python/PyQt/PyQt5/QtGui.py#L27" rel="noreferrer" target="_blank">https://github.com/qgis/QGIS/blob/574d1ea47b921ff9733f75b577e38034bae74793/python/PyQt/PyQt5/QtGui.py#L27</a><br>
<br>
Would you consider adding a similar repr to PyQt itself?<br>
<br>
Many thanks!<br>
Nyall<br>
</blockquote></div>
</blockquote></div>