Skip to content

Commit

Permalink
Fix: typing
Browse files Browse the repository at this point in the history
  • Loading branch information
daringer committed Dec 18, 2023
1 parent 9f70a2d commit cca0fb7
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 20 deletions.
1 change: 1 addition & 0 deletions nitrokeyapp/add_secret_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions nitrokeyapp/error_dialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion nitrokeyapp/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion nitrokeyapp/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions nitrokeyapp/overview_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions nitrokeyapp/qt_utils_mix_in.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()))
Expand Down
9 changes: 4 additions & 5 deletions nitrokeyapp/secrets_tab/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 17 additions & 4 deletions nitrokeyapp/ui_loader.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from typing import Dict, Optional

from PySide6 import QtWidgets
from PySide6.QtUiTools import QUiLoader


Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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(
Expand Down
15 changes: 8 additions & 7 deletions nitrokeyapp/welcome_tab.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand All @@ -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:
Expand Down

0 comments on commit cca0fb7

Please sign in to comment.