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

PR: Change bindings try order to be PyQt5, PySide2, PyQt6, PySide6 #343

Merged
merged 1 commit into from
Apr 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -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/)
Expand All @@ -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
Expand All @@ -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).

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
61 changes: 30 additions & 31 deletions qtpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
======

Expand All @@ -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
=======

Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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
Expand All @@ -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:
Expand Down
22 changes: 11 additions & 11 deletions qtpy/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 8 additions & 8 deletions qtpy/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
19 changes: 12 additions & 7 deletions qtpy/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down