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

Fix importing QtWebEngine after toolkit selection #853

Merged
merged 4 commits into from
Jan 19, 2021

Conversation

kitchoi
Copy link
Contributor

@kitchoi kitchoi commented Dec 16, 2020

Fix #581

First of all, we need a separate distribution for QtWebEngine with PyQt5. This PR expands the extras_require so that pip install pyface[pyqt5] brings in PyQtWebEngine.

After installing PyQtWebEngine, importing QtWebEngineWidgets after instantiating a QCoreApplication results in an ImportError. This PR fixes this by trying to import QtWebEngineWidgets before instantiating the QApplication.

All of this has to do with import side effect so it is hard to test. If the test mentioned in #581 is run on its own, it passes on master:

$ python -m unittest pyface.ui.qt4.tests.test_qt_imports.TestPyfaceQtImports
.
----------------------------------------------------------------------
Ran 1 test in 0.213s

OK

But if it is run after some other tests that have instantiated the QApplication, it fails (on master), as in #581:

$ python -m unittest pyface.ui.qt4.tests.test_gui pyface.ui.qt4.tests.test_qt_imports.TestPyfaceQtImports
======================================================================
ERROR: test_imports (pyface.ui.qt4.tests.test_qt_imports.TestPyfaceQtImports)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/kchoi/Work/ETS/pyface/pyface/qt/QtWebKit.py", line 20, in <module>
    from PyQt5.QtWebEngineWidgets import (
ImportError: QtWebEngineWidgets must be imported before a QCoreApplication instance is created

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/kchoi/Work/ETS/pyface/pyface/ui/qt4/tests/test_qt_imports.py", line 24, in test_imports
    import pyface.qt.QtWebKit
  File "/Users/kchoi/Work/ETS/pyface/pyface/qt/QtWebKit.py", line 28, in <module>
    from PyQt5.QtWebKit import *
ModuleNotFoundError: No module named 'PyQt5.QtWebKit'

----------------------------------------------------------------------
Ran 2 tests in 0.062s

FAILED (errors=1)

With this PR, the second test command also passes.

@codecov-io
Copy link

codecov-io commented Dec 16, 2020

Codecov Report

Merging #853 (aa20577) into master (4a991af) will increase coverage by 0.10%.
The diff coverage is 50.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #853      +/-   ##
==========================================
+ Coverage   41.19%   41.29%   +0.10%     
==========================================
  Files         522      522              
  Lines       28128    28132       +4     
  Branches     4253     4253              
==========================================
+ Hits        11588    11618      +30     
+ Misses      16034    16017      -17     
+ Partials      506      497       -9     
Impacted Files Coverage Δ
pyface/__init__.py 88.88% <ø> (+7.40%) ⬆️
pyface/ui/qt4/init.py 60.00% <50.00%> (-2.50%) ⬇️
pyface/ui/qt4/console/console_widget.py 28.86% <0.00%> (+0.31%) ⬆️
pyface/ui/qt4/code_editor/code_widget.py 43.55% <0.00%> (+0.84%) ⬆️
pyface/wx/python_stc.py 10.36% <0.00%> (+1.21%) ⬆️
pyface/ui/qt4/tasks/task_window_backend.py 60.52% <0.00%> (+1.31%) ⬆️
pyface/ui/qt4/util/gui_test_assistant.py 80.53% <0.00%> (+2.65%) ⬆️
pyface/ui/wx/window.py 77.10% <0.00%> (+4.81%) ⬆️
pyface/ui/wx/data_view/data_view_widget.py 77.52% <0.00%> (+5.61%) ⬆️
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4a991af...79efc16. Read the comment docs.

@kitchoi
Copy link
Contributor Author

kitchoi commented Dec 16, 2020

Note that CI passing does not verify the PR has fixed the issue, because CI is installing PyQt5 from EDM with a version that includes QtWebKit instead of QtWebEngine. The issue has to be verified with an installation using PyQt5/PyQtWebEngine (from PyPI for example).

@@ -22,7 +22,7 @@
__extras_require__ = {
"wx": ["wxpython>=4", "numpy"],
"pyqt": ["pyqt>=4.10", "pygments"],
"pyqt5": ["pyqt5", "pygments"],
"pyqt5": ["pyqt5", "PyQtWebEngine", "pygments"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only needed for pyqt5 >= 5.12, so probably should have that encoded in the PyQt version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you mean "pyqt5": ["pyqt5>=5.12", "PyQtWebEngine", "pygments"]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or I could leave this "PyQtWebEngine" out but instead document this additional requirement somewhere.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking the first of these, unless there is a potential problem with that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You did make me become aware that PyQtWebEngine is only distributed on PyPI for PyQt5>=5.12. But did you mean that it is only available for pyqt5>=5.12, rather than needed?

Specifying "pyqt5>=5.12" would to me means that there are reasons within Pyface that causes pyqt5<5.12 not to be supported, is that true?

So far the ">=5.12" bit seems to me a requirement of PyQtWebEngine, not a requirement of Pyface. If there is a distribution of PyQtWebEngine that would support PyQt5 5.11, I am not sure Pyface should disallow it (I don't think Pyface is setting these minimum requirements based on what are tested on CI - because if it does it would be very restrictive.)

I think only these two options currently make sense to me:
(1) ["pyqt5", "pygments"]
(2) ["pyqt5", "PyQtWebEngine", "pygments"]

In any case, these are extras requirements, so they are already optional - they may even be ignored by some installers (e.g. edm).

Perhaps I should revert the change to "extras_require" so we can focus on the changes in the import order which fixes the import error in #581, the error occurs even if one already has the required Qt web dependencies installed.

@kitchoi
Copy link
Contributor Author

kitchoi commented Jan 5, 2021

I have reverted the change to extras_require for now. It can be added again if it becomes necessary.

Copy link
Contributor

@rahulporuri rahulporuri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

pyface/ui/qt4/init.py Show resolved Hide resolved
@kitchoi kitchoi merged commit 8cc824e into master Jan 19, 2021
@kitchoi kitchoi deleted the import-qt-webengine-after-toolkit-selection branch January 19, 2021 14:36
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

Successfully merging this pull request may close these issues.

Test failures with PyQt5 5.15.0 from PyPI
4 participants