jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/354968 )
Change subject: Introduce the new WbUnknown data type for Wikibase ......................................................................
Introduce the new WbUnknown data type for Wikibase
This can be used later to store claims with an unknown data type.
Bug: T165961 Change-Id: Ib65eb0f34cffa3c058c4e01ff79c145002310b2a --- M pywikibot/__init__.py M tests/wikibase_tests.py 2 files changed, 68 insertions(+), 0 deletions(-)
Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py index 8a1ea57..3b252e7 100644 --- a/pywikibot/__init__.py +++ b/pywikibot/__init__.py @@ -1024,6 +1024,48 @@ return cls(page, site)
+class WbUnknown(_WbRepresentation): + """ + A Wikibase representation for unknown data type. + + This will prevent the bot from breaking completely when a new type + is introduced. + + This data type is just a json container + """ + + _items = ('json',) + + def __init__(self, json): + """ + Create a new WbUnknown object. + + @param json: Wikibase JSON + @type: dict + """ + self.json = json + + def toWikibase(self): + """ + Return the JSON object for the Wikibase API. + + @return: Wikibase JSON + @rtype: dict + """ + return self.json + + @classmethod + def fromWikibase(cls, json): + """ + Create a WbUnknown from the JSON data given by the Wikibase API. + + @param json: Wikibase JSON + @type json: dict + @rtype: pywikibot.WbUnknown + """ + return cls(json) + + _sites = {} _url_cache = {} # The code/fam pair for each URL
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index d66a9d9..00459a3 100644 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -746,6 +746,32 @@ non_map_page, self.get_repo())
+class TestWbUnknown(WikidataTestCase): + + """Test Wikibase WbUnknown data type.""" + + dry = True + + def test_WbUnknown_string(self): + """Test WbUnknown string.""" + q_dict = {'text': 'Test that basics work', 'language': 'en'} + q = pywikibot.WbUnknown(q_dict) + self.assertEqual(q.toWikibase(), q_dict) + + def test_WbUnknown_equality(self): + """Test WbUnknown equality.""" + q_dict = {'text': 'Thou shall test this!', 'language': 'unknown'} + q = pywikibot.WbUnknown(q_dict) + self.assertEqual(q, q) + + def test_WbUnknown_fromWikibase(self): + """Test WbUnknown.fromWikibase() instantiating.""" + q = pywikibot.WbUnknown.fromWikibase({'text': 'Test this!', + 'language': 'en'}) + self.assertEqual(q.toWikibase(), + {'text': 'Test this!', 'language': 'en'}) + + class TestItemPageExtensibility(TestCase):
"""Test ItemPage extensibility."""
pywikibot-commits@lists.wikimedia.org