Skip to content

Commit

Permalink
add missing type annotations or ignore ruff error
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Dec 13, 2024
1 parent 9593f51 commit 6d817a3
Show file tree
Hide file tree
Showing 22 changed files with 67 additions and 48 deletions.
2 changes: 1 addition & 1 deletion pyglossary/entry.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ def isData(cls) -> bool:
def getRawEntrySortKey(
key: Callable[[bytes], Any],
) -> Callable[[RawEntryType], Any]:
def newKey(x: RawEntryType) -> Any:
def newKey(x: RawEntryType) -> Any: # noqa: ANN401
# x is rawEntry, so x[2:] is list[bytes]: list of words in bytes
return key([b.decode("utf-8") for b in x[2:]]) # type: ignore

Expand Down
2 changes: 1 addition & 1 deletion pyglossary/entry_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def xdxf_transform(text: str) -> str:
return _xdxfTr.transformByInnerString(text) # type: ignore


def getHtmlDefi(entry: EntryType):
def getHtmlDefi(entry: EntryType) -> str:
if entry.defiFormat == "m":
return f"<pre>{entry.defi}</pre>"
if entry.defiFormat == "x":
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/glossary_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def progressbar(self, enabled: bool) -> None:

def progressInit(
self,
*args, # noqa: ANN003
*args, # noqa: ANN002
) -> None:
if self._ui and self._progressbar:
self._ui.progressInit(*args)
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/glossary_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def addCleanupPath(self, path: str) -> None: ...
class GlossaryExtendedType(GlossaryType, typing.Protocol):
def progressInit(
self,
*args,
*args, # noqa: ANN002
) -> None: ...

def progress(self, pos: int, total: int, unit: str = "entries") -> None: ...
Expand Down
24 changes: 13 additions & 11 deletions pyglossary/glossary_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ def preventDuplicateWords(self) -> None:

# self._iter = mergeHtmlEntriesWithSameHeadword(self._iter)

def mergeEntriesWithSameHeadwordPlaintext(self):
def mergeEntriesWithSameHeadwordPlaintext(self) -> None:
"""
Merge consequtive entries that have the same word list.
Expand Down Expand Up @@ -649,11 +649,12 @@ def newDataEntry(self, fname: str, data: bytes) -> EntryType:
# return os.access(dirPath, os.W_OK)
# return os.access(os.path.dirname(dirPath), os.W_OK)

# TODO: add ReaderType with Protocol
def _createReader(
self,
format: str,
options: dict[str, Any],
) -> Any:
) -> Any: # noqa: ANN401
readerClass = self.plugins[format].readerClass
if readerClass is None:
raise ReadError("_createReader: readerClass is None")
Expand Down Expand Up @@ -694,7 +695,7 @@ def _validateReadoptions(
)
del options[key]

def _openReader(self, reader: Any, filename: str):
def _openReader(self, reader: Any, filename: str) -> None: # noqa: ANN401
# reader.open returns "Iterator[tuple[int, int]] | None"
progressbar: bool = self.progressbar
try:
Expand All @@ -721,7 +722,7 @@ def directRead(
self,
filename: str,
format: str = "",
**options,
**options, # noqa: ANN003
) -> bool:
self._setTmpDataDir(filename)
return self._read(
Expand All @@ -736,7 +737,7 @@ def _read(
filename: str,
format: str = "",
direct: bool = False,
**options,
**options, # noqa: ANN003
) -> bool:
filename = os.path.abspath(filename)
###
Expand Down Expand Up @@ -781,7 +782,7 @@ def _read(

return True

def loadReader(self, reader: Any) -> None:
def loadReader(self, reader: Any) -> None: # noqa: ANN401
"""
Iterate over `reader` object and loads the whole data into self._data
must call `reader.open(filename)` before calling this function.
Expand All @@ -800,11 +801,12 @@ def loadReader(self, reader: Any) -> None:
core.trace(log, f"Loaded {len(self._data)} entries")
showMemoryUsage()

# TODO: add WriterType with Protocol
def _createWriter(
self,
format: str,
options: dict[str, Any],
) -> Any:
) -> Any: # noqa: ANN401
validOptions = self.formatsWriteOptions.get(format)
if validOptions is None:
raise WriteError(f"No write support for {format!r} format")
Expand All @@ -828,7 +830,7 @@ def write(
self,
filename: str,
format: str,
**kwargs,
**kwargs, # noqa: ANN003
) -> str:
"""
Write to a given glossary file, with given format (optional).
Expand Down Expand Up @@ -895,9 +897,9 @@ def _writeEntries(

@staticmethod
def _openWriter(
writer: Any,
writer: Any, # noqa: ANN401
filename: str,
):
) -> None:
try:
writer.open(filename)
except (FileNotFoundError, LookupError) as e:
Expand All @@ -908,7 +910,7 @@ def _write(
filename: str,
format: str,
sort: bool = False,
**options,
**options, # noqa: ANN003
) -> str:
filename = os.path.abspath(filename)

Expand Down
6 changes: 3 additions & 3 deletions pyglossary/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


class _Formatter(logging.Formatter):
def __init__(self, *args, **kwargs) -> None: # noqa: ANN101
def __init__(self, *args, **kwargs) -> None: # noqa: ANN002, ANN003
logging.Formatter.__init__(self, *args, **kwargs)
self.fill: Callable[[str], str] | None = None

Expand Down Expand Up @@ -65,7 +65,7 @@ class Logger(logging.Logger):
"All", # "Not-Set",
)

def __init__(self, *args) -> None: # noqa: ANN101
def __init__(self, *args) -> None: # noqa: ANN101, ANN002
logging.Logger.__init__(self, *args)
self._verbosity = 3
self._timeEnable = False
Expand All @@ -80,7 +80,7 @@ def getVerbosity(self) -> int:
def trace(self, msg: str) -> None:
self.log(TRACE, msg)

def pretty(self, data: Any, header: str = "") -> None:
def pretty(self, data: Any, header: str = "") -> None: # noqa: ANN401
from pprint import pformat

self.debug(header + pformat(data))
Expand Down
8 changes: 4 additions & 4 deletions pyglossary/option.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def evaluate(cls, raw: str) -> tuple[Any, bool]:
return None, True
return raw, True

def validate(self, value: Any) -> bool:
def validate(self, value: Any) -> bool: # noqa: ANN401
if not self.customValue:
if not self.values:
log.error(
Expand Down Expand Up @@ -191,7 +191,7 @@ def __init__(
**kwargs,
)

def validate(self, value: Any) -> bool:
def validate(self, value: Any) -> bool: # noqa: ANN401
if not self.customValue:
if not self.values:
log.error(
Expand Down Expand Up @@ -359,7 +359,7 @@ def evaluate(

@Option.register
class ListOption(Option):
def __init__(self, **kwargs) -> None:
def __init__(self, **kwargs) -> None: # noqa: ANN003
Option.__init__(
self,
typ="list",
Expand Down Expand Up @@ -527,7 +527,7 @@ def toDict(self) -> dict[str, Any]:
del data["customValue"]
return data

def __init__(self, **kwargs) -> None:
def __init__(self, **kwargs) -> None: # noqa: ANN003
Option.__init__(
self,
typ="str",
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugin_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ def detectOutputFormat( # noqa: PLR0912, PLR0913, C901
format: str = "",
inputFilename: str = "",
addExt: bool = False,
**kwargs,
**kwargs, # noqa: ANN003
) -> DetectedFormat:
from os.path import splitext

Expand Down
8 changes: 4 additions & 4 deletions pyglossary/plugin_prop.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def fromDict(
return self

@classmethod
def fromModule(cls: type, mod: Any) -> PluginProp:
def fromModule(cls: type, mod: Any) -> PluginProp: # noqa: ANN401
self = cls()
self._mod = mod
self._Reader = None
Expand Down Expand Up @@ -199,7 +199,7 @@ def enable(self) -> bool:
return self._enable

@property
def module(self) -> Any:
def module(self) -> Any: # noqa: ANN401
if self._mod is not None:
return self._mod
moduleName = self._moduleName
Expand Down Expand Up @@ -376,7 +376,7 @@ def writeDepends(self) -> dict[str, str]:
self._writeDepends = getattr(self.writerClass, "depends", {})
return self._writeDepends

def checkModule(self, module) -> None:
def checkModule(self, module: Any) -> None: # noqa: ANN401
name = self.name

if hasattr(module, "write"):
Expand Down Expand Up @@ -434,7 +434,7 @@ def checkModule(self, module) -> None:
]

# only run this on CI to do extra validation
def checkModuleMore(self, module) -> None:
def checkModuleMore(self, module: Any) -> None:
name = self.name
if not hasattr(module, "__all__"):
raise PluginCheckError(f"Please add __all__ to plugin {name!r}")
Expand Down
12 changes: 8 additions & 4 deletions pyglossary/plugins/appledict/_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@

import logging
import re
from typing import Any
from typing import TYPE_CHECKING, Any
from xml.sax.saxutils import quoteattr, unescape

if TYPE_CHECKING:
import bs4 as BeautifulSoup
import bs4.element

from pyglossary.text_utils import toStr

__all__ = ["prepare_content"]
Expand Down Expand Up @@ -117,7 +121,7 @@ def prepare_content_without_soup(
return content # noqa: RET504


def _prepare_href(tag) -> None:
def _prepare_href(tag: bs4.element.Tag) -> None:
href = tag["href"]
href = cleanup_link_target(href)

Expand All @@ -136,7 +140,7 @@ def _prepare_href(tag) -> None:
tag["href"] = f"x-dictionary:d:{href}"


def _prepare_onclick(soup) -> None:
def _prepare_onclick(soup: BeautifulSoup.BeautifulSoup) -> None:
for thumb in soup.find_all("div", "pic_thumb"):
thumb["onclick"] = (
'this.setAttribute("style", "display:none"); '
Expand Down Expand Up @@ -169,7 +173,7 @@ def _prepare_onclick(soup) -> None:
def prepare_content_with_soup( # noqa: PLR0912
title: str | None,
body: str,
BeautifulSoup: Any,
BeautifulSoup: BeautifulSoup,
) -> str:
soup = BeautifulSoup.BeautifulSoup(body, features="lxml")
# difference between "lxml" and "html.parser"
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 @@ -646,7 +646,7 @@ def readResFile(self, fname: str, fpath: str, ext: str) -> EntryType:
data = substituteAppleCSS(data)
return self._glos.newDataEntry(fname, data)

def fixResFilename(self, fname: str, relPath: str):
def fixResFilename(self, fname: str, relPath: str) -> str:
if fname == self._cssName:
fname = "style.css"
if relPath:
Expand Down
4 changes: 2 additions & 2 deletions pyglossary/plugins/babylon_bgl/bgl_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def __init__(
self,
fileobj: io.IOBase | None = None,
closeFileobj: bool = False,
**kwargs,
**kwargs, # noqa: ANN003
) -> None:
GzipFile.__init__(self, fileobj=fileobj, **kwargs)
self.closeFileobj = closeFileobj
Expand Down Expand Up @@ -1060,7 +1060,7 @@ def charReferencesStat(self, b_text: bytes, encoding: str) -> None:
pass

@staticmethod
def decodeCharsetTagsBabylonReference(b_text: bytes, b_text2: bytes):
def decodeCharsetTagsBabylonReference(b_text: bytes, b_text2: bytes) -> str:
b_refs = b_text2.split(b";")
add_text = ""
for i_ref, b_ref in enumerate(b_refs):
Expand Down
4 changes: 2 additions & 2 deletions pyglossary/plugins/dsl/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def open(
if self._abbrev:
self.loadAbbrevFile()

def loadAbbrevFile(self):
def loadAbbrevFile(self) -> None:
baseName, _ = splitext(self._filename)
abbrevName = baseName + "_abrv.dsl"
if not isfile(abbrevName):
Expand Down Expand Up @@ -389,7 +389,7 @@ def parseEntryBlock( # noqa: PLR0912 Too many branches (14 > 12)
subglos_list: list[tuple[str, str]] = []
subglos_key, subglos_text = "", ""

def add_subglos():
def add_subglos() -> None:
nonlocal main_text, subglos_key, subglos_text
subglos_list.append((subglos_key, subglos_text))
main_text += f"\t[m2][ref]{subglos_key}[/ref]\n"
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/dsl/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def addText(self, st: str) -> None:
return
self.output += st

def closeLabel(self):
def closeLabel(self) -> None:
# print(f"Label: {self.label!r}")
desc = None
if self.abbrev:
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/stardict/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def writeSynFile(self, altIndexList: T_SdList[tuple[bytes, int]]) -> None:
f"Writing {len(altIndexList)} synonyms took {now() - t0:.2f} seconds",
)

def writeIdxFile(self, indexList: T_SdList[tuple[bytes, bytes]]):
def writeIdxFile(self, indexList: T_SdList[tuple[bytes, bytes]]) -> None:
if not indexList:
return

Expand Down
4 changes: 2 additions & 2 deletions pyglossary/plugins/wiktextract.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def __iter__(self) -> Iterator[EntryType]:
msg = f"[{count} times] {msg}"
log.warning(msg)

def warning(self, msg):
def warning(self, msg: str) -> None:
self._warnings[msg] += 1

def makeEntry(self, data: dict[str, Any]) -> EntryType:
Expand Down Expand Up @@ -397,7 +397,7 @@ def writeSenseExample( # noqa: PLR6301, PLR0912
if not textList:
return

def writePair(prefix: str, text: str):
def writePair(prefix: str, text: str) -> None:
if prefix:
with hf.element("b"):
hf.write(prefix)
Expand Down
12 changes: 10 additions & 2 deletions pyglossary/sort_keys_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,19 @@


class SortKeyMakerType(Protocol):
def __call__(self, sortEncoding: str = "utf-8", **kwargs) -> SortKeyType: ...
def __call__(
self,
sortEncoding: str = "utf-8",
**kwargs, # noqa: ANN003
) -> SortKeyType: ...


class SQLiteSortKeyMakerType(Protocol):
def __call__(self, sortEncoding: str = "utf-8", **kwargs) -> SQLiteSortKeyType: ...
def __call__(
self,
sortEncoding: str = "utf-8",
**kwargs, # noqa: ANN003
) -> SQLiteSortKeyType: ...


class LocaleSortKeyMakerType(Protocol):
Expand Down
Loading

0 comments on commit 6d817a3

Please sign in to comment.