From 411bd7172c0becddfa07d6d3e9381fa8cf1dadd7 Mon Sep 17 00:00:00 2001 From: dalthviz Date: Thu, 28 Apr 2022 12:11:47 -0500 Subject: [PATCH] Change bindings 'try order' to PyQt5, PySide2, PyQt6, PySide6 --- README.md | 14 +++++----- qtpy/__init__.py | 61 ++++++++++++++++++++--------------------- qtpy/tests/conftest.py | 22 +++++++-------- qtpy/tests/test_cli.py | 16 +++++------ qtpy/tests/test_main.py | 19 ++++++++----- 5 files changed, 68 insertions(+), 64 deletions(-) diff --git a/README.md b/README.md index a4ed6c3e..78b4473e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# QtPy: Abstraction layer for PyQt5/PyQt6/PySide2/PySide6 +# QtPy: Abstraction layer for PyQt5/PySide2/PyQt6/PySide6 [![license](https://img.shields.io/pypi/l/qtpy.svg)](./LICENSE) [![pypi version](https://img.shields.io/pypi/v/qtpy.svg)](https://pypi.org/project/QtPy/) @@ -10,7 +10,7 @@ [![Github build status](https://github.com/spyder-ide/qtpy/workflows/Tests/badge.svg)](https://github.com/spyder-ide/qtpy/actions) [![Coverage Status](https://coveralls.io/repos/github/spyder-ide/qtpy/badge.svg?branch=master)](https://coveralls.io/github/spyder-ide/qtpy?branch=master) -*Copyright © 2009–2021 The Spyder Development Team* +*Copyright © 2009–2022 The Spyder Development Team* ## Description @@ -22,7 +22,7 @@ It provides support for PyQt5, PyQt6, PySide6, PySide2 using the Qt5 layout (where the QtGui module has been split into QtGui and QtWidgets). Basically, you can write your code as if you were using PyQt or PySide directly, -but import Qt modules from `qtpy` instead of `PyQt5`, `PyQt6`, `PySide2`, or `PySide6`. +but import Qt modules from `qtpy` instead of `PyQt5`, `PySide2`, `PyQt6` or `PySide6`. Accordingly, when porting code between different Qt bindings (PyQt vs PySide) or Qt versions (Qt5 vs Qt6), QtPy makes this much more painless, and allows you to easily and incrementally transition between them. QtPy handles incompatibilities and differences between bindings or Qt versions for you while keeping your project running, so you can focus more on your own code and less on keeping track of supporting every Qt version and binding. Furthermore, when you do want to upgrade or support new bindings, it allows you to update your project module by module rather than all at once. You can check out examples of this approach in projects using QtPy, like [git-cola](https://github.com/git-cola/git-cola/issues/232). @@ -46,16 +46,16 @@ This project is released under the MIT license. ### Requirements -You need PyQt5, PyQt6, PySide2 or PySide6 installed in your system to make use +You need PyQt5, PySide2, PyQt6 or PySide6 installed in your system to make use of QtPy. If several of these packages are found, PyQt5 is used by default unless you set the `QT_API` environment variable. `QT_API` can take the following values: * `pyqt5` (to use PyQt5). +* `pyside2` (to use PySide2). * `pyqt6` (to use PyQt6). -* `pyside6` (to use PySide6) -* `pyside2` (to use PySide2) +* `pyside6` (to use PySide6). ### Installation @@ -91,7 +91,7 @@ For example, in an environment where PyQt5 is installed and selected this would output the following: ```text ---always-true=PYQT5 --always-false=PYQT6 --always-false=PYSIDE2 --always-false=PYSIDE6 +--always-true=PYQT5 --always-false=PYSIDE2 --always-false=PYQT6 --always-false=PYSIDE6 ``` Using Bash or a similar shell, this can be injected into diff --git a/qtpy/__init__.py b/qtpy/__init__.py index d0ce4449..a37505aa 100644 --- a/qtpy/__init__.py +++ b/qtpy/__init__.py @@ -11,8 +11,8 @@ If one of the APIs has already been imported, then it will be used. -Otherwise, the shim will automatically select the first available API (PyQt5, PyQt6, -PySide2 and PySide6); in that case, you can force the use of one +Otherwise, the shim will automatically select the first available API (PyQt5, PySide2, +PyQt6 and PySide6); in that case, you can force the use of one specific bindings (e.g. if your application is using one specific bindings and you need to use library that use QtPy) by setting up the ``QT_API`` environment variable. @@ -25,14 +25,6 @@ >>> from qtpy import QtGui, QtWidgets, QtCore >>> print(QtWidgets.QWidget) -PyQt6 -===== - - >>> import os - >>> os.environ['QT_API'] = 'pyqt6' - >>> from qtpy import QtGui, QtWidgets, QtCore - >>> print(QtWidgets.QWidget) - PySide2 ====== @@ -44,6 +36,14 @@ >>> from qtpy import QtGui, QtWidgets, QtCore >>> print(QtWidgets.QWidget) +PyQt6 +===== + + >>> import os + >>> os.environ['QT_API'] = 'pyqt6' + >>> from qtpy import QtGui, QtWidgets, QtCore + >>> print(QtWidgets.QWidget) + PySide6 ======= @@ -102,8 +102,8 @@ class PythonQtValueError(ValueError): # Detecting if a binding was specified by the user binding_specified = QT_API in os.environ -API_NAMES = {'pyqt5': 'PyQt5', 'pyqt6': 'PyQt6', - 'pyside2': 'PySide2', 'pyside6': 'PySide6'} +API_NAMES = {'pyqt5': 'PyQt5', 'pyside2': 'PySide2', + 'pyqt6': 'PyQt6', 'pyside6': 'PySide6'} API = os.environ.get(QT_API, 'pyqt5').lower() initial_api = API if API not in API_NAMES: @@ -121,14 +121,14 @@ class PythonQtValueError(ValueError): # Unless `FORCE_QT_API` is set, use previously imported Qt Python bindings if not os.environ.get('FORCE_QT_API'): - if 'PyQt6' in sys.modules: - API = initial_api if initial_api in PYQT6_API else 'pyqt6' - elif 'PyQt5' in sys.modules: + if 'PyQt5' in sys.modules: API = initial_api if initial_api in PYQT5_API else 'pyqt5' - elif 'PySide6' in sys.modules: - API = initial_api if initial_api in PYSIDE6_API else 'pyside6' elif 'PySide2' in sys.modules: API = initial_api if initial_api in PYSIDE2_API else 'pyside2' + elif 'PyQt6' in sys.modules: + API = initial_api if initial_api in PYQT6_API else 'pyqt6' + elif 'PySide6' in sys.modules: + API = initial_api if initial_api in PYSIDE6_API else 'pyside6' if API in PYQT5_API: try: @@ -153,25 +153,11 @@ class PythonQtValueError(ValueError): "system.") del macos_version - except ImportError: - API = 'pyqt6' - else: - os.environ[QT_API] = API - -if API in PYQT6_API: - try: - from PyQt6.QtCore import PYQT_VERSION_STR as PYQT_VERSION # analysis:ignore - from PyQt6.QtCore import QT_VERSION_STR as QT_VERSION # analysis:ignore - - QT5 = PYQT5 = False - QT6 = PYQT6 = True - except ImportError: API = 'pyside2' else: os.environ[QT_API] = API - if API in PYSIDE2_API: try: from PySide2 import __version__ as PYSIDE_VERSION # analysis:ignore @@ -190,6 +176,19 @@ class PythonQtValueError(ValueError): "system.") del macos_version + except ImportError: + API = 'pyqt6' + else: + os.environ[QT_API] = API + +if API in PYQT6_API: + try: + from PyQt6.QtCore import PYQT_VERSION_STR as PYQT_VERSION # analysis:ignore + from PyQt6.QtCore import QT_VERSION_STR as QT_VERSION # analysis:ignore + + QT5 = PYQT5 = False + QT6 = PYQT6 = True + except ImportError: API = 'pyside6' else: diff --git a/qtpy/tests/conftest.py b/qtpy/tests/conftest.py index a24f664f..f9b06f2b 100644 --- a/qtpy/tests/conftest.py +++ b/qtpy/tests/conftest.py @@ -18,35 +18,35 @@ def pytest_report_header(config): """Insert a customized header into the test report.""" versions = os.linesep - versions += 'PyQt6: ' + versions += 'PyQt5: ' try: - from PyQt6 import QtCore - versions += \ - f"PyQt: {QtCore.PYQT_VERSION_STR} - Qt: {QtCore.QT_VERSION_STR}" + from PyQt5 import Qt + versions += f"PyQt: {Qt.PYQT_VERSION_STR} - Qt: {Qt.QT_VERSION_STR}" except ImportError: versions += 'not installed' except AttributeError: versions += 'unknown version' versions += os.linesep - versions += 'PyQt5: ' + versions += 'PySide2: ' try: - from PyQt5 import Qt - versions += f"PyQt: {Qt.PYQT_VERSION_STR} - Qt: {Qt.QT_VERSION_STR}" + import PySide2 + from PySide2 import QtCore + versions += f"PySide: {PySide2.__version__} - Qt: {QtCore.__version__}" except ImportError: versions += 'not installed' except AttributeError: versions += 'unknown version' versions += os.linesep - versions += 'PySide2: ' + versions += 'PyQt6: ' try: - import PySide2 - from PySide2 import QtCore - versions += f"PySide: {PySide2.__version__} - Qt: {QtCore.__version__}" + from PyQt6 import QtCore + versions += \ + f"PyQt: {QtCore.PYQT_VERSION_STR} - Qt: {QtCore.QT_VERSION_STR}" except ImportError: versions += 'not installed' except AttributeError: diff --git a/qtpy/tests/test_cli.py b/qtpy/tests/test_cli.py index 42a10f33..6b0f0381 100644 --- a/qtpy/tests/test_cli.py +++ b/qtpy/tests/test_cli.py @@ -46,29 +46,29 @@ def test_cli_mypy_args(): if qtpy.PYQT5: expected = ' '.join([ '--always-true=PYQT5', - '--always-false=PYQT6', '--always-false=PYSIDE2', + '--always-false=PYQT6', '--always-false=PYSIDE6', ]) - elif qtpy.PYQT6: + elif qtpy.PYSIDE2: expected = ' '.join([ '--always-false=PYQT5', - '--always-true=PYQT6', - '--always-false=PYSIDE2', + '--always-true=PYSIDE2', + '--always-false=PYQT6', '--always-false=PYSIDE6', ]) - elif qtpy.PYSIDE2: + elif qtpy.PYQT6: expected = ' '.join([ '--always-false=PYQT5', - '--always-false=PYQT6', - '--always-true=PYSIDE2', + '--always-false=PYSIDE2', + '--always-true=PYQT6', '--always-false=PYSIDE6', ]) elif qtpy.PYSIDE6: expected = ' '.join([ '--always-false=PYQT5', - '--always-false=PYQT6', '--always-false=PYSIDE2', + '--always-false=PYQT6', '--always-true=PYSIDE6', ]) else: diff --git a/qtpy/tests/test_main.py b/qtpy/tests/test_main.py index 000ddf10..dd57fcca 100644 --- a/qtpy/tests/test_main.py +++ b/qtpy/tests/test_main.py @@ -69,27 +69,32 @@ def test_qt_api(): if QT_API == 'pyqt5': assert_pyqt5() - elif QT_API == 'pyqt6': - assert_pyqt6() elif QT_API == 'pyside2': assert_pyside2() + elif QT_API == 'pyqt6': + assert_pyqt6() elif QT_API == 'pyside6': assert_pyside6() else: # If the tests are run locally, USE_QT_API and QT_API may not be # defined, but we still want to make sure qtpy is behaving sensibly. # We should then be loading, in order of decreasing preference, PyQt5, - # PyQt6, and PySide2. + # PySide2, PyQt6 and PySide6. try: import PyQt5 except ImportError: try: - import PyQt6 - except ImportError: import PySide2 - assert_pyside2() + except ImportError: + try: + import PyQt6 + except ImportError: + import PySide6 + assert_pyside6() + else: + assert_pyqt6() else: - assert_pyqt6() + assert_pyside2() else: assert_pyqt5()