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

Change subject: [bugfix] Pass site to WbTime initializer in 
get_value_at_timestamp
......................................................................

[bugfix] Pass site to WbTime initializer in get_value_at_timestamp

- Pass self.site to WbTime initializer to avoid creating a datasite from
  the default site, which may cause failures.
- update docstrings and type annotations for clarity.
- Simplify helper functions and property checks for better readability.

Bug: T401546
Change-Id: Ib6a69a1b34dc30d664fa0cdfeb8726d5ecf17ce4
---
M pywikibot/page/_wikibase.py
M tests/wikibase_tests.py
2 files changed, 23 insertions(+), 19 deletions(-)

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




diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py
index 9c58032..b328e45 100644
--- a/pywikibot/page/_wikibase.py
+++ b/pywikibot/page/_wikibase.py
@@ -1380,24 +1380,27 @@
         prop: str,
         timestamp: pywikibot.WbTime,
         lang: str = 'en'
-    ):
+    ) -> pywikibot.WbRepresentation | None:
         """Return the best value for this page at a given timestamp.

+        .. versionadded:: 10.4
+
         :param prop: property id, "P###"
         :param timestamp: the timestamp to check the value at
         :param lang: the language to return the value in
-        :return: WbRepresentation object given by Wikibase property number for
-            this page object and valid for the given timestamp and language.
-        :rtype: pywikibot.WbRepresentation or None
+        :return: :class:`pywikibot.WbRepresentation` object given by
+            Wikibase property number for this page object and valid for
+            the given timestamp and language.

         :raises NoWikibaseEntityError: site has no time interval properties
         """
-        if not hasattr(self.site.family, 'interval_start_property') or \
-                not hasattr(self.site.family, 'interval_end_property'):
+        fam = self.site.family
+        if not hasattr(fam, 'interval_start_property') or \
+                not hasattr(fam, 'interval_end_property'):
             raise NoWikibaseEntityError(
-                f'{self.site.family} does not have time interval properties')
-        startp = self.site.family.interval_start_property
-        endp = self.site.family.interval_end_property
+                f'{fam} does not have time interval properties')
+
+        startp, endp = fam.interval_start_property, fam.interval_end_property

         def timestamp_in_interval(p, ts):
             """Check if timestamp is within the qualifiers."""
@@ -1418,22 +1421,23 @@

         def find_value_at_timestamp(claims, ts, language):
             """Find the first best ranked claim at a given timestamp."""
-            sorted_claims = sorted(claims,
-                                   key=lambda c: c.qualifiers.get(startp)[
-                                       0].getTarget() if c.qualifiers.get(
-                                       startp) else pywikibot.WbTime(0),
-                                   reverse=True)
+            sorted_claims = sorted(
+                claims,
+                key=(lambda c: c.qualifiers.get(startp)[0].getTarget()
+                     if c.qualifiers.get(startp)
+                     else pywikibot.WbTime(0, site=self.site)),
+                reverse=True
+            )
             for claim in sorted_claims:
                 if timestamp_in_interval(claim, ts):
-                    if (claim.type == 'monolingualtext'
-                            and claim.getTarget().language != language):
-                        continue
-                    else:
+                    if (claim.type != 'monolingualtext'
+                            or claim.getTarget().language == language):
                         return claim.getTarget()
             return None

         if prop in self.claims:
             return find_value_at_timestamp(self.claims[prop], timestamp, lang)
+
         return None


diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index e5fbac6..eb8c7df 100755
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -1529,7 +1529,7 @@
         self.assertEqual(claim.language, 'ru')

     def test_with_monolingual_wrong_language(self) -> None:
-        """Test getting a monolingual text claim with a good language."""
+        """Test getting a monolingual text claim with a wrong language."""
         wikidata = self.get_repo()
         item = pywikibot.ItemPage(wikidata, 'Q183')
         item.get()

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1176814?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: Ib6a69a1b34dc30d664fa0cdfeb8726d5ecf17ce4
Gerrit-Change-Number: 1176814
Gerrit-PatchSet: 2
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: 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