Xqt has submitted this change. ( 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1074403?usp=email )

Change subject: [IMPR] Show a warning message for a deleted or unknown claim 
type
......................................................................

[IMPR] Show a warning message for a deleted or unknown claim type

Show this warning once and only if WbUnknown.toWikibase() is called.
Also update tests-

Bug: T374676
Change-Id: I56400c8f24390608c225ea953f406d6cbd329ff7
---
M pywikibot/_wbtypes.py
M pywikibot/page/_wikibase.py
M tests/wikibase_tests.py
3 files changed, 24 insertions(+), 17 deletions(-)

Approvals:
  Xqt: Verified; Looks good to me, approved




diff --git a/pywikibot/_wbtypes.py b/pywikibot/_wbtypes.py
index 4764cfa..66374df 100644
--- a/pywikibot/_wbtypes.py
+++ b/pywikibot/_wbtypes.py
@@ -1151,8 +1151,7 @@


 class WbUnknown(WbRepresentation):
-    """
-    A Wikibase representation for unknown data type.
+    """A Wikibase representation for unknown data type.

     This will prevent the bot from breaking completely when a new type
     is introduced.
@@ -1160,24 +1159,34 @@
     This data type is just a json container

     .. versionadded:: 3.0
+    .. versionchanged:: 9.4
+       *warning* parameter was added
     """

     _items = ('json',)

-    def __init__(self, json: dict[str, Any]) -> None:
+    def __init__(self, json: dict[str, Any], warning: str = '') -> None:
         """
         Create a new WbUnknown object.

         :param json: Wikibase JSON
+        :param warning: a warning message which is shown once if
+            :meth:`toWikibase` is called
         """
         self.json = json
+        self.warning = warning

     def toWikibase(self) -> dict[str, Any]:
-        """
-        Return the JSON object for the Wikibase API.
+        """Return the JSON object for the Wikibase API.
+
+        .. versionchanged:: 9.4
+           a waning message given by the warning attribute is shown once.

         :return: Wikibase JSON
         """
+        if self.warning:
+            pywikibot.warning(self.warning)
+            self.warning = ''
         return self.json

     @classmethod
diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py
index 881f3d3..65c2181 100644
--- a/pywikibot/page/_wikibase.py
+++ b/pywikibot/page/_wikibase.py
@@ -1777,19 +1777,19 @@
             except NoWikibaseEntityError:
                 claim_type = None

-            claim.target = None
+            msg = None
             if not claim_type:
-                pywikibot.warning(f'{claim.id} does not exist.')
+                msg = '{claim.id} does not exist.'
             elif claim.type in cls.types:
                 # The default covers string, url types
                 claim.target = cls.TARGET_CONVERTER.get(
                     claim.type, lambda value, site: value)(value, site)
             else:
-                pywikibot.warning(
-                    f'{claim.type} datatype is not supported yet.')
+                msg = f'{claim.type} datatype is not supported yet.'

-            if claim.target is None:
+            if msg is not None:
                 claim.target = pywikibot.WbUnknown.fromWikibase(value)
+                claim.target.warning = msg

         if 'rank' in data:  # References/Qualifiers don't have ranks
             claim.rank = data['rank']
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index a5c3135..0a1f9c2 100755
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -14,7 +14,6 @@
 import unittest
 from contextlib import suppress
 from decimal import Decimal
-from unittest import mock

 import pywikibot
 from pywikibot import pagegenerators
@@ -1192,12 +1191,11 @@

     def test_load_unknown(self):
         """Ensure unknown value is loaded but raises a warning."""
-        with mock.patch.object(pywikibot, 'warning', autospec=True) as warn:
-            self.wdp.get()
-            unknown_value = self.wdp.claims['P99999'][0].getTarget()
-            self.assertIsInstance(unknown_value, pywikibot.WbUnknown)
-            warn.assert_called_once_with(
-                'foo-unknown-bar datatype is not supported yet.')
+        self.wdp.get()
+        unknown_value = self.wdp.claims['P99999'][0].getTarget()
+        self.assertIsInstance(unknown_value, pywikibot.WbUnknown)
+        self.assertEqual(unknown_value.warning,
+                         'foo-unknown-bar datatype is not supported yet.')


 class TestItemPageExtensibility(TestCase):

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1074403?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: I56400c8f24390608c225ea953f406d6cbd329ff7
Gerrit-Change-Number: 1074403
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <i...@gno.de>
Gerrit-Reviewer: Amire80 <amir.ahar...@mail.huji.ac.il>
Gerrit-Reviewer: Xqt <i...@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: Matěj Suchánek <matejsuchane...@gmail.com>
_______________________________________________
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