jenkins-bot has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1187977?usp=email )

Change subject: IMPR: Use a better message in TokenWallet.update_tokens for 
KeyError
......................................................................

IMPR: Use a better message in TokenWallet.update_tokens for KeyError

- raise KeyError with 'No valid token types found to update.' instead of
  'Invalid token None for user <username> on <sitename>.'
  This also solves mypy issues for this module.
- update pre-commit hooks
- update mypy tests

Change-Id: Ic19b041300499cb546b91bcac4e85e2919568c31
---
M .pre-commit-config.yaml
M conftest.py
M pywikibot/site/_tokenwallet.py
3 files changed, 21 insertions(+), 6 deletions(-)

Approvals:
  Xqt: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml
index dfbfd77..090c22d 100644
--- a/.pre-commit-config.yaml
+++ b/.pre-commit-config.yaml
@@ -65,7 +65,7 @@
         language: python
         require_serial: true
   - repo: https://github.com/astral-sh/ruff-pre-commit
-    rev: v0.12.12
+    rev: v0.13.0
     hooks:
       - id: ruff-check
         alias: ruff
@@ -113,7 +113,7 @@
           - flake8-tuple>=0.4.1
           - pep8-naming>=0.15.1
   - repo: https://github.com/pre-commit/mirrors-mypy
-    rev: v1.17.1
+    rev: v1.18.1
     hooks:
       - id: mypy
         args:
@@ -127,6 +127,6 @@
           ^pywikibot/families/[a-z][a-z\d]+_family\.py$|
           ^pywikibot/page/(__init__|_decorators|_revision)\.py$|
           ^pywikibot/scripts/(?:i18n/)?__init__\.py$|
-          
^pywikibot/site/(__init__|_basesite|_decorators|_extensions|_interwikimap|_upload)\.py$|
+          
^pywikibot/site/(__init__|_basesite|_decorators|_extensions|_interwikimap|_tokenwallet|_upload)\.py$|
           ^pywikibot/tools/(_logging|_unidata|formatter)\.py$|
           
^pywikibot/userinterfaces/(__init__|_interface_base|terminal_interface)\.py$
diff --git a/conftest.py b/conftest.py
index ebc6027..27f2480 100644
--- a/conftest.py
+++ b/conftest.py
@@ -21,7 +21,8 @@
     r'families/[a-z][a-z\d]+_family|'
     r'page/(__init__|_decorators|_revision)|'
     r'scripts/(i18n/)?__init__|'
-    r'site/(__init__|_basesite|_decorators|_extensions|_interwikimap|_upload)|'
+    r'site/(__init__|_basesite|_decorators|_extensions|_interwikimap|'
+    r'_tokenwallet|_upload)|'
     r'tools/(_logging|_unidata|formatter)|'
     r'userinterfaces/(__init__|_interface_base|terminal_interface)'
     r')\.py'
diff --git a/pywikibot/site/_tokenwallet.py b/pywikibot/site/_tokenwallet.py
index 0baabde..a691199 100644
--- a/pywikibot/site/_tokenwallet.py
+++ b/pywikibot/site/_tokenwallet.py
@@ -1,6 +1,6 @@
 """Objects representing api tokens."""
 #
-# (C) Pywikibot team, 2008-2023
+# (C) Pywikibot team, 2008-2025
 #
 # Distributed under the terms of the MIT license.
 #
@@ -119,11 +119,25 @@
            r._params['token'] = r.site.tokens.update_tokens(r._params['token'])

         .. versionadded:: 8.0
+
+        :param tokens: A list of token types that need to be updated.
+        :return: A list of updated tokens corresponding to the given
+            *tokens* types.
+        :raises KeyError: If no valid token types can be determined to
+            update.
         """
         # find the token types
         types = [key
                  for key, value in self._tokens.items() for token in tokens
-                 if value == token] or [self._last_token_key]
+                 if value == token]
+
+        # fallback to _last_token_key if no types found
+        if not types and self._last_token_key is not None:
+            types = [self._last_token_key]
+
+        if not types:
+            raise KeyError('No valid token types found to update.')
+
         self.clear()  # clear the cache
         return [self[token_type] for token_type in types]


--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1187977?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: Ic19b041300499cb546b91bcac4e85e2919568c31
Gerrit-Change-Number: 1187977
Gerrit-PatchSet: 1
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]

Reply via email to