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

Change subject: get_best_claim: Move implementation to ItemPage
......................................................................

get_best_claim: Move implementation to ItemPage

Since this is a Wikibase-specific function, move the implementation to
ItemPage and keep the Page version as a wrapper. Since we're touching
the code, also add a test for the new implementation.

Bug: T400610
Change-Id: I2fddb91a130c1e76fbb8f93b08c2b50eaf9da41d
Signed-off-by: Strainu <[email protected]>
---
M pywikibot/page/_page.py
M pywikibot/page/_wikibase.py
M tests/wikibase_tests.py
3 files changed, 52 insertions(+), 23 deletions(-)

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




diff --git a/pywikibot/page/_page.py b/pywikibot/page/_page.py
index 1b30fba..18eb1e0 100644
--- a/pywikibot/page/_page.py
+++ b/pywikibot/page/_page.py
@@ -9,7 +9,7 @@
    itself, including its contents.
 """
 #
-# (C) Pywikibot team, 2008-2024
+# (C) Pywikibot team, 2008-2025
 #
 # Distributed under the terms of the MIT license.
 #
@@ -197,33 +197,20 @@
 
         :raises UnknownExtensionError: site has no Wikibase extension
         """
-        def find_best_claim(claims):
-            """Find the first best ranked claim."""
-            index = None
-            for i, claim in enumerate(claims):
-                if claim.rank == 'preferred':
-                    return claim
-                if index is None and claim.rank == 'normal':
-                    index = i
-            if index is None:
-                index = 0
-            return claims[index]
-
-        if not self.site.has_data_repository:
-            raise UnknownExtensionError(
-                f'Wikibase is not implemented for {self.site}.')
-
-        def get_item_page(func, *args):
+        def get_item_page(page):
+            if not page.site.has_data_repository:
+                raise UnknownExtensionError(
+                    f'Wikibase is not implemented for {page.site}.')
             try:
-                item_p = func(*args)
+                item_p = page.data_item()
                 item_p.get()
                 return item_p
             except NoPageError:
                 return None
             except IsRedirectPageError:
-                return get_item_page(item_p.getRedirectTarget)
+                return get_item_page(item_p.getRedirectTarget())

-        item_page = get_item_page(pywikibot.ItemPage.fromPage, self)
-        if item_page and prop in item_page.claims:
-            return find_best_claim(item_page.claims[prop])
+        item_page = get_item_page(page=self)
+        if item_page:
+            return item_page.get_best_claim(prop)
         return None
diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py
index 5bf8dbd..bc2e5c2 100644
--- a/pywikibot/page/_wikibase.py
+++ b/pywikibot/page/_wikibase.py
@@ -1341,6 +1341,35 @@
             return self._isredir
         return super().isRedirectPage()

+    def get_best_claim(self, prop: str):
+        """Return the first best Claim for this page.
+
+        Return the first 'preferred' ranked Claim specified by Wikibase
+        property or the first 'normal' one otherwise.
+
+        :param prop: property id, "P###"
+        :return: Claim object given by Wikibase property number
+            for this page object.
+        :rtype: pywikibot.Claim or None
+
+        :raises UnknownExtensionError: site has no Wikibase extension
+        """
+        def find_best_claim(claims):
+            """Find the first best ranked claim."""
+            index = None
+            for i, claim in enumerate(claims):
+                if claim.rank == 'preferred':
+                    return claim
+                if index is None and claim.rank == 'normal':
+                    index = i
+            if index is None:
+                index = 0
+            return claims[index]
+
+        if prop in self.claims:
+            return find_best_claim(self.claims[prop])
+        return None
+

 class Property:

diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 4d1d77f..47a736d 100755
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -1497,6 +1497,19 @@
         self.assertEqual(diff, expected)


+class TestHighLevelApi(WikidataTestCase):
+
+    """Test high-level API for Wikidata."""
+
+    def test_get_best_claim(self) -> None:
+        """Test getting the best claim for a property."""
+        wikidata = self.get_repo()
+        item = pywikibot.ItemPage(wikidata, 'Q90')
+        item.get()
+        self.assertEqual(item.get_best_claim('P17').getTarget(),
+                         pywikibot.ItemPage(wikidata, 'Q142'))
+
+
 if __name__ == '__main__':
     with suppress(SystemExit):
         unittest.main()

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1173478?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: I2fddb91a130c1e76fbb8f93b08c2b50eaf9da41d
Gerrit-Change-Number: 1173478
Gerrit-PatchSet: 5
Gerrit-Owner: Strainu <[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