Skip to content

Commit b5db325

Browse files
committed
Fix linting and type checking recommendations
1 parent 51bcd26 commit b5db325

File tree

12 files changed

+47
-40
lines changed

12 files changed

+47
-40
lines changed

src/zimscraperlib/download.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ def shutdown(self) -> None:
3434
self.executor.shutdown(wait=True)
3535

3636
def _run_youtube_dl(self, url: str, options: dict[str, Any]) -> None:
37-
with youtube_dl.YoutubeDL(options) as ydl:
37+
with youtube_dl.YoutubeDL(
38+
options # pyright: ignore[reportArgumentType]
39+
) as ydl:
3840
ydl.download([url]) # pyright: ignore[reportUnknownMemberType]
3941

4042
def download(

src/zimscraperlib/i18n.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,17 @@ def __eq__(self, value: object) -> bool:
164164
and self.native == getattr(value, "native", None)
165165
)
166166

167+
def __hash__(self):
168+
return hash(
169+
f"{getattr(self, "iso_639_1", None)}$"
170+
f"{getattr(self, "iso_639_2b", None)}$"
171+
f"{getattr(self, "iso_639_2t", None)}$"
172+
f"{getattr(self, "iso_639_3", None)}$"
173+
f"{getattr(self, "iso_639_5", None)}$"
174+
f"{getattr(self, "english", None)}$"
175+
f"{getattr(self, "native", None)}"
176+
)
177+
167178

168179
def find_language_names(query: str) -> tuple[str, str]:
169180
"""(native, english) language names for query"""

src/zimscraperlib/image/probing.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,16 @@
55

66
import colorthief # pyright: ignore[reportMissingTypeStubs]
77
import PIL.Image
8+
from PIL.Image import EXTENSION as PIL_FMT_EXTENSION
9+
from PIL.Image import init as init_pil
810

911
from zimscraperlib.filesystem import get_content_mimetype, get_file_mimetype
1012

13+
init_pil() # populate the PIL_FMT_EXTENSION dictionary
14+
15+
known_extensions = {".svg": "SVG"}
16+
known_extensions.update(PIL_FMT_EXTENSION)
17+
1118

1219
def get_colors(
1320
src: pathlib.Path, *, use_palette: bool | None = True
@@ -82,13 +89,6 @@ def format_for(
8289
"Cannot guess image format from file suffix when byte array is passed"
8390
)
8491

85-
from PIL.Image import EXTENSION as PIL_FMT_EXTENSION
86-
from PIL.Image import init as init_pil
87-
88-
init_pil() # populate the PIL_FMT_EXTENSION dictionary
89-
90-
known_extensions = {".svg": "SVG"}
91-
known_extensions.update(PIL_FMT_EXTENSION)
9292
return known_extensions[src.suffix] if src.suffix in known_extensions else None
9393

9494

src/zimscraperlib/inputs.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
import shutil
33
import tempfile
44
from collections.abc import Iterable
5-
from typing import TypeVar
65

76
from zimscraperlib import logger
87
from zimscraperlib.constants import DEFAULT_USER_AGENT
@@ -14,8 +13,6 @@
1413
)
1514
from zimscraperlib.download import stream_file
1615

17-
T = TypeVar("T")
18-
1916

2017
def handle_user_provided_file(
2118
source: pathlib.Path | str | None = None,
@@ -136,6 +133,6 @@ def compute_tags(
136133
}
137134

138135

139-
def unique_values(items: list[T]) -> list[T]:
136+
def unique_values[T](items: list[T]) -> list[T]:
140137
"""Return unique values in input list while preserving list order"""
141138
return list(dict.fromkeys(items))

src/zimscraperlib/zim/creator.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ def add_item_for(
381381
def add_item( # pyright: ignore[reportIncompatibleMethodOverride]
382382
self,
383383
item: libzim.writer.Item,
384+
*,
384385
duplicate_ok: bool | None = None,
385386
callbacks: list[Callback] | Callback | None = None,
386387
):
@@ -417,6 +418,7 @@ def add_redirect(
417418
path: str,
418419
target_path: str,
419420
title: str | None = "",
421+
*,
420422
is_front: bool | None = None,
421423
duplicate_ok: bool | None = None,
422424
):

src/zimscraperlib/zim/items.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ def __init__(
235235
title: str | None = None,
236236
mimetype: str | None = None,
237237
hints: dict[libzim.writer.Hint, int] | None = None,
238+
*,
238239
use_disk: bool | None = None,
239240
**kwargs: Any,
240241
):

src/zimscraperlib/zim/metadata.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,6 @@ def get_libzim_value(self) -> bytes: ...
163163
# Alias for convenience when function accept any metadata
164164
AnyMetadata = MetadataBase[Any]
165165

166-
# TypeVar bounded to subclasses of GenericMetadata, used by class decorators so that
167-
# they properly accommodate to the class they are used on while still knowing they have
168-
# access to all attributes of the MetadataBase class
169-
U = TypeVar("U", bound=AnyMetadata)
170-
171166

172167
def clean_str(value: str) -> str:
173168
"""Clean a string value for unwanted control characters and strip white chars"""
@@ -179,47 +174,47 @@ def nb_grapheme_for(value: str) -> int:
179174
return len(regex.findall(r"\X", value))
180175

181176

182-
def mandatory(cls: type[U]):
177+
def mandatory[U: AnyMetadata](cls: type[U]):
183178
"""Marks a Metadata mandatory: must be set to please Creator and cannot be empty"""
184179
cls.is_required = True
185180
cls.empty_allowed = False
186181
return cls
187182

188183

189-
def allow_empty(cls: type[U]):
184+
def allow_empty[U: AnyMetadata](cls: type[U]):
190185
"""Whether input can be blank"""
191186
cls.empty_allowed = True
192187
return cls
193188

194189

195-
def allow_duplicates(cls: type[U]):
190+
def allow_duplicates[U: AnyMetadata](cls: type[U]):
196191
"""Whether list input can accept duplicate values"""
197192
cls.duplicates_allowed = True
198193
return cls
199194

200195

201-
def deduplicate(cls: type[U]):
196+
def deduplicate[U: AnyMetadata](cls: type[U]):
202197
"""Whether duplicates in list inputs should be reduced"""
203198
cls.duplicates_allowed = True
204199
cls.require_deduplication = True
205200
return cls
206201

207202

208-
def only_lang_codes(cls: type[U]):
203+
def only_lang_codes[U: AnyMetadata](cls: type[U]):
209204
"""Whether list input should be checked to only accept ISO-639-1 codes"""
210205
cls.oz_only_iso636_3_allowed = True
211206
return cls
212207

213208

214-
def x_protected(cls: type[U]):
209+
def x_protected[U: AnyMetadata](cls: type[U]):
215210
"""Whether metadata name should be checked for collision with reserved names
216211
217212
when applying recommendations"""
218213
cls.oz_x_protected = True
219214
return cls
220215

221216

222-
def x_prefixed(cls: type[U]):
217+
def x_prefixed[U: AnyMetadata](cls: type[U]):
223218
"""Whether metadata names should be automatically X-Prefixed"""
224219
cls.oz_x_protected = False
225220
cls.oz_x_prefixed = True

tests/conftest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import pytest
44

5+
from zimscraperlib.download import stream_file
6+
57

68
def pytest_addoption(parser: pytest.Parser):
79
parser.addoption(
@@ -162,7 +164,6 @@ def valid_user_agent():
162164

163165
@pytest.fixture(scope="session")
164166
def small_zim_file(tmpdir_factory: pytest.TempdirFactory) -> pathlib.Path:
165-
from zimscraperlib.download import stream_file
166167

167168
dst = pathlib.Path(tmpdir_factory.mktemp("data") / "small.zim")
168169
stream_file(
@@ -174,7 +175,6 @@ def small_zim_file(tmpdir_factory: pytest.TempdirFactory) -> pathlib.Path:
174175

175176
@pytest.fixture(scope="session")
176177
def ns_zim_file(tmpdir_factory: pytest.TempdirFactory) -> pathlib.Path:
177-
from zimscraperlib.download import stream_file
178178

179179
dst = pathlib.Path(tmpdir_factory.mktemp("data") / "ns.zim")
180180
stream_file(
@@ -187,7 +187,6 @@ def ns_zim_file(tmpdir_factory: pytest.TempdirFactory) -> pathlib.Path:
187187

188188
@pytest.fixture(scope="session")
189189
def real_zim_file(tmpdir_factory: pytest.TempdirFactory) -> pathlib.Path:
190-
from zimscraperlib.download import stream_file
191190

192191
dst = pathlib.Path(tmpdir_factory.mktemp("data") / "small.zim")
193192
stream_file(

tests/download/test_download.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99
import requests
1010
import requests.structures
11-
from yt_dlp import DownloadError # pyright: ignore[reportMissingTypeStubs]
11+
from yt_dlp.utils import DownloadError
1212

1313
from zimscraperlib.constants import DEFAULT_WEB_REQUESTS_TIMEOUT
1414
from zimscraperlib.download import (

tests/image/test_illustration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_get_zim_illustration(
4040

4141

4242
def test_get_missing_user_zim_illustration():
43-
with pytest.raises(Exception, match="missing.png could not be found"):
43+
with pytest.raises(Exception, match=r"missing\.png could not be found"):
4444
get_zim_illustration("./missing.png")
4545

4646

0 commit comments

Comments
 (0)