[PyQt] PyQt/PyCharm/PEP problem with PyQt types
me at the-compiler.org
Wed Nov 22 10:03:05 GMT 2017
On Wed, Nov 22, 2017 at 09:38:21AM +0000, J Barchan 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
As mentioned in an earlier reply, there's Optional[QWidget] which is a
shorthand for that.
> 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?
Yes, and it's probably the more correct way, because the caller code *has* to
deal with the value being None.
> 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?
Yes, PyQt should use Optional[QWidget] here. However, there's probably no way
for PyQt to know where None is acceptable and where it isn't...
FWIW last time I looked at PyQt's type definitions, they were mostly intended
for IDE documentation, and not for typechecking - and mypy (probably the most
widely used type checking tool) didn't like the files at all.
https://www.qutebrowser.org | me at the-compiler.org (Mail/XMPP)
GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
I love long mails! | https://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 833 bytes
Desc: not available
More information about the PyQt