Skip to content

Commit 086623b

Browse files
committed
Fixed type checks for libzim 3.5.0
Also ignoring warning for missing source of regex as its a C module
1 parent 82882cb commit 086623b

File tree

5 files changed

+23
-15
lines changed

5 files changed

+23
-15
lines changed

src/zimscraperlib/zim/archive.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import libzim.suggestion # SuggestionSearcher # pyright: ignore
1919

2020
from zimscraperlib.zim._libkiwix import convertTags, parseMimetypeCounter
21-
from zimscraperlib.zim.items import Item
2221

2322

2423
class Archive(libzim.reader.Archive):
@@ -61,7 +60,7 @@ def get_entry_by_id(self, id_: int) -> libzim.reader.Entry:
6160
"""Entry from its Id in ZIM"""
6261
return self._get_entry_by_id(id_)
6362

64-
def get_item(self, path: str) -> Item:
63+
def get_item(self, path: str) -> libzim.reader.Item:
6564
"""Item from a path"""
6665
return self.get_entry_by_path(path).get_item()
6766

src/zimscraperlib/zim/creator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
import libzim.writer # pyright: ignore
3232
import PIL.Image
33-
import regex
33+
import regex # pyright: ignore [reportMissingModuleSource]
3434

3535
from zimscraperlib import logger
3636
from zimscraperlib.constants import (
@@ -491,7 +491,7 @@ def add_redirect(
491491

492492
try:
493493
try:
494-
super().add_redirection(path, title, target_path, hints)
494+
super().add_redirection(path, title or path, target_path, hints)
495495
except RuntimeError as exc:
496496
if not DUPLICATE_EXC_STR.match(str(exc)) or not duplicate_ok:
497497
raise exc

src/zimscraperlib/zim/items.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import re
1111
import tempfile
1212
import urllib.parse
13+
from collections.abc import Callable
1314
from typing import Any
1415

1516
import libzim.writer # pyright: ignore
@@ -65,6 +66,11 @@ def get_hints(self) -> dict:
6566
return getattr(self, "hints", {})
6667

6768

69+
def no_indexing_indexdata() -> IndexData:
70+
"""IndexData asking libzim not to index this item"""
71+
return IndexData("", "")
72+
73+
6874
class StaticItem(Item):
6975
"""scraperlib Item with auto contentProvider from `content` or `filepath`
7076
@@ -107,19 +113,17 @@ def __init__(
107113
path=path, title=title, mimetype=mimetype, hints=hints, **kwargs
108114
)
109115
if index_data:
110-
self.get_indexdata = lambda: index_data
116+
self.get_indexdata: Callable[[], IndexData] = lambda: index_data
111117
elif not auto_index:
112-
self.get_indexdata = lambda: IndexData("", "") # index nothing
118+
self.get_indexdata = no_indexing_indexdata # index nothing
113119
else:
114120
self._get_auto_index() # consider to add auto index
115121

116122
# Populate item title from index data if title is not set by caller
117-
if (
118-
(not hasattr(self, "title") or not self.title)
119-
and hasattr(self, "get_indexdata")
120-
and self.get_indexdata().get_title()
121-
):
122-
self.title = self.get_indexdata().get_title()
123+
if (not getattr(self, "title", None)) and hasattr(self, "get_indexdata"):
124+
title = self.get_indexdata().get_title()
125+
if title:
126+
self.title = title
123127

124128
def get_contentprovider(self) -> libzim.writer.ContentProvider:
125129
# content was set manually

src/zimscraperlib/zim/providers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import io
1515
import pathlib
16+
from typing import Generator
1617

1718
import libzim.writer # pyright: ignore
1819
import requests
@@ -61,7 +62,7 @@ def __init__(
6162
def get_size(self) -> int:
6263
return self.size # pyright: ignore
6364

64-
def gen_blob(self) -> libzim.writer.Blob:
65+
def gen_blob(self) -> Generator[libzim.writer.Blob, None, None]:
6566
yield libzim.writer.Blob(self.fileobj.getvalue()) # pragma: no cover
6667

6768

@@ -92,7 +93,7 @@ def get_size_of(url) -> int | None:
9293
def get_size(self) -> int:
9394
return self.size # pyright: ignore
9495

95-
def gen_blob(self) -> libzim.writer.Blob: # pragma: no cover
96+
def gen_blob(self) -> Generator[libzim.writer.Blob, None, None]: # pragma: no cover
9697
for chunk in self.resp.iter_content(10 * 1024):
9798
if chunk:
9899
yield libzim.writer.Blob(chunk)

tests/zim/test_zim_creator.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,15 @@ def get_size(self) -> int:
3838

3939

4040
class SpecialURLProviderItem(StaticItem):
41+
url: str
42+
4143
def get_contentprovider(self):
4244
return SpecialURLProvider(self.url)
4345

4446

4547
class FileLikeProviderItem(StaticItem):
48+
fileobj: io.BytesIO
49+
4650
def get_contentprovider(self):
4751
if not self.fileobj:
4852
raise AttributeError("fileobj cannot be None")
@@ -125,7 +129,7 @@ def test_create_without_workaround(tmp_path):
125129
fpath, "welcome", workaround_nocancel=False
126130
).config_dev_metadata() as creator:
127131
with pytest.raises(RuntimeError, match="AttributeError"):
128-
creator.add_item("hello")
132+
creator.add_item("hello") # pyright: ignore [reportArgumentType]
129133

130134

131135
def test_noindexlanguage(tmp_path):

0 commit comments

Comments
 (0)