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

Damon Lynch damonlynch at gmail.com
Tue Nov 21 18:11:45 GMT 2017


On Tue, Nov 21, 2017 at 12:47 PM, Cody Scott <cody at perspexis.com> wrote:

> 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!
>>
>>
>>
>>
The documentation you are looking for is here:
https://docs.python.org/3/library/typing.html#typing.Optional

-- 
http://www.damonlynch.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20171121/eec719de/attachment-0001.html>


More information about the PyQt mailing list