Skip to content

Commit

Permalink
add types for list vars with empty initial value
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Dec 15, 2024
1 parent fab2925 commit e843098
Show file tree
Hide file tree
Showing 26 changed files with 61 additions and 62 deletions.
2 changes: 1 addition & 1 deletion pyglossary/plugins/aard2_slob.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ def open(self, filename: str) -> None: # noqa: PLR0912
if t_created_by in tags:
self._glos.setInfo("author", tags[t_created_by])

copyrightLines = []
copyrightLines: list[str] = []
for key in (t_copyright, t_license_name, t_license_url):
try:
value = tags.pop(key)
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/appledict/_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def base36(x: int) -> str:
Simplified version of int2base
http://stackoverflow.com/questions/2267362/convert-integer-to-a-string-in-a-given-numeric-base-in-python#2267446.
"""
digits = []
digits: list[str] = []
while x:
digits.append(digs[x % 36])
x //= 36
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/babylon_bgl/bgl_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ def decodeCharsetTags( # noqa: PLR0912
"""
b_parts = re_charset_decode.split(b_text)
u_text = ""
encodings = [] # stack of encodings
encodings: list[str] = [] # stack of encodings
defaultEncodingOnly = True
for i, b_part in enumerate(b_parts):
if i % 3 == 0: # text block
Expand Down
4 changes: 2 additions & 2 deletions pyglossary/plugins/babylon_bgl/bgl_reader_debug.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ def findAndPrintCharSamples(self, b_data: bytes, hint, encoding):
f"\nmarked = {res}\norig = {b_data}\n",
)

def findCharSamples(self, b_data):
def findCharSamples(self, b_data: bytes) -> list[int]:
"""
Find samples of chars in b_data.
Expand All @@ -520,7 +520,7 @@ def findCharSamples(self, b_data):
Returns a list of offsets in b_data
May return an empty list.
"""
res = []
res: list[int] = []
if not isinstance(b_data, bytes):
log.error("findCharSamples: b_data is not a bytes instance")
return res
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/cc_kedict.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ def _createEntry(
def nextBlock(self) -> EntryType:
if not self._file:
raise StopIteration
lines = []
lines: list[str] = []
while True:
line = self.readline()
if not line:
Expand Down
14 changes: 7 additions & 7 deletions pyglossary/plugins/dicformids.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from pyglossary.plugins.tabfile import Reader as TabfileReader

if TYPE_CHECKING:
from collections.abc import Iterator
from collections.abc import Generator, Iterator

from pyglossary.glossary_types import EntryType, GlossaryType
from pyglossary.option import Option
Expand Down Expand Up @@ -92,12 +92,12 @@ class Reader:

def __init__(self, glos: GlossaryType) -> None:
self._glos = glos
self._tabFileNames = []
self._tabFileNames: list[str] = []
self._tabFileReader = None

def open(self, dirname: str) -> None:
self._dirname = dirname
orderFileNames = []
orderFileNames: list[tuple[int, str]] = []
for fname in os.listdir(dirname):
if not fname.startswith("directory"):
continue
Expand Down Expand Up @@ -212,11 +212,11 @@ def open(self, dirname: str) -> None:
if not os.path.isdir(dirname):
os.mkdir(dirname)

def write(self) -> None:
def write(self) -> Generator[None, EntryType, None]:
self.nextIndex()

dicMaxSize = 0
indexData = []
indexData: list[tuple[str, int, int]] = []

def writeBucket(dicIndex: int, entryList: list[EntryType]) -> None:
nonlocal dicMaxSize
Expand Down Expand Up @@ -247,7 +247,7 @@ def writeBucket(dicIndex: int, entryList: list[EntryType]) -> None:
bucketSize = self.linesPerDirectoryFile
wordCount = 0
dicIndex = 0
entryList = [] # aka bucket
entryList: list[EntryType] = [] # aka bucket
while True:
entry = yield
if entry is None:
Expand All @@ -264,7 +264,7 @@ def writeBucket(dicIndex: int, entryList: list[EntryType]) -> None:

if entryList:
writeBucket(dicIndex, entryList)
entryList = None
entryList = []

self.dicMaxSize = dicMaxSize
self.wordCount = wordCount
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 @@ -365,8 +365,8 @@ def parseEntryBlock( # noqa: PLR0912 Too many branches (14 > 12)
term_lines: list[str],
text_lines: list[str],
) -> Iterator[EntryType]:
terms = []
defiTitles = []
terms: list[str] = []
defiTitles: list[str] = []
for line in term_lines:
tr = TitleTransformer(line)
res, err = tr.transform()
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/ebook_epub2.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def write_ncx(self, group_labels: list[str]) -> None:
write_ncx
only for epub.
"""
ncx_items = []
ncx_items: list[str] = []
index = 1
if self._include_index_page:
ncx_items.append(
Expand Down
5 changes: 3 additions & 2 deletions pyglossary/plugins/ebook_kobo.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ def writeGroup(lastPrefix: str) -> None:
gzipFile.write(htmlContents.encode("utf-8"))
htmlContents = htmlHeader

allWords = []
data = []
allWords: list[str] = []
# TODO: switch to SQLite, like StarDict writer
data: list[tuple[str, bytes]] = []

while True:
entry = yield
Expand Down
14 changes: 7 additions & 7 deletions pyglossary/plugins/freedict/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def writeTransCit(
) -> None:
from lxml import etree as ET

quotes = []
quotes: list[Element] = []
sense = ET.Element(f"{TEI}sense")
for child in elem.xpath("child::node()"):
if isinstance(child, str):
Expand Down Expand Up @@ -482,7 +482,7 @@ def writeGramGroups(
attrib["color"] = color

for gramGrp in gramGrpList:
parts = []
parts: list[str] = []
for child in gramGrp.iterchildren():
part = self.normalizeGramGrpChild(child)
if part:
Expand Down Expand Up @@ -596,7 +596,7 @@ def getEntryByElem( # noqa: PLR0912
from lxml import etree as ET

glos = self._glos
keywords = []
keywords: list[str] = []
buff = BytesIO()
pron_color = self._pron_color

Expand All @@ -608,7 +608,7 @@ def getEntryByElem( # noqa: PLR0912
def br() -> Element:
return ET.Element("br")

inflectedKeywords = []
inflectedKeywords: list[str] = []

for form in entry.findall("form", NAMESPACE):
inflected = form.get("type") == "infl"
Expand Down Expand Up @@ -692,7 +692,7 @@ def stripParagList(
self,
elems: list[Element],
) -> str:
lines = []
lines: list[str] = []
for elem in elems:
for line in self.stripParag(elem).split("\n"):
line = line.strip() # noqa: PLW2901
Expand Down Expand Up @@ -731,14 +731,14 @@ def replaceRefLink(self, text: str) -> str:
return self._ref_pattern.sub('<a href="\\1">\\2</a>', text)

def setDescription(self, header: Element) -> None:
elems = []
elems: list[Element] = []
for tag in ("sourceDesc", "projectDesc"):
elems += header.findall(f".//{tag}//p", NAMESPACE)
desc = self.stripParagList(elems)
if not desc:
return

website_list = []
website_list: list[str] = []
for match in self._website_pattern.findall(desc):
if not match[1]:
continue
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/html_dir.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def pageHeader(n: int) -> str:
)

def navBar() -> str:
links = []
links: list[str] = []
if len(self._filenameList) > 1:
links.append(f'<a href="./{self._filenameList[-2]}">&#9664;</a>')
links.extend(
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 @@ -217,7 +217,7 @@ def __iter__(self) -> Iterator[EntryType]: # noqa: PLR0912

term = self.getTerm(termE)

words = []
words: list[str] = []
if term:
words.append(term)
if code:
Expand All @@ -234,7 +234,7 @@ def __iter__(self) -> Iterator[EntryType]: # noqa: PLR0912
if identifierSynonym is not None and identifierSynonym.text:
words.append(identifierSynonym.text)

defiParts = []
defiParts: list[str] = []

definition = elem.find("./definition")
if definition is None or not definition.text:
Expand Down Expand Up @@ -269,7 +269,7 @@ def __iter__(self) -> Iterator[EntryType]: # noqa: PLR0912

relatedList = elem.findall("./related/entry")
if relatedList:
relatedLinkList = []
relatedLinkList: list[str] = []
for related in relatedList:
relatedURL = related.text
relatedCode = relatedURL.split("/")[-1]
Expand Down
8 changes: 4 additions & 4 deletions pyglossary/plugins/jmdict.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def br() -> Element:
hf.write(gloss)
hf.write(br())

relatedWords = []
relatedWords: list[str] = []
for elem in sense.findall("xref"):
if not elem.text:
continue
Expand All @@ -168,7 +168,7 @@ def br() -> Element:
hf.write(word)
hf.write(br())

antonymWords = []
antonymWords: list[str] = []
for elem in sense.findall("ant"):
if not elem.text:
continue
Expand Down Expand Up @@ -257,7 +257,7 @@ def getEntryByElem( # noqa: PLR0912
from lxml import etree as ET

glos = self._glos
keywords = []
keywords: list[str] = []
f = BytesIO()
translit = self._translitation

Expand Down Expand Up @@ -300,7 +300,7 @@ def br() -> Element:
continue
if not reb.text:
continue
props = []
props: list[str] = []
if r_ele.find("re_nokanji") is not None:
props.append("no kanji")
inf = r_ele.find("re_inf")
Expand Down
6 changes: 3 additions & 3 deletions pyglossary/plugins/jmnedict.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def br() -> Element:
hf.write(desc)
hf.write(br())

relatedWords = []
relatedWords: list[str] = []
for elem in trans.findall("xref"):
if not elem.text:
continue
Expand All @@ -149,7 +149,7 @@ def getEntryByElem( # noqa: PLR0912
from lxml import etree as ET

glos = self._glos
keywords = []
keywords: list[str] = []
f = BytesIO()

def br() -> Element:
Expand All @@ -176,7 +176,7 @@ def br() -> Element:
continue
if not reb.text:
continue
props = []
props: list[str] = []
if r_ele.find("re_nokanji") is not None:
props.append("no kanji")
inf = r_ele.find("re_inf")
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/lingoes_ldf.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def fixInfoWord(cls, word: str) -> str:
def nextBlock(self) -> nextBlockResultType:
if not self._file:
raise StopIteration
entryLines = []
entryLines: list[str] = []
while True:
line = self.readline()
if not line:
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/quickdic6/quickdic.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def add_index( # noqa: PLR0913
index_start = len(rows)
count = 0
token_norm = "" if token == token_norm else token_norm # noqa: PLW2901
html_indices = []
html_indices: list[int] = []
rows.append((1, i_entry))
if ttype == 4:
if tidx not in html_indices:
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/quickdic6/read_funcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def read_list(
) -> list[T]:
size = read_int(fp)
toc = struct.unpack(f">{size + 1}q", fp.read(8 * (size + 1)))
entries = []
entries: list[T] = []
for offset in toc[:-1]:
fp.seek(offset)
entries.append(fun(fp))
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/quickdic6/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _extract_rows_from_indexentry(
block_rows = rows[start_index : start_index + count + 1]
assert block_rows[0][0] in {1, 3}
assert block_rows[0][1] == i_entry
e_rows = []
e_rows: list[tuple[int, int]] = []
for entry_type, entry_idx in block_rows[1:]:
if entry_type in {1, 3}:
# avoid an endless recursion
Expand Down
8 changes: 3 additions & 5 deletions pyglossary/plugins/quickdic6/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def write_quickdic(dic: QuickDic, path: str) -> None:

def write(self) -> typing.Generator[None, EntryType, None]:
synonyms: dict[str, list[str]] = {}
htmls = []
htmls: list[tuple[int, str, str]] = []
log.info("Converting individual entries ...")
while True:
entry = yield
Expand Down Expand Up @@ -111,8 +111,6 @@ def write(self) -> typing.Generator[None, EntryType, None]:
log.info(f"QuickDic: {langs = }, {name = }")

sources = [("", len(htmls))]
pairs = []
texts = []

created = None
createdStr = os.getenv("QUICKDIC_CREATION_TIME")
Expand All @@ -123,8 +121,8 @@ def write(self) -> typing.Generator[None, EntryType, None]:
self._dic = QuickDic(
name=name,
sources=sources,
pairs=pairs,
texts=texts,
pairs=[],
texts=[],
htmls=htmls,
created=created,
# version: int = 6,
Expand Down
2 changes: 1 addition & 1 deletion pyglossary/plugins/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def _writeInfo(self) -> None:
newline = self._newline
info_keys = self._getInfoKeys()
infoDefLine = "CREATE TABLE dbinfo ("
infoValues = []
infoValues: list[str] = []
glos = self._glos

for key in info_keys:
Expand Down
Loading

0 comments on commit e843098

Please sign in to comment.