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

Change subject: [wikibase] give a more informative exception
......................................................................

[wikibase] give a more informative exception

Give a more informative exception if a property does not exists instead
of just KeyError: <prop>

Bug: T368908
Change-Id: I53d95f413d535947dc0d3fd4b753c095dd653c9b
---
M pywikibot/site/_datasite.py
1 file changed, 12 insertions(+), 8 deletions(-)

Approvals:
  Matěj Suchánek: Looks good to me, approved
  jenkins-bot: Verified




diff --git a/pywikibot/site/_datasite.py b/pywikibot/site/_datasite.py
index a61773d..f5be91e 100644
--- a/pywikibot/site/_datasite.py
+++ b/pywikibot/site/_datasite.py
@@ -1,6 +1,6 @@
 """Objects representing API interface to Wikibase site."""
 #
-# (C) Pywikibot team, 2012-2023
+# (C) Pywikibot team, 2012-2024
 #
 # Distributed under the terms of the MIT license.
 #
@@ -253,11 +253,12 @@
                 yield page

     def getPropertyType(self, prop):
-        """
-        Obtain the type of a property.
+        """Obtain the type of a property.

-        This is used specifically because we can cache
-        the value for a much longer time (near infinite).
+        This is used specifically because we can cache the value for a
+        much longer time (near infinite).
+
+        :raises KeyError: *prop* does not exist
         """
         params = {'action': 'wbgetentities', 'ids': prop.getID(),
                   'props': 'datatype'}
@@ -269,11 +270,14 @@
         # the IDs returned from the API can be upper or lowercase, depending
         # on the version. See bug T55894 for more information.
         try:
-            dtype = data['entities'][prop.getID()]['datatype']
+            entity = data['entities'][prop.getID()]
         except KeyError:
-            dtype = data['entities'][prop.getID().lower()]['datatype']
+            entity = data['entities'][prop.getID().lower()]

-        return dtype
+        if 'missing' in entity:
+            raise KeyError(f'{prop} does not exist')
+
+        return entity['datatype']
 
     @need_right('edit')
     def editEntity(self, entity, data, bot: bool = True, **kwargs):

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1051096?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: I53d95f413d535947dc0d3fd4b753c095dd653c9b
Gerrit-Change-Number: 1051096
Gerrit-PatchSet: 4
Gerrit-Owner: Xqt <i...@gno.de>
Gerrit-Reviewer: Matěj Suchánek <matejsuchane...@gmail.com>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- pywikibot-commits@lists.wikimedia.org
To unsubscribe send an email to pywikibot-commits-le...@lists.wikimedia.org

Reply via email to