Skip to content

Commit

Permalink
Import _utils instead of importing from it
Browse files Browse the repository at this point in the history
  • Loading branch information
StSav012 committed Nov 1, 2023
1 parent 72d7827 commit 9fd5efb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 52 deletions.
10 changes: 5 additions & 5 deletions qtpy/QtCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
PYSIDE6,
QT_VERSION,
QtBindingsNotFoundError,
_utils,
)
from ._utils import possibly_static_exec, possibly_static_exec_

if PYQT5:
from PyQt5.QtCore import *
Expand Down Expand Up @@ -68,7 +68,7 @@
*args,
**kwargs,
),
_function=possibly_static_exec,
_function=_utils.possibly_static_exec,
)
QEventLoop.exec_ = partialmethod(QEventLoop.exec)
QThread.exec_ = partialmethod(QThread.exec)
Expand Down Expand Up @@ -125,7 +125,7 @@
*args,
**kwargs,
),
_function=possibly_static_exec_,
_function=_utils.possibly_static_exec_,
)
QEventLoop.exec = partialmethod(QEventLoop.exec_)
QThread.exec = partialmethod(QThread.exec_)
Expand Down Expand Up @@ -156,7 +156,7 @@
*args,
**kwargs,
),
_function=possibly_static_exec,
_function=_utils.possibly_static_exec,
)
QEventLoop.exec_ = partialmethod(QEventLoop.exec)
QThread.exec_ = partialmethod(QThread.exec)
Expand Down Expand Up @@ -198,9 +198,9 @@
PYSIDE6,
QT_VERSION,
QtBindingsNotFoundError,
_utils,
)
del TYPE_CHECKING
del contextlib
del partial, partialmethod
del parse
del possibly_static_exec, possibly_static_exec_
28 changes: 13 additions & 15 deletions qtpy/QtGui.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
"""Provides QtGui classes and functions."""
from functools import partial, partialmethod

from . import PYQT5, PYQT6, PYSIDE2, PYSIDE6
from ._utils import possibly_static_exec, to_q_point_f
from . import PYQT5, PYQT6, PYSIDE2, PYSIDE6, _utils

_QTOPENGL_NAMES = {
"QOpenGLBuffer",
Expand All @@ -31,18 +30,18 @@
}


def __getattr__(name):
def __getattr__(attr):
"""Custom getattr to chain and wrap errors due to missing optional deps."""
from ._utils import getattr_missing_optional_dep

raise getattr_missing_optional_dep(
name,
attr,
module_name=__name__,
optional_names=getattr(__getattr__, "_missing_optional_names", {}),
optional_names=getattr(__getattr__, "missing_optional_names", {}),
)


__getattr__._missing_optional_names = {}
__getattr__.missing_optional_names = {}


if PYQT5:
Expand All @@ -68,7 +67,7 @@ def __getattr__(name):
from PyQt6.QtOpenGL import *
except ImportError as error:
for name in _QTOPENGL_NAMES:
__getattr__._missing_optional_names[name] = {
__getattr__.missing_optional_names[name] = {
"name": "PyQt6.QtOpenGL",
"missing_package": "pyopengl",
"import_error": error,
Expand All @@ -91,7 +90,7 @@ def __getattr__(name):
*args,
**kwargs,
),
_function=possibly_static_exec,
_function=_utils.possibly_static_exec,
)
QTextDocument.print_ = partialmethod(QTextDocument.print)

Expand Down Expand Up @@ -125,7 +124,7 @@ def __getattr__(name):
from PySide6.QtOpenGL import *
except ImportError as error:
for name in _QTOPENGL_NAMES:
__getattr__._missing_optional_names[name] = {
__getattr__.missing_optional_names[name] = {
"name": "PySide6.QtOpenGL",
"missing_package": "pyopengl",
"import_error": error,
Expand All @@ -145,7 +144,7 @@ def __getattr__(name):
*args,
**kwargs,
),
_function=possibly_static_exec,
_function=_utils.possibly_static_exec,
)

if PYSIDE2 or PYSIDE6:
Expand Down Expand Up @@ -194,12 +193,12 @@ def movePositionPatched(
QNativeGestureEvent.globalX = lambda self: self.globalPos().x()
QNativeGestureEvent.globalY = lambda self: self.globalPos().y()
QNativeGestureEvent.globalPosition = partialmethod(
to_q_point_f,
_utils.to_q_point_f,
get_point_method="globalPos",
)
QEnterEvent.position = lambda self: self.localPos()
QEnterEvent.globalPosition = partialmethod(
to_q_point_f,
_utils.to_q_point_f,
get_point_method="globalPos",
)
QTabletEvent.position = lambda self: self.posF()
Expand All @@ -211,7 +210,7 @@ def movePositionPatched(
# nor `QHoverEvent.globalY` in the Qt5 docs.
QMouseEvent.position = lambda self: self.localPos()
QMouseEvent.globalPosition = partialmethod(
to_q_point_f,
_utils.to_q_point_f,
get_point_method="globalPos",
)

Expand Down Expand Up @@ -256,7 +255,6 @@ def movePositionPatched(
QDropEvent.posF = lambda self: self.position()

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, _utils
del partial, partialmethod
del possibly_static_exec, to_q_point_f
del _QTOPENGL_NAMES
56 changes: 24 additions & 32 deletions qtpy/QtWidgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,21 @@

from packaging.version import parse

from . import PYQT5, PYQT6, PYSIDE2, PYSIDE6
from . import QT_VERSION as _qt_version
from ._utils import (
add_action,
possibly_static_exec,
static_method_kwargs_wrapper,
)
from . import PYQT5, PYQT6, PYSIDE2, PYSIDE6, QT_VERSION, _utils


def __getattr__(name):
def __getattr__(attr):
"""Custom getattr to chain and wrap errors due to missing optional deps."""
from ._utils import getattr_missing_optional_dep

raise getattr_missing_optional_dep(
name,
attr,
module_name=__name__,
optional_names=__getattr__._missing_optional_names,
optional_names=__getattr__.missing_optional_names,
)


__getattr__._missing_optional_names = {}
__getattr__.missing_optional_names = {}


if PYQT5:
Expand All @@ -53,7 +47,7 @@ def __getattr__(name):
try:
from PyQt6.QtOpenGLWidgets import QOpenGLWidget
except ImportError as error:
__getattr__._missing_optional_names["QOpenGLWidget"] = {
__getattr__.missing_optional_names["QOpenGLWidget"] = {
"name": "PyQt6.QtOpenGLWidgets",
"missing_package": "pyopengl",
"import_error": error,
Expand All @@ -74,12 +68,12 @@ def __getattr__(name):
*args,
**kwargs,
),
_function=possibly_static_exec,
_function=_utils.possibly_static_exec,
)
QDialog.exec_ = partialmethod(QDialog.exec)
QMenu.exec_ = partialmethod(
lambda *args, _function, **kwargs: _function(QMenu, *args, **kwargs),
_function=possibly_static_exec,
_function=_utils.possibly_static_exec,
)
QLineEdit.getTextMargins = lambda self: (
self.textMargins().left(),
Expand Down Expand Up @@ -111,7 +105,7 @@ def __getattr__(name):
try:
from PySide6.QtOpenGLWidgets import QOpenGLWidget
except ImportError as error:
__getattr__._missing_optional_names["QOpenGLWidget"] = {
__getattr__.missing_optional_names["QOpenGLWidget"] = {
"name": "PySide6.QtOpenGLWidgets",
"missing_package": "pyopengl",
"import_error": error,
Expand All @@ -138,75 +132,73 @@ def __getattr__(name):
*args,
**kwargs,
),
_function=possibly_static_exec,
_function=_utils.possibly_static_exec,
)
QDialog.exec_ = partialmethod(QDialog.exec)
QMenu.exec_ = partialmethod(
lambda *args, _function, **kwargs: _function(QMenu, *args, **kwargs),
_function=possibly_static_exec,
_function=_utils.possibly_static_exec,
)

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


if PYSIDE2 or PYSIDE6:
# Make PySide2/6 `QFileDialog` static methods accept the `directory` kwarg as `dir`
QFileDialog.getExistingDirectory = static_method_kwargs_wrapper(
QFileDialog.getExistingDirectory = _utils.static_method_kwargs_wrapper(
QFileDialog.getExistingDirectory,
"directory",
"dir",
)
QFileDialog.getOpenFileName = static_method_kwargs_wrapper(
QFileDialog.getOpenFileName = _utils.static_method_kwargs_wrapper(
QFileDialog.getOpenFileName,
"directory",
"dir",
)
QFileDialog.getOpenFileNames = static_method_kwargs_wrapper(
QFileDialog.getOpenFileNames = _utils.static_method_kwargs_wrapper(
QFileDialog.getOpenFileNames,
"directory",
"dir",
)
QFileDialog.getSaveFileName = static_method_kwargs_wrapper(
QFileDialog.getSaveFileName = _utils.static_method_kwargs_wrapper(
QFileDialog.getSaveFileName,
"directory",
"dir",
)
else:
# Make PyQt5/6 `QFileDialog` static methods accept the `dir` kwarg as `directory`
QFileDialog.getExistingDirectory = static_method_kwargs_wrapper(
QFileDialog.getExistingDirectory = _utils.static_method_kwargs_wrapper(
QFileDialog.getExistingDirectory,
"dir",
"directory",
)
QFileDialog.getOpenFileName = static_method_kwargs_wrapper(
QFileDialog.getOpenFileName = _utils.static_method_kwargs_wrapper(
QFileDialog.getOpenFileName,
"dir",
"directory",
)
QFileDialog.getOpenFileNames = static_method_kwargs_wrapper(
QFileDialog.getOpenFileNames = _utils.static_method_kwargs_wrapper(
QFileDialog.getOpenFileNames,
"dir",
"directory",
)
QFileDialog.getSaveFileName = static_method_kwargs_wrapper(
QFileDialog.getSaveFileName = _utils.static_method_kwargs_wrapper(
QFileDialog.getSaveFileName,
"dir",
"directory",
)

# Make `addAction` compatible with Qt6 >= 6.3
if PYQT5 or PYSIDE2 or parse(_qt_version) < parse("6.3"):
QMenu.addAction = partialmethod(add_action, old_add_action=QMenu.addAction)
if PYQT5 or PYSIDE2 or parse(QT_VERSION) < parse("6.3"):
QMenu.addAction = partialmethod(_utils.add_action, old_add_action=QMenu.addAction)
QToolBar.addAction = partialmethod(
add_action,
_utils.add_action,
old_add_action=QToolBar.addAction,
)

# Clean up the namespace
del PYQT5, PYQT6, PYSIDE2, PYSIDE6
del _qt_version
del add_action, static_method_kwargs_wrapper, possibly_static_exec
del PYQT5, PYQT6, PYSIDE2, PYSIDE6, QT_VERSION, _utils
del parse
del partial, partialmethod

0 comments on commit 9fd5efb

Please sign in to comment.