jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/358786 )
Change subject: Make WbRepresentations hashable
......................................................................
Make WbRepresentations hashable
Bug: T167827
Change-Id: If6631bdcd430a4f067ac05b790dadacba3d8f343
---
M pywikibot/__init__.py
M pywikibot/_wbtypes.py
M tests/wikibase_tests.py
3 files changed, 78 insertions(+), 9 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 4856a66..e376535 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -984,6 +984,10 @@
specifics['ending'], specifics['label'])
self.page = page
+ def __hash__(self):
+ """Override super.hash() as toWikibase is a string for
_WbDataPage."""
+ return hash(self.toWikibase())
+
def toWikibase(self):
"""
Convert the data to the value required by the Wikibase API.
diff --git a/pywikibot/_wbtypes.py b/pywikibot/_wbtypes.py
index cc68d67..7071ac4 100644
--- a/pywikibot/_wbtypes.py
+++ b/pywikibot/_wbtypes.py
@@ -50,5 +50,8 @@
return self.toWikibase() == other.toWikibase()
return NotImplemented
+ def __hash__(self):
+ return hash(frozenset(self.toWikibase().items()))
+
def __ne__(self, other):
return not self.__eq__(other)
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py
index 00459a3..1a206ee 100644
--- a/tests/wikibase_tests.py
+++ b/tests/wikibase_tests.py
@@ -44,6 +44,20 @@
return page
+class WbRepresentationTestCase(WikidataTestCase):
+
+ """Test methods inherited or extended from
_WbRepresentation."""
+
+ def setUp(self):
+ """Setup tests."""
+ super(WbRepresentationTestCase, self).setUp()
+
+ def _test_hashable(self, representation):
+ """Test that the representation is hashable."""
+ list_of_dupes = [representation, representation]
+ self.assertEqual(len(set(list_of_dupes)), 1)
+
+
class TestLoadRevisionsCaching(BasePageLoadRevisionsCachingTestBase,
WikidataTestCase):
@@ -136,11 +150,19 @@
ItemPage(self.get_repo(), 'q5296'))
-class TestWikibaseCoordinate(WikidataTestCase):
+class TestWikibaseCoordinate(WbRepresentationTestCase):
"""Test Wikibase Coordinate data type."""
dry = True
+
+ def test_Coordinate_WbRepresentation_methods(self):
+ """Test inherited or extended methods from
_WbRepresentation."""
+ repo = self.get_repo()
+ coord = pywikibot.Coordinate(
+ site=repo, lat=12.0, lon=13.0, precision=0,
+ globe='moon')
+ self._test_hashable(coord)
def test_Coordinate_dim(self):
"""Test Coordinate dimension."""
@@ -178,7 +200,7 @@
'globe':
'http://www.wikidata.org/entity/Q123'})
-class TestWikibaseCoordinateNonDry(WikidataTestCase):
+class TestWikibaseCoordinateNonDry(WbRepresentationTestCase):
"""
Test Wikibase Coordinate data type (non-dry).
@@ -262,11 +284,18 @@
self.assertNotEqual(c, d)
-class TestWbTime(WikidataTestCase):
+class TestWbTime(WbRepresentationTestCase):
"""Test Wikibase WbTime data type."""
dry = True
+
+ def test_WbTime_WbRepresentation_methods(self):
+ """Test inherited or extended methods from
_WbRepresentation."""
+ repo = self.get_repo()
+ t = pywikibot.WbTime(site=repo, year=2010, month=0, day=0, hour=12,
+ minute=43)
+ self._test_hashable(t)
def test_WbTime_timestr(self):
"""Test timestr functions of WbTime."""
@@ -335,11 +364,17 @@
precision='invalid_precision')
-class TestWbQuantity(WikidataTestCase):
+class TestWbQuantity(WbRepresentationTestCase):
"""Test Wikibase WbQuantity data type."""
dry = True
+
+ def test_WbQuantity_WbRepresentation_methods(self):
+ """Test inherited or extended methods from
_WbRepresentation."""
+ repo = self.get_repo()
+ q = pywikibot.WbQuantity(amount=1234, error=1, site=repo)
+ self._test_hashable(q)
def test_WbQuantity_integer(self):
"""Test WbQuantity for integer value."""
@@ -457,7 +492,7 @@
'unit':
'http://www.wikidata.org/entity/Q712226', })
-class TestWbQuantityNonDry(WikidataTestCase):
+class TestWbQuantityNonDry(WbRepresentationTestCase):
"""
Test Wikibase WbQuantity data type (non-dry).
@@ -574,11 +609,17 @@
ItemPage(test_repo, 'Q123'))
-class TestWbMonolingualText(WikidataTestCase):
+class TestWbMonolingualText(WbRepresentationTestCase):
"""Test Wikibase WbMonolingualText data type."""
dry = True
+
+ def test_WbMonolingualText_WbRepresentation_methods(self):
+ """Test inherited or extended methods from
_WbRepresentation."""
+ q = pywikibot.WbMonolingualText(
+ text='Test that basics work', language='en')
+ self._test_hashable(q)
def test_WbMonolingualText_string(self):
"""Test WbMonolingualText string."""
@@ -614,7 +655,7 @@
text=None, language='sv')
-class TestWbGeoShapeNonDry(WikidataTestCase):
+class TestWbGeoShapeNonDry(WbRepresentationTestCase):
"""
Test Wikibase WbGeoShape data type (non-dry).
@@ -627,6 +668,11 @@
self.commons = pywikibot.Site('commons', 'commons')
self.page = Page(self.commons, 'Data:Lyngby Hovedgade.map')
super(TestWbGeoShapeNonDry, self).setUp()
+
+ def test_WbGeoShape_WbRepresentation_methods(self):
+ """Test inherited or extended methods from
_WbRepresentation."""
+ q = pywikibot.WbGeoShape(self.page)
+ self._test_hashable(q)
def test_WbGeoShape_page(self):
"""Test WbGeoShape page."""
@@ -680,7 +726,7 @@
non_map_page, self.get_repo())
-class TestWbTabularDataNonDry(WikidataTestCase):
+class TestWbTabularDataNonDry(WbRepresentationTestCase):
"""
Test Wikibase WbTabularData data type (non-dry).
@@ -693,6 +739,11 @@
self.commons = pywikibot.Site('commons', 'commons')
self.page = Page(self.commons, 'Data:Bea.gov/GDP by state.tab')
super(TestWbTabularDataNonDry, self).setUp()
+
+ def test_WbTabularData_WbRepresentation_methods(self):
+ """Test inherited or extended methods from
_WbRepresentation."""
+ q = pywikibot.WbTabularData(self.page)
+ self._test_hashable(q)
def test_WbTabularData_page(self):
"""Test WbTabularData page."""
@@ -746,11 +797,17 @@
non_map_page, self.get_repo())
-class TestWbUnknown(WikidataTestCase):
+class TestWbUnknown(WbRepresentationTestCase):
"""Test Wikibase WbUnknown data type."""
dry = True
+
+ def test_WbUnknown_WbRepresentation_methods(self):
+ """Test inherited or extended methods from
_WbRepresentation."""
+ q_dict = {'text': 'Test that basics work', 'language':
'en'}
+ q = pywikibot.WbUnknown(q_dict)
+ self._test_hashable(q)
def test_WbUnknown_string(self):
"""Test WbUnknown string."""
@@ -1345,6 +1402,11 @@
self._test_invoke()
self._test_no_wikitext()
+ def test_item_is_hashable(self):
+ """Ensure that ItemPages are hashable."""
+ list_of_dupes = [self._page, self._page]
+ self.assertEqual(len(set(list_of_dupes)), 1)
+
class TestPageMethodsWithItemTitle(WikidataTestCase, BasePageMethodsTestBase):
--
To view, visit
https://gerrit.wikimedia.org/r/358786
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: If6631bdcd430a4f067ac05b790dadacba3d8f343
Gerrit-PatchSet: 4
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Lokal Profil <lokal.profil(a)gmail.com>
Gerrit-Reviewer: Dalba <dalba.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Lokal Profil <lokal.profil(a)gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski(a)gmail.com>
Gerrit-Reviewer: Merlijn van Deen <valhallasw(a)arctus.nl>
Gerrit-Reviewer: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: Multichill <maarten(a)mdammers.nl>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>