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

Change subject: page._collections: Improve repr string for BaseDataDict and 
ClaimCollection
......................................................................

page._collections: Improve repr string for BaseDataDict and ClaimCollection

- use __class__.__name__ for class identifier
- use reprlib.repr to shorten long data repr strings
- add tests for repr and str functions

Change-Id: I32e7909a257e0864f4bd63c57af3e2de98f28342
---
M pywikibot/page/_collections.py
M tests/wikibase_tests.py
2 files changed, 25 insertions(+), 3 deletions(-)

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




diff --git a/pywikibot/page/_collections.py b/pywikibot/page/_collections.py
index 0bcc05c..b3b7984 100644
--- a/pywikibot/page/_collections.py
+++ b/pywikibot/page/_collections.py
@@ -1,11 +1,12 @@
 """Structures holding data for Wikibase entities."""
 #
-# (C) Pywikibot team, 2019-2024
+# (C) Pywikibot team, 2019-2025
 #
 # Distributed under the terms of the MIT license.
 #
 from __future__ import annotations

+import reprlib
 from collections import defaultdict
 from collections.abc import MutableMapping, MutableSequence
 from typing import Any
@@ -65,7 +66,7 @@
         return key in self._data

     def __repr__(self) -> str:
-        return f'{type(self)}({self._data})'
+        return f'{type(self).__name__}({reprlib.repr(self._data)})'

     @staticmethod
     def normalizeKey(key) -> str:
@@ -241,7 +242,7 @@
         return key in self._data

     def __repr__(self) -> str:
-        return f'{type(self)}({self._data})'
+        return f'{type(self).__name__}({reprlib.repr(self._data)})'

     @classmethod
     def normalizeData(cls, data) -> dict:
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 14ef333..89592bd 100755
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -324,8 +324,12 @@
         item = ItemPage(wikidata)
         self.assertEqual(item._link._title, '-1')
         self.assertLength(item.labels, 0)
+        self.assertEqual(str(item.labels), 'LanguageDict({})')
+        self.assertEqual(repr(item.labels), 'LanguageDict({})')
         self.assertLength(item.descriptions, 0)
         self.assertLength(item.aliases, 0)
+        self.assertEqual(str(item.aliases), 'AliasesDict({})')
+        self.assertEqual(repr(item.aliases), 'AliasesDict({})')
         self.assertLength(item.claims, 0)
         self.assertLength(item.sitelinks, 0)

@@ -1453,6 +1457,23 @@
         self.assertIn('en', item.aliases)
         self.assertIn('NYC', item.aliases['en'])

+    def test_str_repr(self) -> None:
+        """Test str and repr of labels and aliases."""
+        self.assertEqual(
+            str(self.wdp.labels),
+            "LanguageDict({'af': 'New York Stad', 'als': 'New York City', "
+            "'am': 'ኒው ዮርክ ከተማ', 'an': 'Nueva York', ...})"
+        )
+        self.assertEqual(
+            str(self.wdp.aliases),
+            "AliasesDict({'be': ['Горад Нью-Ёрк'], 'be-tarask': ['Нью Ёрк'], "
+            "'ca': ['Ciutat de Nova York', 'New York City',"
+            " 'New York City (New York)', 'NYC', 'N. Y.', 'N Y'], "
+            "'da': ['New York City'], ...})"
+        )
+        self.assertEqual(str(self.wdp.labels), repr(self.wdp.labels))
+        self.assertEqual(str(self.wdp.aliases), repr(self.wdp.aliases))
+
     def test_itempage_json(self) -> None:
         """Test itempage json."""
         old = json.dumps(self.wdp._content, indent=2, sort_keys=True)

--
To view, visit 
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1179272?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: I32e7909a257e0864f4bd63c57af3e2de98f28342
Gerrit-Change-Number: 1179272
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[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