<div dir="ltr"><div class="gmail_default" style="font-family:tahoma,sans-serif">Thank you both, for the same solution. I had noticed the <span style="font-family:monospace,monospace">Union[QWidget, None]</span> construct and wondered if that was the only way/what I'm supposed to do in Python. But it raises two questions, <i>and only solves one of the two problems</i>:</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">1. I am "surprised" at the need to do this from Python every time an object might have value <span style="font-family:monospace,monospace">None</span>. It's "cumbersome" compared to the way <span style="font-family:monospace,monospace">NULL/0</span> is handled from C++, where it's always an acceptable value for any <span style="font-family:monospace,monospace">Object*</span> parameter/return value. Do you guys really write that for every place <span style="font-family:monospace,monospace">None</span> is acceptable?</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">However, even if I find it "ugly", I agree I can use that for my own functions, e.g. a return value. I can use this for the first of my examples, only.<br></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif">2. The problem is unsolved where a <i>PyQt</i> function accepts/returns <span style="font-family:monospace,monospace">None</span>. The second example I gave is calling <span style="font-family:monospace,monospace">QWidget.setParent(None)</span>. The declaration of this in <span style="font-family:monospace,monospace">QtWidgets.pyi</span> is:</div><div class="gmail_default" style="font-family:tahoma,sans-serif"><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:9pt"><span style="color:rgb(0,0,128);font-weight:bold"> def </span>setParent(<span style="color:rgb(148,85,141)">self</span>, parent: <span style="color:rgb(0,128,128);font-weight:bold">'QWidget'</span>) -> <span style="color:rgb(0,0,128);font-weight:bold">None</span>: </pre></div><div class="gmail_default" style="font-family:tahoma,sans-serif"></div><div class="gmail_default" style="font-family:tahoma,sans-serif">Following your solutions, this <i>ought</i> to have been:</div><div class="gmail_default" style="font-family:tahoma,sans-serif"></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:"DejaVu Sans Mono";font-size:9pt"><span style="color:rgb(0,0,128);font-weight:bold"> def </span>setParent(<span style="color:rgb(148,85,141)">self</span>, parent: typing.Union[<span style="color:rgb(0,128,128);font-weight:bold">'QWidget'</span>, None]) -> <span style="color:rgb(0,0,128);font-weight:bold">None</span>:</pre></div><div class="gmail_default" style="font-family:tahoma,sans-serif">But the problem is that it <i>isn't</i> defined like that in the <span style="font-family:monospace,monospace">.pyi</span>, and I can't help that. And it's not just that function, it's lots of others too which accept or return <span style="font-family:monospace,monospace">None</span>, so this issue keeps arising in my calling code. Was it PyQt's job to recognise this and generate a different declaration or what? Given where we are, I don't see any way of avoiding the warning when calling such a PyQt function with <span style="font-family:monospace,monospace">None</span>, do you?<br></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div><div class="gmail_default" style="font-family:tahoma,sans-serif"><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On 21 November 2017 at 18:11, Damon Lynch <span dir="ltr"><<a href="mailto:damonlynch@gmail.com" target="_blank">damonlynch@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote"><span class="">On Tue, Nov 21, 2017 at 12:47 PM, Cody Scott <span dir="ltr"><<a href="mailto:cody@perspexis.com" target="_blank">cody@perspexis.com</a>></span> wrote:<br><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">What if you do Union[QWidget, None] ?<br></div><div class="gmail_extra"><br><div class="gmail_quote"><div><div class="m_-5849286311674569537gmail-h5">On Tue, Nov 21, 2017 at 11:11 AM, J Barchan <span dir="ltr"><<a href="mailto:jnbarchan@gmail.com" target="_blank">jnbarchan@gmail.com</a>></span> wrote:<br></div></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div class="m_-5849286311674569537gmail-h5"><div dir="ltr"><div style="font-family:tahoma,sans-serif">This is my first post to this mailing list. I am a Python(3)/PyQt/Qt newbie, so please bear with me!</div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">I use PyCharm to do my PyQt work. The editor is very good at "highlighting dubious constructs", often relating to (what I understand to be) external "PEP" warnings. I "annotate" my functions with the types of parameters & return values, to check for warnings and to help with code completion suggestions.</div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">I am having problems with parameters/return types which are supposed to be PyQt objects but where a valid value is <b>None</b> in Python (corresponding to 0/NULL in C++). (This is perhaps a general Python problem maybe not specific to PyQt/Qt, I don't know, but it's a problem in the context of PyQt.)<br></div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">Example of my own function return type:</div><div style="font-family:tahoma,sans-serif"><br></div><div><span style="font-family:monospace,monospace">def myfunc() -> QWidget:</span></div><div><span style="font-family:monospace,monospace"> if ...:</span></div><div><span style="font-family:monospace,monospace"> return mywidget</span></div><div><span style="font-family:monospace,monospace"> return <b>None</b></span></div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">Example of calling PyQt function:</div><div style="font-family:tahoma,sans-serif"><br></div><div><span style="font-family:monospace,monospace">mywidget.setParent(<b>None</b>)</span></div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">In both these cases PyCharm highlights the <b><span style="font-family:monospace,monospace">None</span></b> and warns me "Expected type 'QWidget', got 'None' instead". This is to do with "type inspections".<br></div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">(There are however <i>some</i> cases, I think, where I have looked at a PyQt function declaration stub in the <span style="font-family:monospace,monospace">.pyi</span> file which <i>seemed</i> to me to have the same signature yet it did <i>not</i> complain about <span style="font-family:monospace,monospace">None</span>, although I cannot recall which/what any difference was.)</div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">Wherever I have a Python-object type for either a parameter or a return value I expect <span style="font-family:monospace,monospace">None</span> to be acceptable. What is going on here, what do I have to do to my PyCharm/PyQt code acceptable without this warning (and, no, I don't want to turn all "type inspection" warnings off)?</div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif">Many thanks in advance if I get an answer!</div><div style="font-family:tahoma,sans-serif"><br></div><div style="font-family:tahoma,sans-serif"><br></div><br clear="all"></div></div></div></blockquote></div></div></blockquote><div><br></div></span><div>The documentation you are looking for is here: <a href="https://docs.python.org/3/library/typing.html#typing.Optional" target="_blank">https://docs.python.org/3/<wbr>library/typing.html#typing.<wbr>Optional</a> <br></div></div><span class="HOEnZb"><font color="#888888"><br>-- <br><div class="m_-5849286311674569537gmail_signature"><a href="http://www.damonlynch.net" target="_blank">http://www.damonlynch.net</a><div></div><div></div><div></div></div>
</font></span></div></div>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><span style="font-family:tahoma,sans-serif">Kindest,</span></div><div><span style="font-family:tahoma,sans-serif">Jonathan</span></div></div></div></div></div>
</div>