From cca0fb750efe92ab0a8b2a08c5521fd47a794819 Mon Sep 17 00:00:00 2001 From: Markus Meissner Date: Mon, 18 Dec 2023 17:42:35 +0100 Subject: [PATCH] Fix: typing --- nitrokeyapp/add_secret_dialog.py | 1 + nitrokeyapp/error_dialog.py | 1 + nitrokeyapp/gui.py | 2 +- nitrokeyapp/logger.py | 2 +- nitrokeyapp/overview_tab.py | 1 + nitrokeyapp/qt_utils_mix_in.py | 7 +++++-- nitrokeyapp/secrets_tab/__init__.py | 9 ++++----- nitrokeyapp/ui_loader.py | 21 +++++++++++++++++---- nitrokeyapp/welcome_tab.py | 15 ++++++++------- 9 files changed, 39 insertions(+), 20 deletions(-) diff --git a/nitrokeyapp/add_secret_dialog.py b/nitrokeyapp/add_secret_dialog.py index 13c068e7..adc21b0e 100644 --- a/nitrokeyapp/add_secret_dialog.py +++ b/nitrokeyapp/add_secret_dialog.py @@ -21,6 +21,7 @@ def __init__(self, parent: Optional[QWidget] = None) -> None: QDialog.__init__(self, parent) QtUtilsMixIn.__init__(self) + # self.ui === self -> this tricks mypy due to monkey-patching self self.ui = self.load_ui("add_secret_dialog.ui", self) for kind in OtpKind: diff --git a/nitrokeyapp/error_dialog.py b/nitrokeyapp/error_dialog.py index e55d14c1..e8f9b3cc 100644 --- a/nitrokeyapp/error_dialog.py +++ b/nitrokeyapp/error_dialog.py @@ -16,6 +16,7 @@ def __init__(self, log_file: str, parent: Optional[QWidget] = None) -> None: self.log_file = log_file + # self.ui === self -> this tricks mypy due to monkey-patching self self.ui = self.load_ui("error_dialog.ui", self) self.button_save_log = QPushButton("Save Log File", self) diff --git a/nitrokeyapp/gui.py b/nitrokeyapp/gui.py index 149511e6..6bbd452e 100644 --- a/nitrokeyapp/gui.py +++ b/nitrokeyapp/gui.py @@ -83,8 +83,8 @@ def __init__(self, qt_app: QtWidgets.QApplication, log_file: str): self.trigger_handle_exception.connect(self.handle_exception) # loads main ui + # self.ui === self -> this tricks mypy due to monkey-patching self self.ui = self.load_ui("mainwindow.ui", self) - # self.setCentralWidget(self.ui) self.info_box = InfoBox( self.ui.information_frame, diff --git a/nitrokeyapp/logger.py b/nitrokeyapp/logger.py index f9ecd05e..e5d3d21e 100644 --- a/nitrokeyapp/logger.py +++ b/nitrokeyapp/logger.py @@ -29,7 +29,7 @@ def init_logging() -> Generator[str, None, None]: handlers = [handler] if log_to_console: - handlers.append(console_handler) + handlers.append(console_handler) # type: ignore logging.basicConfig(format=log_format, level=logging.DEBUG, handlers=handlers) diff --git a/nitrokeyapp/overview_tab.py b/nitrokeyapp/overview_tab.py index 62355c02..86107e6f 100644 --- a/nitrokeyapp/overview_tab.py +++ b/nitrokeyapp/overview_tab.py @@ -20,6 +20,7 @@ def __init__(self, info_box: InfoBox, parent: Optional[QWidget] = None) -> None: self.data: Optional[DeviceData] = None self.info_box = info_box + # self.ui === self -> this tricks mypy due to monkey-patching self self.ui = self.load_ui("overview_tab.ui", self) self.collapse(self.ui.more_options_frame, self.ui.more_options_btn) diff --git a/nitrokeyapp/qt_utils_mix_in.py b/nitrokeyapp/qt_utils_mix_in.py index 9f13ac1d..01dd0151 100644 --- a/nitrokeyapp/qt_utils_mix_in.py +++ b/nitrokeyapp/qt_utils_mix_in.py @@ -1,5 +1,5 @@ from pathlib import Path -from typing import Optional, Type, TypeVar +from typing import Any, Optional, Type, TypeVar from PySide6 import QtGui, QtWidgets from PySide6.QtCore import QDir, QObject, QSize @@ -18,7 +18,10 @@ def __init__(self) -> None: assert isinstance(self, QObject) @staticmethod - def load_ui(filename: str, base_instance: Optional[QObject] = None) -> bool: + def load_ui( + filename: str, base_instance: Optional[QtWidgets.QWidget] = None + ) -> Any: + # returning `Any` to avoid `mypy` going crazy due to monkey-patching loader = UiLoader(base_instance, customWidgets=None) p_dir = (Path(__file__).parent / "ui").absolute() loader.setWorkingDirectory(QDir(p_dir.as_posix())) diff --git a/nitrokeyapp/secrets_tab/__init__.py b/nitrokeyapp/secrets_tab/__init__.py index 1085dbdc..07e12b29 100644 --- a/nitrokeyapp/secrets_tab/__init__.py +++ b/nitrokeyapp/secrets_tab/__init__.py @@ -72,14 +72,13 @@ def __init__(self, parent: Optional[QWidget] = None) -> None: self.clipboard = QGuiApplication.clipboard() self.originalText = self.clipboard.text() + # self.ui === self -> this tricks mypy due to monkey-patching self self.ui = self.load_ui("secrets_tab.ui", self) - # breakpoint() - # print(self.pageCompatible) labels = [ - self.labelName, - self.labelAlgorithm, - self.labelOtp, + self.ui.labelName, + self.ui.labelAlgorithm, + self.ui.labelOtp, ] max_width = max([label.width() for label in labels]) for label in labels: diff --git a/nitrokeyapp/ui_loader.py b/nitrokeyapp/ui_loader.py index e4321b63..07e286c6 100644 --- a/nitrokeyapp/ui_loader.py +++ b/nitrokeyapp/ui_loader.py @@ -1,3 +1,6 @@ +from typing import Dict, Optional + +from PySide6 import QtWidgets from PySide6.QtUiTools import QUiLoader @@ -12,7 +15,11 @@ class UiLoader(QUiLoader): This mimics the behaviour of :func:`PyQt4.uic.loadUi`. """ - def __init__(self, baseinstance, customWidgets=None): + def __init__( + self, + baseinstance: Optional[QtWidgets.QWidget], + customWidgets: Optional[Dict[str, QtWidgets.QWidget]] = None, + ): """ Create a loader for the given ``baseinstance``. The user interface is created in ``baseinstance``, which must be an @@ -26,9 +33,14 @@ def __init__(self, baseinstance, customWidgets=None): QUiLoader.__init__(self, baseinstance) self.baseinstance = baseinstance - self.customWidgets = customWidgets + self.customWidgets = customWidgets or {} - def createWidget(self, class_name, parent=None, name=""): + def createWidget( + self, + class_name: str, + parent: Optional[QtWidgets.QWidget] = None, + name: str = "", + ) -> QtWidgets.QWidget: """ Function that is called for each widget defined in ui file, overridden here to populate baseinstance instead. @@ -44,7 +56,8 @@ def createWidget(self, class_name, parent=None, name=""): widget = QUiLoader.createWidget(self, class_name, parent, name) else: try: - widget = self.customWidgets[class_name](parent) + # @fixme? in fact QWidget is callable + widget = self.customWidgets[class_name](parent) # type: ignore except (TypeError, KeyError): raise Exception( diff --git a/nitrokeyapp/welcome_tab.py b/nitrokeyapp/welcome_tab.py index 5cc23f8e..fdaa51ae 100644 --- a/nitrokeyapp/welcome_tab.py +++ b/nitrokeyapp/welcome_tab.py @@ -18,10 +18,11 @@ def __init__(self, parent: Optional[QWidget], log_file: str) -> None: self.log_file = log_file - self.load_ui("welcome_tab.ui", self) - self.buttonSaveLog.pressed.connect(self.save_log) - self.VersionNr.setText(__version__) - self.CheckUpdate.pressed.connect(self.check_update) + # self.ui === self -> this tricks mypy due to monkey-patching self + self.ui = self.load_ui("welcome_tab.ui", self) + self.ui.buttonSaveLog.pressed.connect(self.save_log) + self.ui.VersionNr.setText(__version__) + self.ui.CheckUpdate.pressed.connect(self.check_update) def check_update(self) -> None: self.c_version = __version__ @@ -35,14 +36,14 @@ def check_update(self) -> None: self.n_version_v = Version.from_str(self.n_version) if Version.__lt__(self.c_version_v, self.n_version_v): - self.CheckUpdate.setText("update available") - self.CheckUpdate.pressed.connect( + self.ui.CheckUpdate.setText("update available") + self.ui.CheckUpdate.pressed.connect( lambda: webbrowser.open( "https://github.com/Nitrokey/nitrokey-app2/releases" ) ) else: - self.CheckUpdate.setText("App is up to date") + self.ui.CheckUpdate.setText("App is up to date") @Slot() def save_log(self) -> None: