From 96002254adfeb4b084fe74251fdc8752d938f49e Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Sun, 14 May 2023 11:07:14 +0200 Subject: [PATCH] Docstrings and types. --- matcher/commons.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/matcher/commons.py b/matcher/commons.py index 469566c..8c537e9 100644 --- a/matcher/commons.py +++ b/matcher/commons.py @@ -1,10 +1,11 @@ """Use mediawiki API to look up images on Wikimedia Commons.""" import urllib.parse +from typing import Any import requests -from . import utils +from . import CallParams, utils commons_start = "http://commons.wikimedia.org/wiki/Special:FilePath/" commons_url = "https://www.wikidata.org/w/api.php" @@ -16,9 +17,9 @@ def commons_uri_to_filename(uri: str) -> str: return urllib.parse.unquote(utils.drop_start(uri, commons_start)) -def api_call(params: dict[str, str | int]) -> requests.models.Response: - """Make an API call.""" - call_params = { +def api_call(params: CallParams) -> requests.Response: + """Call the Commons API.""" + call_params: CallParams = { "format": "json", "formatversion": 2, **params, @@ -27,9 +28,11 @@ def api_call(params: dict[str, str | int]) -> requests.models.Response: return requests.get(commons_url, params=call_params, timeout=5) -def image_detail(filenames, thumbheight=None, thumbwidth=None): +def image_detail( + filenames: list[str], thumbheight: int | None = None, thumbwidth: int | None = None +) -> dict[str, Any]: """Detail for multiple images.""" - params = { + params: CallParams = { "action": "query", "prop": "imageinfo", "iiprop": "url", @@ -39,7 +42,7 @@ def image_detail(filenames, thumbheight=None, thumbwidth=None): if thumbwidth is not None: params["iiurlwidth"] = thumbwidth - images = {} + images: dict[str, Any] = {} for cur in utils.chunk(filenames, page_size): call_params = params.copy()