<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">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="gmail-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="gmail-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><div>The documentation you are looking for is here: <a href="https://docs.python.org/3/library/typing.html#typing.Optional">https://docs.python.org/3/library/typing.html#typing.Optional</a> <br></div></div><br>-- <br><div class="gmail_signature"><a href="http://www.damonlynch.net" target="_blank">http://www.damonlynch.net</a><div></div><div></div><div></div></div>
</div></div>