Skip to content

Commit 9bf3b28

Browse files
strainujenkins-bot
authored andcommitted
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 <github@strainu.ro>
1 parent 49d7916 commit 9bf3b28

File tree

3 files changed

+52
-23
lines changed

3 files changed

+52
-23
lines changed

pywikibot/page/_page.py

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
itself, including its contents.
1010
"""
1111
#
12-
# (C) Pywikibot team, 2008-2024
12+
# (C) Pywikibot team, 2008-2025
1313
#
1414
# Distributed under the terms of the MIT license.
1515
#
@@ -197,33 +197,20 @@ def get_best_claim(self, prop: str):
197197
198198
:raises UnknownExtensionError: site has no Wikibase extension
199199
"""
200-
def find_best_claim(claims):
201-
"""Find the first best ranked claim."""
202-
index = None
203-
for i, claim in enumerate(claims):
204-
if claim.rank == 'preferred':
205-
return claim
206-
if index is None and claim.rank == 'normal':
207-
index = i
208-
if index is None:
209-
index = 0
210-
return claims[index]
211-
212-
if not self.site.has_data_repository:
213-
raise UnknownExtensionError(
214-
f'Wikibase is not implemented for {self.site}.')
215-
216-
def get_item_page(func, *args):
200+
def get_item_page(page):
201+
if not page.site.has_data_repository:
202+
raise UnknownExtensionError(
203+
f'Wikibase is not implemented for {page.site}.')
217204
try:
218-
item_p = func(*args)
205+
item_p = page.data_item()
219206
item_p.get()
220207
return item_p
221208
except NoPageError:
222209
return None
223210
except IsRedirectPageError:
224-
return get_item_page(item_p.getRedirectTarget)
211+
return get_item_page(item_p.getRedirectTarget())
225212

226-
item_page = get_item_page(pywikibot.ItemPage.fromPage, self)
227-
if item_page and prop in item_page.claims:
228-
return find_best_claim(item_page.claims[prop])
213+
item_page = get_item_page(page=self)
214+
if item_page:
215+
return item_page.get_best_claim(prop)
229216
return None

pywikibot/page/_wikibase.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,6 +1341,35 @@ def isRedirectPage(self):
13411341
return self._isredir
13421342
return super().isRedirectPage()
13431343

1344+
def get_best_claim(self, prop: str):
1345+
"""Return the first best Claim for this page.
1346+
1347+
Return the first 'preferred' ranked Claim specified by Wikibase
1348+
property or the first 'normal' one otherwise.
1349+
1350+
:param prop: property id, "P###"
1351+
:return: Claim object given by Wikibase property number
1352+
for this page object.
1353+
:rtype: pywikibot.Claim or None
1354+
1355+
:raises UnknownExtensionError: site has no Wikibase extension
1356+
"""
1357+
def find_best_claim(claims):
1358+
"""Find the first best ranked claim."""
1359+
index = None
1360+
for i, claim in enumerate(claims):
1361+
if claim.rank == 'preferred':
1362+
return claim
1363+
if index is None and claim.rank == 'normal':
1364+
index = i
1365+
if index is None:
1366+
index = 0
1367+
return claims[index]
1368+
1369+
if prop in self.claims:
1370+
return find_best_claim(self.claims[prop])
1371+
return None
1372+
13441373

13451374
class Property:
13461375

tests/wikibase_tests.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,19 @@ def test_json_diff(self) -> None:
14971497
self.assertEqual(diff, expected)
14981498

14991499

1500+
class TestHighLevelApi(WikidataTestCase):
1501+
1502+
"""Test high-level API for Wikidata."""
1503+
1504+
def test_get_best_claim(self) -> None:
1505+
"""Test getting the best claim for a property."""
1506+
wikidata = self.get_repo()
1507+
item = pywikibot.ItemPage(wikidata, 'Q90')
1508+
item.get()
1509+
self.assertEqual(item.get_best_claim('P17').getTarget(),
1510+
pywikibot.ItemPage(wikidata, 'Q142'))
1511+
1512+
15001513
if __name__ == '__main__':
15011514
with suppress(SystemExit):
15021515
unittest.main()

0 commit comments

Comments
 (0)