From e76132a2f01f5b8c61feed23aa401c69fd950918 Mon Sep 17 00:00:00 2001 From: deeplow Date: Thu, 21 Jul 2022 16:09:18 +0100 Subject: [PATCH] add typed hints to Settings dictionary MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Originally tied to implment following PEP 589 [1] – TypedDict: Type Hints for Dictionaries with a Fixed Set of Keys for the Settings dict. But this quickly turned out to very challenging without redoing the code. So we opted instead for using the Any keyword. [1]: https://peps.python.org/pep-0589/ --- dangerzone/settings.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/dangerzone/settings.py b/dangerzone/settings.py index 380778f..9451616 100644 --- a/dangerzone/settings.py +++ b/dangerzone/settings.py @@ -1,21 +1,22 @@ import json import logging import os -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any, Dict, Optional log = logging.getLogger(__name__) if TYPE_CHECKING: from .global_common import GlobalCommon - class Settings: + settings: Dict[str, Any] + def __init__(self, global_common: "GlobalCommon") -> None: self.global_common = global_common self.settings_filename = os.path.join( self.global_common.appdata_path, "settings.json" ) - self.default_settings = { + self.default_settings: Dict[str, Any] = { "save": True, "ocr": True, "ocr_language": "English", @@ -25,14 +26,16 @@ class Settings: self.load() - def get(self, key: str): + def get(self, key: str) -> Any: return self.settings[key] - def set(self, key: str, val) -> None: + def set(self, key: str, val: Any) -> None: self.settings[key] = val def load(self) -> None: if os.path.isfile(self.settings_filename): + self.settings = self.default_settings + # If the settings file exists, load it try: with open(self.settings_filename, "r") as settings_file: