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: Clean up the Qt namespace and add the tests #465

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
9 changes: 9 additions & 0 deletions qtpy/Qsci.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@
) from error
elif PYSIDE2 or PYSIDE6:
raise QtBindingMissingModuleError(name="Qsci")

del (
PYQT5,
PYQT6,
PYSIDE2,
PYSIDE6,
QtBindingMissingModuleError,
QtModuleNotInstalledError,
)
5 changes: 5 additions & 0 deletions qtpy/Qt3DAnimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

for __name in inspect.getmembers(__temp.Qt3DAnimation):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect
elif PYSIDE6:
# https://bugreports.qt.io/projects/PYSIDE/issues/PYSIDE-1026
import inspect
Expand All @@ -47,3 +48,7 @@

for __name in inspect.getmembers(__temp.Qt3DAnimation):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtModuleNotInstalledError
5 changes: 5 additions & 0 deletions qtpy/Qt3DCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

for __name in inspect.getmembers(__temp.Qt3DCore):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect
elif PYSIDE6:
# https://bugreports.qt.io/projects/PYSIDE/issues/PYSIDE-1026
import inspect
Expand All @@ -47,3 +48,7 @@

for __name in inspect.getmembers(__temp.Qt3DCore):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtModuleNotInstalledError
5 changes: 5 additions & 0 deletions qtpy/Qt3DExtras.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

for __name in inspect.getmembers(__temp.Qt3DExtras):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect
elif PYSIDE6:
# https://bugreports.qt.io/projects/PYSIDE/issues/PYSIDE-1026
import inspect
Expand All @@ -47,3 +48,7 @@

for __name in inspect.getmembers(__temp.Qt3DExtras):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtModuleNotInstalledError
5 changes: 5 additions & 0 deletions qtpy/Qt3DInput.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

for __name in inspect.getmembers(__temp.Qt3DInput):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect
elif PYSIDE6:
# https://bugreports.qt.io/projects/PYSIDE/issues/PYSIDE-1026
import inspect
Expand All @@ -47,3 +48,7 @@

for __name in inspect.getmembers(__temp.Qt3DInput):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtModuleNotInstalledError
5 changes: 5 additions & 0 deletions qtpy/Qt3DLogic.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

for __name in inspect.getmembers(__temp.Qt3DLogic):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect
elif PYSIDE6:
# https://bugreports.qt.io/projects/PYSIDE/issues/PYSIDE-1026
import inspect
Expand All @@ -47,3 +48,7 @@

for __name in inspect.getmembers(__temp.Qt3DLogic):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtModuleNotInstalledError
5 changes: 5 additions & 0 deletions qtpy/Qt3DRender.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

for __name in inspect.getmembers(__temp.Qt3DRender):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect
elif PYSIDE6:
# https://bugreports.qt.io/projects/PYSIDE/issues/PYSIDE-1026
import inspect
Expand All @@ -47,3 +48,7 @@

for __name in inspect.getmembers(__temp.Qt3DRender):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtModuleNotInstalledError
3 changes: 3 additions & 0 deletions qtpy/QtAxContainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
from PySide2.QtAxContainer import *
elif PYSIDE6:
from PySide6.QtAxContainer import *

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtBindingMissingModuleError
3 changes: 3 additions & 0 deletions qtpy/QtBluetooth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@
raise QtBindingMissingModuleError(name="QtBluetooth")
elif PYSIDE6:
from PySide6.QtBluetooth import *

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtBindingMissingModuleError
4 changes: 4 additions & 0 deletions qtpy/QtCharts.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@

for __name in inspect.getmembers(__temp.QtCharts):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect
elif PYSIDE6:
from PySide6 import QtCharts
from PySide6.QtCharts import *

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtModuleNotInstalledError
3 changes: 3 additions & 0 deletions qtpy/QtConcurrent.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,6 @@
from PySide2.QtConcurrent import *
elif PYSIDE6:
from PySide6.QtConcurrent import *

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtBindingMissingModuleError
131 changes: 70 additions & 61 deletions qtpy/QtCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,20 @@

"""Provides QtCore classes and functions."""
import contextlib
from functools import partial, partialmethod
from typing import TYPE_CHECKING

from packaging.version import parse

from . import PYQT5, PYQT6, PYSIDE2, PYSIDE6
from . import QT_VERSION as _qt_version
from ._utils import possibly_static_exec, possibly_static_exec_
from . import (
PYQT5,
PYQT6,
PYSIDE2,
PYSIDE6,
QT_VERSION,
QtBindingsNotFoundError,
_utils,
)

if PYQT5:
from PyQt5.QtCore import *
Expand Down Expand Up @@ -55,13 +62,16 @@
from PyQt6.QtGui import Qt

# Map missing methods
QCoreApplication.exec_ = lambda *args, **kwargs: possibly_static_exec(
QCoreApplication,
*args,
**kwargs,
QCoreApplication.exec_ = partial(
lambda *args, _function, **kwargs: _function(
QCoreApplication,
*args,
**kwargs,
),
_function=_utils.possibly_static_exec,
)
QEventLoop.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QThread.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QEventLoop.exec_ = partialmethod(QEventLoop.exec)
QThread.exec_ = partialmethod(QThread.exec)

# Those are imported from `import *`
del (
Expand All @@ -77,7 +87,7 @@
from .enums_compat import promote_enums

promote_enums(QtCore)
del QtCore
del QtCore, promote_enums

# Alias deprecated ItemDataRole enum values removed in Qt6
Qt.BackgroundColorRole = (
Expand All @@ -95,10 +105,8 @@
Qt.ItemFlags = lambda value=0: Qt.ItemFlag(value)

elif PYSIDE2:
import PySide2.QtCore
from PySide2.QtCore import *

__version__ = PySide2.QtCore.__version__
from PySide2.QtCore import __version__

# Missing QtGui utility functions on Qt
if getattr(Qt, "mightBeRichText", None) is None:
Expand All @@ -111,23 +119,21 @@
# Fails with PySide2 5.12.0
pass

QCoreApplication.exec = lambda *args, **kwargs: possibly_static_exec_(
QCoreApplication,
*args,
**kwargs,
)
QEventLoop.exec = lambda self, *args, **kwargs: self.exec_(*args, **kwargs)
QThread.exec = lambda self, *args, **kwargs: self.exec_(*args, **kwargs)
QTextStreamManipulator.exec = lambda self, *args, **kwargs: self.exec_(
*args,
**kwargs,
QCoreApplication.exec = partial(
lambda *args, _function, **kwargs: _function(
QCoreApplication,
*args,
**kwargs,
),
_function=_utils.possibly_static_exec_,
)
QEventLoop.exec = partialmethod(QEventLoop.exec_)
QThread.exec = partialmethod(QThread.exec_)
QTextStreamManipulator.exec = partialmethod(QTextStreamManipulator.exec_)

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

__version__ = PySide6.QtCore.__version__
from PySide6.QtCore import __version__

# Missing QtGui utility functions on Qt
if getattr(Qt, "mightBeRichText", None) is None:
Expand All @@ -144,49 +150,31 @@
Qt.MidButton = Qt.MiddleButton

# Map DeprecationWarning methods
QCoreApplication.exec_ = lambda *args, **kwargs: possibly_static_exec(
QCoreApplication,
*args,
**kwargs,
)
QEventLoop.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QThread.exec_ = lambda self, *args, **kwargs: self.exec(*args, **kwargs)
QTextStreamManipulator.exec_ = lambda self, *args, **kwargs: self.exec(
*args,
**kwargs,
QCoreApplication.exec_ = partial(
lambda *args, _function, **kwargs: _function(
QCoreApplication,
*args,
**kwargs,
),
_function=_utils.possibly_static_exec,
)
QEventLoop.exec_ = partialmethod(QEventLoop.exec)
QThread.exec_ = partialmethod(QThread.exec)
QTextStreamManipulator.exec_ = partialmethod(QTextStreamManipulator.exec)

# Passing as default value 0 in the same way PySide6 6.3.2 does for the `Qt.ItemFlags` definition.
if parse(_qt_version) > parse("6.3"):
if parse(QT_VERSION) > parse("6.3"):
Qt.ItemFlags = lambda value=0: Qt.ItemFlag(value)

# For issue #153 and updated for issue #305
if PYQT5 or PYQT6:
QDate.toPython = lambda self, *args, **kwargs: self.toPyDate(
*args,
**kwargs,
)
QDateTime.toPython = lambda self, *args, **kwargs: self.toPyDateTime(
*args,
**kwargs,
)
QTime.toPython = lambda self, *args, **kwargs: self.toPyTime(
*args,
**kwargs,
)
QDate.toPython = partialmethod(QDate.toPyDate)
QDateTime.toPython = partialmethod(QDateTime.toPyDateTime)
QTime.toPython = partialmethod(QTime.toPyTime)
if PYSIDE2 or PYSIDE6:
QDate.toPyDate = lambda self, *args, **kwargs: self.toPython(
*args,
**kwargs,
)
QDateTime.toPyDateTime = lambda self, *args, **kwargs: self.toPython(
*args,
**kwargs,
)
QTime.toPyTime = lambda self, *args, **kwargs: self.toPython(
*args,
**kwargs,
)
QDate.toPyDate = partialmethod(QDate.toPython)
QDateTime.toPyDateTime = partialmethod(QDateTime.toPython)
QTime.toPyTime = partialmethod(QTime.toPython)

# Mirror https://github.com/spyder-ide/qtpy/pull/393
if PYQT5 or PYSIDE2:
Expand All @@ -195,3 +183,24 @@
if PYQT6 or PYSIDE6:
QLibraryInfo.location = QLibraryInfo.path
QLibraryInfo.LibraryLocation = QLibraryInfo.LibraryPath

# If something is imported, `__version__` ought to be defined.
try:
assert __version__
except (NameError, AssertionError):
raise QtBindingsNotFoundError from None

# Clean up the namespace
del (
PYQT5,
PYQT6,
PYSIDE2,
PYSIDE6,
QT_VERSION,
QtBindingsNotFoundError,
_utils,
)
del TYPE_CHECKING
del contextlib
del partial, partialmethod
del parse
11 changes: 11 additions & 0 deletions qtpy/QtDBus.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@
from PySide6.QtDBus import *
else:
raise QtModuleNotInOSError(name="QtDBus")

# Clean up the namespace
del (
PYQT5,
PYQT6,
PYSIDE2,
PYSIDE6,
QtBindingMissingModuleError,
QtModuleNotInOSError,
)
del sys
4 changes: 4 additions & 0 deletions qtpy/QtDataVisualization.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,9 @@

for __name in inspect.getmembers(__temp.QtDataVisualization):
globals()[__name[0]] = __name[1]
del __name, __temp, inspect
elif PYSIDE6:
from PySide6.QtDataVisualization import *

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtModuleNotInstalledError
3 changes: 3 additions & 0 deletions qtpy/QtDesigner.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@
raise QtBindingMissingModuleError(name="QtDesigner")
elif PYSIDE6:
from PySide6.QtDesigner import *

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QtBindingMissingModuleError
Loading
Loading