Skip to content

Commit

Permalink
add or improve typing for dicts
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Dec 16, 2024
1 parent e843098 commit 5aaf737
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 79 deletions.
2 changes: 1 addition & 1 deletion pyglossary/plugin_prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/appledict_bin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
15 changes: 9 additions & 6 deletions pyglossary/plugins/appledict_bin/appledict_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from __future__ import annotations

from dataclasses import dataclass
from typing import Any

__all__ = ["AppleDictProperties", "from_metadata"]

Expand Down Expand Up @@ -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 = [
Expand Down
6 changes: 3 additions & 3 deletions pyglossary/plugins/iupac_goldbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/jmdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
5 changes: 1 addition & 4 deletions pyglossary/text_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/ui/argparse_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pyglossary/ui/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 9 additions & 8 deletions pyglossary/ui/ui_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -234,7 +235,7 @@ def reverseLoop(
) -> None:
from pyglossary.reverse import reverseGlossary

reverseKwArgs = {}
reverseKwArgs: dict[str, Any] = {}
for key in (
"words",
"matchWord",
Expand Down Expand Up @@ -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 = {}
Expand Down
38 changes: 15 additions & 23 deletions pyglossary/ui/ui_cmd_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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
Expand All @@ -1179,3 +1169,5 @@ def run( # noqa: PLR0913

if self.config != self.savedConfig and confirm("Save Config?"):
self.saveConfig()

return True
17 changes: 7 additions & 10 deletions pyglossary/ui/ui_gtk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down
17 changes: 7 additions & 10 deletions pyglossary/ui/ui_gtk4.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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):
Expand Down
15 changes: 6 additions & 9 deletions pyglossary/ui/ui_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5aaf737

Please sign in to comment.