From 46a62c16699ce47a88785bc8465ddf634fe05968 Mon Sep 17 00:00:00 2001 From: deeplow Date: Tue, 2 Aug 2022 12:20:28 +0100 Subject: [PATCH] fix type hints with commonn input/output filename Input_filename and output_filename could be None or Str. This lead to typing issues where the static analysis type hint tool could not check that the type colisions would not happen in runtime. So the logic was replaced by throwing a runtime exception if either of these valiables is ever used without first having been set. --- dangerzone/common.py | 26 ++++++++++++++++++++++++-- dangerzone/gui/main_window.py | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/dangerzone/common.py b/dangerzone/common.py index 8f48018..6d4d9cd 100644 --- a/dangerzone/common.py +++ b/dangerzone/common.py @@ -14,5 +14,27 @@ class Common(object): def __init__(self) -> None: # Name of input and out files - self.input_filename: Optional[str] = None - self.output_filename: Optional[str] = None + self._input_filename: Optional[str] = None + self._output_filename: Optional[str] = None + + @property + def input_filename(self) -> str: + if self._input_filename is None: + raise RuntimeError("Input filename has not been set yet.") + else: + return self._input_filename + + @input_filename.setter + def input_filename(self, filename: str) -> None: + self._input_filename = filename + + @property + def output_filename(self) -> str: + if self._output_filename is None: + raise RuntimeError("Output filename has not been set yet.") + else: + return self._output_filename + + @output_filename.setter + def output_filename(self, filename: str) -> None: + self._output_filename = filename \ No newline at end of file diff --git a/dangerzone/gui/main_window.py b/dangerzone/gui/main_window.py index fa15f03..75ab60c 100644 --- a/dangerzone/gui/main_window.py +++ b/dangerzone/gui/main_window.py @@ -471,7 +471,7 @@ class SettingsWidget(QtWidgets.QWidget): self.save_lineedit.setText(os.path.basename(self.common.output_filename)) def start_button_clicked(self) -> None: - if self.common.output_filename is None: + if self.save_checkbox.checkState() == QtCore.Qt.Unchecked: # If not saving, then save it to a temp file instead tmp = tempfile.mkstemp(suffix=".pdf", prefix="dangerzone_") self.common.output_filename = tmp[1]