[PyQt] PyQt/PyCharm/PEP problem with PyQt types

Cody Scott cody at perspexis.com
Tue Nov 21 17:47:07 GMT 2017

What if you do Union[QWidget, None] ?

On Tue, Nov 21, 2017 at 11:11 AM, J Barchan <jnbarchan at gmail.com> wrote:

> This is my first post to this mailing list.  I am a Python(3)/PyQt/Qt
> newbie, so please bear with me!
> 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.
> I am having problems with parameters/return types which are supposed to be
> PyQt objects but where a valid value is *None* 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.)
> Example of my own function return type:
> def myfunc() -> QWidget:
>     if ...:
>         return mywidget
>     return *None*
> Example of calling PyQt function:
> mywidget.setParent(*None*)
> In both these cases PyCharm highlights the *None* and warns me "Expected
> type 'QWidget', got 'None' instead".  This is to do with "type inspections".
> (There are however *some* cases, I think, where I have looked at a PyQt
> function declaration stub in the .pyi file which *seemed* to me to have
> the same signature yet it did *not* complain about None, although I
> cannot recall which/what any difference was.)
> Wherever I have a Python-object type for either a parameter or a return
> value I expect None 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)?
> Many thanks in advance if I get an answer!
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> https://www.riverbankcomputing.com/mailman/listinfo/pyqt
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20171121/477d24fe/attachment.html>

More information about the PyQt mailing list