diff --git a/pyglossary/plugin_prop.py b/pyglossary/plugin_prop.py index 3e896a269..da36af48e 100644 --- a/pyglossary/plugin_prop.py +++ b/pyglossary/plugin_prop.py @@ -45,7 +45,7 @@ def optionsPropFromDict( optionsPropDict: dict[str, Any], ) -> dict[str, Option]: - props = {} + props: dict[str, Option] = {} for name, propDict in optionsPropDict.items(): try: prop = optionFromDict(propDict) diff --git a/pyglossary/plugins/appledict_bin/__init__.py b/pyglossary/plugins/appledict_bin/__init__.py index caa155007..16dad0630 100644 --- a/pyglossary/plugins/appledict_bin/__init__.py +++ b/pyglossary/plugins/appledict_bin/__init__.py @@ -454,7 +454,7 @@ def convertEntryBytesToXml( return entryRoot def readEntryIds(self) -> None: - titleById = {} + titleById: dict[str, str] = {} for entryBytesTmp, _ in self.yieldEntryBytes( self._file, self._properties, diff --git a/pyglossary/plugins/appledict_bin/appledict_properties.py b/pyglossary/plugins/appledict_bin/appledict_properties.py index 80272a029..2359316d7 100644 --- a/pyglossary/plugins/appledict_bin/appledict_properties.py +++ b/pyglossary/plugins/appledict_bin/appledict_properties.py @@ -15,6 +15,7 @@ from __future__ import annotations from dataclasses import dataclass +from typing import Any __all__ = ["AppleDictProperties", "from_metadata"] @@ -49,15 +50,17 @@ class AppleDictProperties: css_name: str | None -def from_metadata(metadata: dict) -> AppleDictProperties: +def from_metadata(metadata: dict[str, Any]) -> AppleDictProperties: format_version: int = metadata.get("IDXDictionaryVersion", -1) - dictionaryIndexes: list[dict] | None = metadata.get("IDXDictionaryIndexes") + dictionaryIndexes: list[dict[str, Any]] | None = metadata.get( + "IDXDictionaryIndexes", + ) if dictionaryIndexes: - key_text_metadata = dictionaryIndexes[0] - body_metadata = dictionaryIndexes[2] + key_text_metadata: dict[str, Any] = dictionaryIndexes[0] + body_metadata: dict[str, Any] = dictionaryIndexes[2] else: - key_text_metadata = {} - body_metadata = {} + key_text_metadata: dict[str, Any] = {} + body_metadata: dict[str, Any] = {} key_text_data_fields = key_text_metadata.get("IDXIndexDataFields", {}) key_text_variable_fields = [ diff --git a/pyglossary/plugins/iupac_goldbook.py b/pyglossary/plugins/iupac_goldbook.py index b7b52be84..18d79ed5a 100644 --- a/pyglossary/plugins/iupac_goldbook.py +++ b/pyglossary/plugins/iupac_goldbook.py @@ -60,7 +60,7 @@ def __init__(self, glos: GlossaryType) -> None: self._filename = "" self._file = None self._fileSize = 0 - self._termByCode = None + self._termByCode: dict[str, str] = {} def __len__(self) -> int: return 0 @@ -71,7 +71,7 @@ def close(self) -> None: self._file = None self._filename = "" self._fileSize = 0 - self._termByCode = None + self._termByCode = {} def open(self, filename: str) -> None: try: @@ -100,7 +100,7 @@ def open(self, filename: str) -> None: events=("end",), tag="entry", ) - termByCode = {} + termByCode: dict[str, str] = {} for _, elem in context: termE = elem.find("./term") if termE is None: diff --git a/pyglossary/plugins/jmdict.py b/pyglossary/plugins/jmdict.py index 33de9e31d..e5f88f31c 100644 --- a/pyglossary/plugins/jmdict.py +++ b/pyglossary/plugins/jmdict.py @@ -240,7 +240,7 @@ def br() -> Element: continue sentList.append(sentElem.text) with hf.element("li"): - style = {} + style: dict[str, str] = {} if self._example_color: style["color"] = self._example_color with hf.element("font", attrib=style): diff --git a/pyglossary/text_writer.py b/pyglossary/text_writer.py index 66ad82cfc..fd7389562 100644 --- a/pyglossary/text_writer.py +++ b/pyglossary/text_writer.py @@ -53,10 +53,7 @@ def __init__( self._entryFmt = entryFmt self._writeInfo = writeInfo - - if not outInfoKeysAliasDict: - outInfoKeysAliasDict = {} - self._outInfoKeysAliasDict = outInfoKeysAliasDict + self._outInfoKeysAliasDict = outInfoKeysAliasDict or {} # TODO: replace outInfoKeysAliasDict arg with a func? # TODO: use @property setters diff --git a/pyglossary/ui/argparse_main.py b/pyglossary/ui/argparse_main.py index 2774b45bd..21aaf1059 100644 --- a/pyglossary/ui/argparse_main.py +++ b/pyglossary/ui/argparse_main.py @@ -308,7 +308,7 @@ def configFromArgs( args: argparse.Namespace, log: logging.Logger, ) -> dict[str, Any]: - config = {} + config: dict[str, Any] = {} for key, option in UIBase.configDefDict.items(): if not option.hasFlag: continue diff --git a/pyglossary/ui/main.py b/pyglossary/ui/main.py index 827d4141e..1e87003c4 100644 --- a/pyglossary/ui/main.py +++ b/pyglossary/ui/main.py @@ -207,13 +207,13 @@ def mainPrepare() -> tuple[bool, MainPrepareResult | None]: config.update(configFromArgs(args, log)) logHandler.config = config - convertOptions = {} + convertOptions: dict[str, Any] = {} for key in convertOptionsKeys: value = getattr(args, key, None) if value is not None: convertOptions[key] = value - infoOverride = {} + infoOverride: dict[str, str] = {} for key, validate in infoOverrideSpec: value = getattr(args, key, None) if value is None: diff --git a/pyglossary/ui/ui_cmd.py b/pyglossary/ui/ui_cmd.py index f59c59ba8..9d0355bf4 100644 --- a/pyglossary/ui/ui_cmd.py +++ b/pyglossary/ui/ui_cmd.py @@ -118,13 +118,14 @@ def printHelp() -> None: print(text) -def parseFormatOptionsStr(st: str) -> dict[str, Any] | None: +# TODO: raise exception instead of returning None +def parseFormatOptionsStr(st: str) -> dict[str, str] | None: """Prints error and returns None if failed to parse one option.""" st = st.strip() if not st: return {} - opt = {} + opt: dict[str, str] = {} parts = st.split(";") for part in parts: if not part: @@ -234,7 +235,7 @@ def reverseLoop( ) -> None: from pyglossary.reverse import reverseGlossary - reverseKwArgs = {} + reverseKwArgs: dict[str, Any] = {} for key in ( "words", "matchWord", @@ -270,11 +271,11 @@ def run( # noqa: PLR0912, PLR0913 inputFormat: str = "", outputFormat: str = "", reverse: bool = False, - config: dict | None = None, - readOptions: dict | None = None, - writeOptions: dict | None = None, - convertOptions: dict | None = None, - glossarySetAttrs: dict | None = None, + config: dict[str, Any] | None = None, + readOptions: dict[str, Any] | None = None, + writeOptions: dict[str, Any] | None = None, + convertOptions: dict[str, Any] | None = None, + glossarySetAttrs: dict[str, Any] | None = None, ) -> bool: if config is None: config = {} diff --git a/pyglossary/ui/ui_cmd_interactive.py b/pyglossary/ui/ui_cmd_interactive.py index 7c9d72e8a..48f93cd91 100644 --- a/pyglossary/ui/ui_cmd_interactive.py +++ b/pyglossary/ui/ui_cmd_interactive.py @@ -56,7 +56,7 @@ join, relpath, ) -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Any if TYPE_CHECKING: from collections.abc import Iterable @@ -274,7 +274,7 @@ def __init__( self._outputFilename = "" self._inputFormat = "" self._outputFormat = "" - self.config = {} + self.config: dict[str, Any] = {} self._readOptions = {} self._writeOptions = {} self._convertOptions = {} @@ -1122,40 +1122,30 @@ def run( # noqa: PLR0913 inputFormat: str = "", outputFormat: str = "", reverse: bool = False, - config: dict | None = None, - readOptions: dict | None = None, - writeOptions: dict | None = None, - convertOptions: dict | None = None, - glossarySetAttrs: dict | None = None, - ): + config: dict[str, Any] | None = None, + readOptions: dict[str, Any] | None = None, + writeOptions: dict[str, Any] | None = None, + convertOptions: dict[str, Any] | None = None, + glossarySetAttrs: dict[str, Any] | None = None, + ) -> bool: if reverse: raise NotImplementedError("Reverse is not implemented in this UI") - if config is None: - config = {} - if readOptions is None: - readOptions = {} - if writeOptions is None: - writeOptions = {} - if convertOptions is None: - convertOptions = {} - if glossarySetAttrs is None: - glossarySetAttrs = {} self._inputFilename = inputFilename self._outputFilename = outputFilename self._inputFormat = inputFormat self._outputFormat = outputFormat - self._readOptions = readOptions - self._writeOptions = writeOptions - self._convertOptions = convertOptions - self._glossarySetAttrs = glossarySetAttrs + self._readOptions = readOptions or {} + self._writeOptions = writeOptions or {} + self._convertOptions = convertOptions or {} + self._glossarySetAttrs = glossarySetAttrs or {} if not self._progressbar: self._glossarySetAttrs["progressbar"] = False self.loadConfig() self.savedConfig = self.config.copy() - self.config = config + self.config = config or {} del inputFilename, outputFilename, inputFormat, outputFormat del config, readOptions, writeOptions, convertOptions @@ -1179,3 +1169,5 @@ def run( # noqa: PLR0913 if self.config != self.savedConfig and confirm("Save Config?"): self.saveConfig() + + return True diff --git a/pyglossary/ui/ui_gtk.py b/pyglossary/ui/ui_gtk.py index bd9c0fcf5..05ba5a97b 100644 --- a/pyglossary/ui/ui_gtk.py +++ b/pyglossary/ui/ui_gtk.py @@ -569,7 +569,7 @@ def valueCellClicked(self, path, forceMenu=False) -> bool: def getOptionsValues(self): model = self.treev.get_model() - optionsValues = {} + optionsValues: dict[str, Any] = {} for row in model: if not row[0]: # not enable continue @@ -1385,15 +1385,12 @@ def run( # noqa: PLR0913 inputFormat: str = "", outputFormat: str = "", reverse: bool = False, - config: dict | None = None, - readOptions: dict | None = None, - writeOptions: dict | None = None, - convertOptions: dict | None = None, - glossarySetAttrs: dict | None = None, + config: dict[str, Any] | None = None, + readOptions: dict[str, Any] | None = None, + writeOptions: dict[str, Any] | None = None, + convertOptions: dict[str, Any] | None = None, + glossarySetAttrs: dict[str, Any] | None = None, ): - if glossarySetAttrs is None: - glossarySetAttrs = {} - self.config = config if inputFilename: @@ -1418,7 +1415,7 @@ def run( # noqa: PLR0913 if convertOptions: log.debug(f"Using {convertOptions=}") - self._glossarySetAttrs = glossarySetAttrs + self._glossarySetAttrs = glossarySetAttrs or {} self.convertInputEntry.grab_focus() gtk.Dialog.present(self) diff --git a/pyglossary/ui/ui_gtk4.py b/pyglossary/ui/ui_gtk4.py index 2848733de..81d62156b 100644 --- a/pyglossary/ui/ui_gtk4.py +++ b/pyglossary/ui/ui_gtk4.py @@ -612,7 +612,7 @@ def valueCellClicked(self, path, forceMenu=False) -> bool: def getOptionsValues(self): model = self.treev.get_model() - optionsValues = {} + optionsValues: dict[str, Any] = {} for row in model: if not row[0]: # not enable continue @@ -1461,15 +1461,12 @@ def run( # noqa: PLR0913 inputFormat: str = "", outputFormat: str = "", reverse: bool = False, - config: dict | None = None, - readOptions: dict | None = None, - writeOptions: dict | None = None, - convertOptions: dict | None = None, - glossarySetAttrs: dict | None = None, + config: dict[str, Any] | None = None, + readOptions: dict[str, Any] | None = None, + writeOptions: dict[str, Any] | None = None, + convertOptions: dict[str, Any] | None = None, + glossarySetAttrs: dict[str, Any] | None = None, ): - if glossarySetAttrs is None: - glossarySetAttrs = {} - self.config = config if inputFilename: @@ -1494,7 +1491,7 @@ def run( # noqa: PLR0913 if convertOptions: log.debug(f"Using {convertOptions=}") - self._glossarySetAttrs = glossarySetAttrs + self._glossarySetAttrs = glossarySetAttrs or {} self.present() def exitApp(self): diff --git a/pyglossary/ui/ui_tk.py b/pyglossary/ui/ui_tk.py index ba07668b5..6b0019a55 100644 --- a/pyglossary/ui/ui_tk.py +++ b/pyglossary/ui/ui_tk.py @@ -1477,15 +1477,12 @@ def run( # noqa: PLR0913 inputFormat: str = "", outputFormat: str = "", reverse: bool = False, - config: dict | None = None, - readOptions: dict | None = None, - writeOptions: dict | None = None, - convertOptions: dict | None = None, - glossarySetAttrs: dict | None = None, + config: dict[str, Any] | None = None, + readOptions: dict[str, Any] | None = None, + writeOptions: dict[str, Any] | None = None, + convertOptions: dict[str, Any] | None = None, + glossarySetAttrs: dict[str, Any] | None = None, ): - if glossarySetAttrs is None: - glossarySetAttrs = {} - self.config = config if inputFilename: @@ -1524,7 +1521,7 @@ def run( # noqa: PLR0913 if convertOptions: log.info(f"Using {convertOptions=}") - self._glossarySetAttrs = glossarySetAttrs + self._glossarySetAttrs = glossarySetAttrs or {} # inputFilename and readOptions are for DB Editor # which is not implemented