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: Avoid future deprecations and decrease general technical debt #273

Merged
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
16 changes: 10 additions & 6 deletions qtpy/QtCharts.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,19 @@
if PYQT5:
try:
from PyQt5.QtChart import *
except ImportError:
raise PythonQtError('The QtChart module was not found. '
'It needs to be installed separately for PyQt5.')
except ImportError as error:
raise PythonQtError(
'The QtChart module was not found. '
'It needs to be installed separately for PyQt5.'
) from error
elif PYQT6:
try:
from PyQt6.QtCharts import *
except ImportError:
raise PythonQtError('The QtCharts module was not found. '
'It needs to be installed separately for PyQt6.')
except ImportError as error:
raise PythonQtError(
'The QtCharts module was not found. '
'It needs to be installed separately for PyQt6.'
) from error
elif PYSIDE6:
from PySide6.QtCharts import *
elif PYSIDE2:
Expand Down
18 changes: 9 additions & 9 deletions qtpy/QtCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,21 @@
del pyqtSignal, pyqtBoundSignal, pyqtSlot, pyqtProperty, QT_VERSION_STR

elif PYSIDE6:
from PySide6.QtCore import *
import PySide6.QtCore
__version__ = PySide6.QtCore.__version__
from PySide6.QtCore import *
import PySide6.QtCore
__version__ = PySide6.QtCore.__version__

# obsolete in qt6
Qt.BackgroundColorRole = Qt.BackgroundRole
Qt.TextColorRole = Qt.ForegroundRole
Qt.MidButton = Qt.MiddleButton
# obsolete in qt6
Qt.BackgroundColorRole = Qt.BackgroundRole
Qt.TextColorRole = Qt.ForegroundRole
Qt.MidButton = Qt.MiddleButton

elif PYSIDE2:
from PySide2.QtCore import *

try: # may be limited to PySide-5.11a1 only
try: # may be limited to PySide-5.11a1 only
from PySide2.QtGui import QStringListModel
except:
except Exception:
pass

import PySide2.QtCore
Expand Down
6 changes: 2 additions & 4 deletions qtpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,11 @@


class PythonQtError(RuntimeError):
"""Error raise if no bindings could be selected."""
pass
"""Error raised if no bindings could be selected."""


class PythonQtWarning(Warning):
"""Warning if some features are not implemented in a binding."""
pass


# Qt API environment variable name
Expand Down Expand Up @@ -110,7 +108,7 @@ class PythonQtWarning(Warning):
elif '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'
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'

Expand Down
4 changes: 2 additions & 2 deletions qtpy/tests/test_qtcore.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Test QtCore."""

import pytest
from qtpy import PYQT5, PYQT6, PYSIDE2, QtCore

"""Test QtCore."""


def test_qtmsghandler():
"""Test qtpy.QtMsgHandler"""
Expand Down
2 changes: 1 addition & 1 deletion qtpy/tests/test_qtdesigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

@pytest.mark.skipif(PYSIDE2, reason="QtDesigner is not avalaible in PySide2")
def test_qtdesigner():
"""Test the qtpy.QtDesigner namespace."""
from qtpy import QtDesigner
"""Test the qtpy.QtDesigner namespace"""
assert QtDesigner.QAbstractExtensionFactory is not None
assert QtDesigner.QAbstractExtensionManager is not None
assert QtDesigner.QDesignerActionEditorInterface is not None
Expand Down
4 changes: 2 additions & 2 deletions qtpy/tests/test_qtsvg.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def test_qtsvg():
from qtpy import QtSvg

if not (PYSIDE6 or PYQT6):
assert QtSvg.QGraphicsSvgItem is not None
assert QtSvg.QSvgWidget is not None
assert QtSvg.QGraphicsSvgItem is not None
assert QtSvg.QSvgWidget is not None
assert QtSvg.QSvgGenerator is not None
assert QtSvg.QSvgRenderer is not None
16 changes: 10 additions & 6 deletions qtpy/tests/test_uic.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ class _QComboBoxSubclass(QComboBox):
"""

@contextlib.contextmanager
def enabled_qcombobox_subclass(tmpdir):
def enabled_qcombobox_subclass(temp_dir_path):
"""
Context manager that sets up a temporary module with a QComboBox subclass
and then removes it once we are done.
"""

with open(tmpdir.join('qcombobox_subclass.py').strpath, 'w') as f:
with open(
temp_dir_path / 'qcombobox_subclass.py',
mode='w',
encoding="utf-8",
) as f:
f.write(QCOMBOBOX_SUBCLASS)

sys.path.insert(0, tmpdir.strpath)
sys.path.insert(0, str(temp_dir_path))

yield

Expand Down Expand Up @@ -106,7 +110,7 @@ def __init__(self):
os.environ.get('CI', None) is not None
and sys.platform.startswith('linux'),
reason="Segfaults on Linux CIs under all bindings (PYSIDE2/6 & PYQT5/6)")
def test_load_ui_custom_auto(tmpdir):
def test_load_ui_custom_auto(tmp_path):
"""
Test that we can load a .ui file with custom widgets without having to
explicitly specify a dictionary of custom widgets, even in the case of
Expand All @@ -115,7 +119,7 @@ def test_load_ui_custom_auto(tmpdir):

app = get_qapp()

with enabled_qcombobox_subclass(tmpdir):
with enabled_qcombobox_subclass(tmp_path):
from qcombobox_subclass import _QComboBoxSubclass
with warnings.catch_warnings():
warnings.filterwarnings(
Expand All @@ -136,4 +140,4 @@ def test_load_full_uic():
else:
objects = ['compileUi', 'compileUiDir', 'loadUi', 'loadUiType',
'widgetPluginPath']
assert all([hasattr(uic, o) for o in objects])
assert all((hasattr(uic, o) for o in objects))
10 changes: 6 additions & 4 deletions qtpy/uic.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,11 @@ def createWidget(self, class_name, parent=None, name=''):
# customWidgets is empty.
try:
widget = self.customWidgets[class_name](parent)
except KeyError:
raise Exception('No custom widget ' + class_name + ' '
'found in customWidgets')
except KeyError as error:
raise Exception(
f'No custom widget {class_name} '
'found in customWidgets'
) from error

if self.baseinstance:
# set an attribute for the new child widget on the base
Expand Down Expand Up @@ -254,7 +256,7 @@ def loadUiType(uifile, from_imports=False):
widget_class = ui.find('widget').get('class')
form_class = ui.find('class').text

with open(uifile) as fd:
with open(uifile, encoding="utf-8") as fd:
code_stream = StringIO()
frame = {}

Expand Down