Xqt has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1309780?usp=email )
Change subject: typing: Fix typing errors in api._generators
......................................................................
typing: Fix typing errors in api._generators
Also raise the exception from :func:`comms.http.fetch` in WikiStats.get()
instead of raising :exc:`AttributeError` if that function fails.
Change-Id: I13eab77a0e2d851deef7caf6d818dc537aef8ca6
---
M .pre-commit-config.yaml
M conftest.py
M pywikibot/__init__.py
M pywikibot/comms/http.py
M pywikibot/data/__init__.py
M pywikibot/data/api/_generators.py
M pywikibot/data/sparql.py
M pywikibot/data/wikistats.py
M pywikibot/page/_basepage.py
9 files changed, 40 insertions(+), 13 deletions(-)
Approvals:
Xqt: Verified; Looks good to me, approved
diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index 7694429..9e58af9 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -127,7 +127,7 @@
(__metadata__|backports|config|cosmetic_changes|daemonize|diff|echo|exceptions|fixes|logging|plural|time|titletranslate)|
(comms|data|families|specialbots)/__init__|
comms/eventstreams|
- data/(api/(__init__|_optionset)|citoid|memento|wikistats)|
+
data/(api/(__init__|_generators|_optionset)|citoid|memento|wikistats)|
families/[a-z][a-z\d]+_family|
page/(__init__|_decorators|_page|_revision|_user)|
pagegenerators/(__init__|_filters)|
diff --git a/conftest.py b/conftest.py
index 59c66b9..7693fa1 100644
--- a/conftest.py
+++ b/conftest.py
@@ -41,7 +41,7 @@
r'exceptions|fixes|logging|plural|time|titletranslate)|'
r'(comms|data|families|specialbots)/__init__|'
r'comms/eventstreams|'
- r'data/(api/(__init__|_optionset)|citoid|memento|wikistats)|'
+ r'data/(api/(__init__|_generators|_optionset)|citoid|memento|wikistats)|'
r'families/[a-z][a-z\d]+_family|'
r'page/(__init__|_decorators|_page|_revision|_user)|'
r'pagegenerators/(__init__|_filters)|'
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index acd09e0..b58d0b6 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -314,7 +314,7 @@
# Throttle and thread handling
-def sleep(secs: int) -> None:
+def sleep(secs: int | float) -> None:
"""Suspend execution of the current thread for the given number of seconds.
Drop this process from the throttle log if wait time is greater than
diff --git a/pywikibot/comms/http.py b/pywikibot/comms/http.py
index 314cb39..fdcff85 100644
--- a/pywikibot/comms/http.py
+++ b/pywikibot/comms/http.py
@@ -415,7 +415,7 @@
headers: dict[str, str] | None = None,
default_error_handling: bool = True,
use_fake_user_agent: bool | str = False,
- **kwargs) -> requests.Response:
+ **kwargs) -> requests.Response | Exception:
"""HTTP request.
.. version-changed:: 7.0
diff --git a/pywikibot/data/__init__.py b/pywikibot/data/__init__.py
index 19270cc..f0ae5b9 100644
--- a/pywikibot/data/__init__.py
+++ b/pywikibot/data/__init__.py
@@ -25,7 +25,7 @@
request. Starting with 1 if attribute is missing.
"""
- def wait(self, delay: int | None = None) -> None:
+ def wait(self, delay: int | float | None = None) -> None:
"""Determine how long to wait after a failed request.
:param delay: Minimum time in seconds to wait. Overwrites
@@ -36,7 +36,7 @@
self.max_retries = pywikibot.config.max_retries
if not hasattr(self, 'retry_wait'):
- self.retry_wait = pywikibot.config.retry_wait
+ self.retry_wait: int | float = pywikibot.config.retry_wait
if not hasattr(self, 'current_retries'):
self.current_retries = 1
diff --git a/pywikibot/data/api/_generators.py
b/pywikibot/data/api/_generators.py
index 42cce31..f049802 100644
--- a/pywikibot/data/api/_generators.py
+++ b/pywikibot/data/api/_generators.py
@@ -14,7 +14,7 @@
from abc import ABC, abstractmethod
from collections.abc import Callable, Iterable
from contextlib import suppress
-from typing import Any
+from typing import Any, cast
from warnings import warn
import pywikibot
@@ -521,8 +521,13 @@
if not self.support_namespace():
raise TypeError(f'{self.limited_module or self.modules} module'
' does not support a namespace parameter')
- param = self.site._paraminfo.parameter('query+' + self.limited_module,
- 'namespace')
+
+ # mypy cannot infer that support_namespace() guarantees
+ # self.limited_module is not None; therefore use cast here.
+ param = self.site._paraminfo.parameter(
+ 'query+' + cast(str, self.limited_module),
+ 'namespace'
+ )
if isinstance(namespaces, str):
namespaces = namespaces.split('|')
diff --git a/pywikibot/data/sparql.py b/pywikibot/data/sparql.py
index a795358..d259326 100644
--- a/pywikibot/data/sparql.py
+++ b/pywikibot/data/sparql.py
@@ -7,10 +7,10 @@
from __future__ import annotations
from textwrap import fill
-from typing import Any
+from typing import Any, cast
from urllib.parse import quote
-from requests import JSONDecodeError
+from requests import JSONDecodeError, Response
from requests.exceptions import Timeout
from pywikibot import Site
@@ -74,7 +74,7 @@
self.endpoint = endpoint
self.entity_url = entity_url
- self.last_response = None
+ self.last_response: Response | Exception | None = None
if max_retries is not None:
self.max_retries = max_retries
@@ -148,7 +148,8 @@
url = f'{self.endpoint}?query={quote(query)}'
while True:
try:
- self.last_response = http.fetch(url, headers=headers)
+ self.last_response = cast(Response,
+ http.fetch(url, headers=headers))
except Timeout:
pass
except ServerError as e:
diff --git a/pywikibot/data/wikistats.py b/pywikibot/data/wikistats.py
index 2e3b049..78704c4 100644
--- a/pywikibot/data/wikistats.py
+++ b/pywikibot/data/wikistats.py
@@ -86,6 +86,10 @@
def get(self, table: str) -> list:
"""Get a list of a table of data.
+ .. version-changed:: 11.6
+ Raise the exception from :func:`comms.http.fetch` instead of
+ raising :exc:`AttributeError` if that function fails.
+
:param table: table of data to fetch
"""
if table in _data:
@@ -98,6 +102,9 @@
path = '/api.php?action=dump&table={table}&format=csv'
url = self.url + path
r = http.fetch(url.format(table=table))
+ if isinstance(r, Exception):
+ raise r
+
f = StringIO(r.text)
reader = DictReader(f)
data = list(reader)
diff --git a/pywikibot/page/_basepage.py b/pywikibot/page/_basepage.py
index 41b2deb..5062f00 100644
--- a/pywikibot/page/_basepage.py
+++ b/pywikibot/page/_basepage.py
@@ -77,6 +77,20 @@
'_timestamp',
)
+ if TYPE_CHECKING:
+ _catinfo: dict[str, int]
+ _contentmodel: str
+ _imageforpage: dict[str, Any]
+ _isredir: bool
+ _lintinfo: dict[str, Any]
+ _pageid: int
+ _pageimage: pywikibot.FilePage
+ _preloadedtext: str
+ _protection: list[str]
+ _revid: int
+ _text: str
+ _timestamp: str
+
def __init__(self, source, title: str = '', ns: int = 0) -> None:
"""Instantiate a Page object.
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1309780?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I13eab77a0e2d851deef7caf6d818dc537aef8ca6
Gerrit-Change-Number: 1309780
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]