PyQt Type hinting

Answered

Let me first say I love PyCharm, and your support for open source.

I'm just a bit stuck on type hinting with PyQt. Take a look at the image below, when I type "b." I would expect it to list the methods of QPixmap, instead it lists the QPixmap class and its super class. Pressing enter just inserts QPixmap. Surely I must be doing something wrong?

 

 

9 comments

Hi Philip! Looks like a bug. I created a corresponding ticket in our bug tracker: PY-26329.

0

Far be it for me, but although Pavel knows much better than I do I'm not sure where the bug is, or at least read on.

I got your behaviour with `:rtype: PyQt5.QtGui.QPixmap`.  However, with:

```

 from PyQt5 import QtGui

:rtype: QtGui.QPixmap

```

I get correct completions offered!

Also if I use:

```

 from PyQt5 import QtGui

def a() -> QtGui.QPixmap:

```

completions work, which is how I do type annotations.

There seems to be something about need the `import PyQt5` statement to keep PyCharm happy here.  Also in the `:rtype:` statement PyCharm squiggles just the `Pixmap` part of the word, which is wrong (but not in the ` -> ` type declaration).

0

```

 from PyQt5 import QtGui

:rtype: QtGui.QPixmap

```

is ok, but if you're not using QtGui in the module (except for documentation) the import seems superfluous.

def a() -> PyQt5.QtGui.QPixmap:

would be my preferred way, but the project lead doesn't like it, so unfortunately my hands are tied!

0

@Philip Ridout

For me `def a() -> PyQt5.QtGui.QPixmap:` does not work.  I *have to* do an `from PyQt5 import QtGui` statement and then use `-> QtGui.QPixmap`.  Might be something to do with my code, dunno.

" [ -> ] but the project lead doesn't like it"  What project lead?  In PyQt5 (at least) all the `pyi` files I see use that style anyway....

0

Sorry, by 'The project lead' I mean the person who is managing the project.

0

Which project??!!  PyQt or PyCharm?

0

The open source project I contribute. OpenLP.org

0

OIC!  I misunderstood, I get it  now.  It's your team leader who's imposing the rules.

Well, as I said above I found a way which gives you the right completions for either `:rtype:` or `->` type annotations, without waiting for "bug fix".

 

And BTW, a superfluous `import PyQt5....` shouldn't make that much difference, given that your project is using PyQt elsewhere and imports are cached.  Up to you if you want it to work now :)

0

Please sign in to leave a comment.