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

J Barchan jnbarchan at gmail.com
Wed Nov 22 10:02:53 GMT 2017


P.S.
To be specific, it seems to me that if Python does not accept None (without
warning) for, say, a C++ QWidget* parameter, which will of course accept
NULL/0, then it is the wrong Python type for the C++ parameter.  The Python
signature of:

    def setParent(self, parent: QWidget)

actually corresponds only to the C++ declaration of:

    setParent(QWidget &)

where the & denotes "reference" not "pointer".  And that is not the way
that function is declared in Qt.  If PyQt wants to support * (pointer)
correctly, it needs all such parameters declared as Union[QWidget, None],
no?

On 22 November 2017 at 09:38, J Barchan <jnbarchan at gmail.com> wrote:

> Thank you both, for the same solution.  I had noticed the Union[QWidget,
> None] construct and wondered if that was the only way/what I'm supposed
> to do in Python.  But it raises two questions, *and only solves one of
> the two problems*:
>
> 1. I am "surprised" at the need to do this from Python every time an
> object might have value None.  It's "cumbersome" compared to the way
> NULL/0 is handled from C++, where it's always an acceptable value for any
> Object* parameter/return value.  Do you guys really write that for every
> place None is acceptable?
>
> 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.
>
> 2. The problem is unsolved where a *PyQt* function accepts/returns None.
> The second example I gave is calling QWidget.setParent(None).  The
> declaration of this in QtWidgets.pyi is:
>
>     def setParent(self, parent: 'QWidget') -> None:
>
> Following your solutions, this *ought* to have been:
>
>     def setParent(self, parent: typing.Union['QWidget', None]) -> None:
>
> But the problem is that it *isn't* defined like that in the .pyi, and I
> can't help that.  And it's not just that function, it's lots of others too
> which accept or return None, 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 None, do you?
>
>
>
> On 21 November 2017 at 18:11, Damon Lynch <damonlynch at gmail.com> wrote:
>
>>
>>
>> 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
>>
>
>
>
> --
> Kindest,
> Jonathan
>



-- 
Kindest,
Jonathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20171122/77b560d9/attachment-0001.html>


More information about the PyQt mailing list