Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make PySide2 stubs work when loaded through Qt.py #1318

Closed
fredrikaverpil opened this issue May 3, 2019 · 18 comments
Closed

Make PySide2 stubs work when loaded through Qt.py #1318

fredrikaverpil opened this issue May 3, 2019 · 18 comments

Comments

@fredrikaverpil
Copy link

With PyCharm, all you have to do is to pip install pyside2 qt.py into your venv, and autocompletion/intellisense will work thanks to the official PySide2 stubs:

pycharm

This is what happens with jedi in vscode:

jedi

...and just for completion, this happens with the Python language server (jedi disabled) in vscode (not relevant here):

pylangserver

This was all tested using PySide2 5.12.3.

@dwtd
Copy link

dwtd commented May 23, 2019

I tried this on my Windows 7 workstation and it doesn’t seem to work. The .pyi files are nowhere to be found.

This is immediately after installing PySide2 v5.12.13 and Qt.py. Is it possible this is OS-dependent? (looks like you’re on a Mac?)

@fredrikaverpil
Copy link
Author

fredrikaverpil commented May 23, 2019

On my mac, I just pip install pyside2 (gives me 5.12.3) and I see this:

$ cd venv/lib/python3.7/site-packages/pyside2 
$ ls *.pyi
Qt3DAnimation.pyi       QtCore.pyi              QtNetwork.pyi           QtScxml.pyi             QtWebEngine.pyi
Qt3DCore.pyi            QtDataVisualization.pyi QtOpenGL.pyi            QtSensors.pyi           QtWebEngineCore.pyi
Qt3DExtras.pyi          QtGui.pyi               QtPositioning.pyi       QtSql.pyi               QtWebEngineWidgets.pyi
Qt3DInput.pyi           QtHelp.pyi              QtPrintSupport.pyi      QtSvg.pyi               QtWebSockets.pyi
Qt3DLogic.pyi           QtLocation.pyi          QtQml.pyi               QtTest.pyi              QtWidgets.pyi
Qt3DRender.pyi          QtMacExtras.pyi         QtQuick.pyi             QtTextToSpeech.pyi      QtXml.pyi
QtCharts.pyi            QtMultimedia.pyi        QtQuickWidgets.pyi      QtUiTools.pyi           QtXmlPatterns.pyi
QtConcurrent.pyi        QtMultimediaWidgets.pyi QtRemoteObjects.pyi     QtWebChannel.pyi

$ python --version
Python 3.7.3

You don't see them inside the package?

@dwtd
Copy link

dwtd commented May 23, 2019

Unfortunately no. I also installed via pip (pip install pyside2 qt.py), but when I attempt the same ls, I get this:

$ cd venv/Lib/site-packages/pyside2
$ ls *.pyi
ls: cannot access '*.pyi': No such file or directory

$ python --version
Python 3.6.6

The directory seems to contain everything but .pyi files. I get nothing from a find command run from the venv root.

@ColinKennedy
Copy link

ColinKennedy commented May 24, 2019

I installed PySide2 / Qt.py for Python 2 and the .pyi files are there for me

$ cd /home/selecaoone/.local/lib/python2.7/site-packages/PySide2 && ls *.pyi
Qt3DAnimation.pyi  Qt3DRender.pyi           QtGui.pyi                QtNetwork.pyi       QtQuick.pyi          QtSql.pyi           QtWebChannel.pyi        QtWidgets.pyi
Qt3DCore.pyi       QtCharts.pyi             QtHelp.pyi               QtOpenGL.pyi        QtQuickWidgets.pyi   QtSvg.pyi           QtWebEngineCore.pyi     QtX11Extras.pyi
Qt3DExtras.pyi     QtConcurrent.pyi         QtLocation.pyi           QtPositioning.pyi   QtRemoteObjects.pyi  QtTest.pyi          QtWebEngine.pyi         QtXmlPatterns.pyi
Qt3DInput.pyi      QtCore.pyi               QtMultimedia.pyi         QtPrintSupport.pyi  QtScxml.pyi          QtTextToSpeech.pyi  QtWebEngineWidgets.pyi  QtXml.pyi
Qt3DLogic.pyi      QtDataVisualization.pyi  QtMultimediaWidgets.pyi  QtQml.pyi           QtSensors.pyi        QtUiTools.pyi       QtWebSockets.pyi

This is the command that was used to install:

python -m pip install PySide2 Qt.py --user

I use Centos 7, by the way

@davidhalter
Copy link
Owner

@dwtd You might need to try pip install --upgrade.

@ColinKennedy @fredrikaverpil So I suppose with the current master branch completion for PySide2 is not working but not for Qt.py?!

@ColinKennedy
Copy link

I'll have to look into this more over the weekend as I'm short for time but I wasn't aware that Jedi supported .pyi files. #839

Also there's a thread on PyCharm / Intellisense support which may be relevant to this issue thread.
mottosso/Qt.py#199

@dwtd
Copy link

dwtd commented May 24, 2019

Thanks, all. I tried the same install on my Mac, and sure enough, the .pyi files are installed there. Looks like this might be specific to Windows 7. I also tried copying the .pyi files from my Mac to my PC (total hack I know) but no luck there either.

@davidhalter Thanks for the suggestion. I confirmed that the python modules in my virtual environment are all current.

@ColinKennedy Thanks for the link to the other thread. I had actually started there, wasn't able to get things working, and then found this thread.

I'll switch my project back to pure PySide2 for now, but I'm happy to help troubleshooting if you all have additional ideas.

@fredrikaverpil
Copy link
Author

I can confirm that I also do not see the *.pyi files on Windows. I have posted about this in the PySide2 gitter channel.

@ColinKennedy @fredrikaverpil So I suppose with the current master branch completion for PySide2 is not working but not for Qt.py?!

@davidhalter I am unsure how I can make use of the master branch to try it out. I am using vscode. Can I somehow replace the bundled jedi with the master branch one?

@fredrikaverpil
Copy link
Author

fredrikaverpil commented May 24, 2019

@davidhalter I realized jedi is just dumped into a folder within the vscode-python extension. I tried replacing it, but then jedi broke completely, it seems. I have reached out to some of the devs to see if they can give me a pointer on any additional steps I must take...

EDIT: I was able to debug via the console in the "Developer Tools" of vscode. Turns out parso had to be updated and jedi.api.environment.Environment was changed since 0.12.0 to be called with just one argument.

@fredrikaverpil
Copy link
Author

fredrikaverpil commented May 24, 2019

@davidhalter I got jedi from master branch working in vscode now. I can confirm that autocomplete works with PySide2 but not with Qt.py.

pyside2

qtpy

@fredrikaverpil
Copy link
Author

fredrikaverpil commented May 24, 2019

It's possible that something needs to be done on Microsoft's side with the vscode-python custom completion.py file to make this all work?

@fredrikaverpil
Copy link
Author

fredrikaverpil commented May 24, 2019

Quick proof of .pyi working in vscode:

Screenshot 2019-05-24 at 23 43 11

Although the .pyi bundled with PySide2 cannot be loaded through Qt.py (which is what this issue is about).

Steps to enable .pyi files in vscode:

  1. Replace parso and jedi modules with their respective master branches in ~/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/lib
  2. Update ~/.vscode/extensions/ms-python.python-2019.4.12954/pythonFiles/completion.py to say self.environment = jedi.api.environment.Environment(sys.executable) (this now only takes one argument).
  3. Enable jedi in vscode

@davidhalter
Copy link
Owner

davidhalter commented May 26, 2019

For the second step you could also just write:

self.environment = jedi.api.environment.get_cached_default_environment()

@fredrikaverpil
Copy link
Author

For the second step you could also just write: ...

@davidhalter ah, yes - much better.

Do you think that the Qt.py "passthrough" is not working because of jedi or because of vscode's jedi implementation?

@davidhalter
Copy link
Owner

The Qt passthrough is not working, because it's dynamic. If you look at the source code, you can see that it's even really hard to understand for a human.

There's a couple of possible solutions:

  • Just don't use Qt.py and use PySide2 instead :)
  • Copy the stubs from PySide2 and save them inside Qt-stubs.
  • Remap Qt to to PySide in Jedi.
  • Write your own Qt module that looks like this:
try:
    from PySide2 import *
except ImportError:
    from PyQt4 import *

Not sure though how much of the logic inside Qt.py is actually needed.

There's absolutely no way Jedi ever understands the crazy logic of Qt.py without hardcoding it.

@fredrikaverpil
Copy link
Author

fredrikaverpil commented May 26, 2019

If you look at the source code, you can see that it's even really hard to understand for a human.

Which source code are you referring to?
I am guessing you mean Qt.py 😉 and in case you have the time;

I am one of the developers behind Qt.py and all it really does is replace itself with the Qt binding you have available in your environment (e.g. PySide2). The core purpose of Qt.py is to provide a way to write PySide2 code which is backwards compatible with PySide/PyQt4 (and will also work with PyQt5). And for this purpose, the way the replacement is performed gets complicated. For example, a certain module was moved between Qt4 and Qt5 and thus needs remapping.

Qt.py is widely used and adopted in the feature films business, where a bunch of Qt-based 3D/animation software which has Python scripting capabilities are used in a variety of versions within the same company. Nobody wants to maintain separate code for all of these Qt bindings and Qt.py makes it possible to avoid that.

So, as you might imagine some of us working with Qt.py sorely miss the intellisense/autocompletion... and here we are 😄

For some reason, this kind of "passthrough" just works in PyCharm and I am trying to figure out how this is possible and if this can somehow be enabled for my favourite editor (vscode) as well, and Jedi looks like my best bet right now. But I take it then that you don't think Jedi can enable this behavior?

Remap Qt to PySide in Jedi.

Oh, this sounds interesting but I am not sure in which way you mean. How would I go about trying this?


I just tweaked the file structure a little bit and now I can actually make use of the vanilla PySide2 .pyi files when using Qt.py. So perhaps this is the best way forward: mottosso/Qt.py#199 (comment) unless you can come to think of a less brute force approach?
– There is no way I can add the location of the desired .pyi files to sys.path manually or something?

Either way, I am very grateful for your work and I am super happy to see you devoted your time to add support for typeshed/stubs!

As soon as jedi gets a release with typeshed/stubs I'll make a PR to get it into vscode! ❤️

@davidhalter
Copy link
Owner

Oh, this sounds interesting but I am not sure in which way you mean. How would I go about trying this?

It's something that would need either a plugin structure for Jedi (which isn't public, yet) or we have to hard-code it within Jedi, which I really don't like.

So IMO tweaking the file structure is probably the best idea for now. Since you create a compatibility layer between different projects, separate stubs are probably the way to go anyway.

@fredrikaverpil
Copy link
Author

Ok. Thanks for clarifying. I will close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants