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

Change subject: Wikibase: Fix get_value_at_timestamp
......................................................................

Wikibase: Fix get_value_at_timestamp

We need to keep the rank of the claims in the loop.

Bug: T407701
Change-Id: Ie211919095e7d68fc075434b31ea59de9c84a640
Signed-off-by: Strainu <[email protected]>
---
M pywikibot/page/_wikibase.py
1 file changed, 18 insertions(+), 3 deletions(-)

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




diff --git a/pywikibot/page/_wikibase.py b/pywikibot/page/_wikibase.py
index 1ef035b..f7d99c9 100644
--- a/pywikibot/page/_wikibase.py
+++ b/pywikibot/page/_wikibase.py
@@ -1430,12 +1430,16 @@
                      else pywikibot.WbTime(0, site=self.site)),
                 reverse=True
             )
+            best_claim = None
             for claim in sorted_claims:
+                if claim.rank == 'deprecated':
+                    continue
                 if timestamp_in_interval(claim, ts):
                     if (claim.type != 'monolingualtext'
-                            or claim.getTarget().language == language):
-                        return claim.getTarget()
-            return None
+                            or claim.getTarget().language == language)\
+                            and claim.has_better_rank(best_claim):
+                        best_claim = claim
+            return best_claim and best_claim.getTarget()

         if prop in self.claims:
             return find_value_at_timestamp(self.claims[prop], timestamp, lang)
@@ -2235,6 +2239,17 @@
             'type': self.value_types.get(self.type, self.type)
         }

+    def has_better_rank(self, other) -> bool:
+        """Check if this claim has a better rank than the other claim.
+
+        :param other: The other claim to compare with.
+        :return: True if this claim has a better rank, False otherwise.
+        """
+        if other is None:
+            return True
+        rank_order = {'preferred': 3, 'normal': 2, 'deprecated': 1}
+        return rank_order.get(self.rank, 0) > rank_order.get(other.rank, 0)
+

 class LexemePage(WikibasePage):


--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1197039?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: Ie211919095e7d68fc075434b31ea59de9c84a640
Gerrit-Change-Number: 1197039
Gerrit-PatchSet: 3
Gerrit-Owner: Strainu <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
Gerrit-CC: DErenrich <[email protected]>
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to