jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/342494 )
Change subject: [BUG]Correct _wbtypes equality comparison ......................................................................
[BUG]Correct _wbtypes equality comparison
Current comparison of Coordinate requires `_entity` to be set and also compares `globe`. As these can take multiple forms yet still be equivalent a separate `__eq__` would be needed. A similar situation arose for unit comparison in WbQuantity.
Rather than ending up with multiple different `__eq__` simply update `_wbtypes.__eq__` to compare on `.toWikibase()`. By definition this will always compare all the relevant components but no entirely internal variables.
Bug:T160282 Change-Id: I6584e60782db554da46736cc9f12f47772ed91b1 --- M pywikibot/__init__.py M pywikibot/_wbtypes.py M tests/wikibase_tests.py 3 files changed, 26 insertions(+), 12 deletions(-)
Approvals: jenkins-bot: Verified Xqt: Looks good to me, approved
diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py index 45b08f5..799def8 100644 --- a/pywikibot/__init__.py +++ b/pywikibot/__init__.py @@ -674,16 +674,6 @@ return None return format(value, "+g")
- def __eq__(self, other): - """Override equality to handle different unit representations.""" - if isinstance(other, self.__class__): - self_dict = self.__dict__.copy() - other_dict = other.__dict__.copy() - self_dict['_unit'] = self.unit - other_dict['_unit'] = other.unit - return self_dict == other_dict - return NotImplemented - def __init__(self, amount, unit=None, error=None, site=None): u""" Create a new WbQuantity object. diff --git a/pywikibot/_wbtypes.py b/pywikibot/_wbtypes.py index dd3f5a5..cc68d67 100644 --- a/pywikibot/_wbtypes.py +++ b/pywikibot/_wbtypes.py @@ -46,7 +46,9 @@ return '{0}({1})'.format(self.__class__.__name__, attrs)
def __eq__(self, other): - return self.__dict__ == other.__dict__ + if isinstance(other, self.__class__): + return self.toWikibase() == other.toWikibase() + return NotImplemented
def __ne__(self, other): return not self.__eq__(other) diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index fbed2b3..3b2c3ea 100644 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -242,6 +242,28 @@ self.assertEqual(q.get_globe_item(test_repo), ItemPage(test_repo, 'Q123'))
+ def test_Coordinate_equality(self): + """Test Coordinate equality with different globe representations.""" + repo = self.get_repo() + a = pywikibot.Coordinate( + site=repo, lat=12.0, lon=13.0, precision=0.1, + globe='moon') + b = pywikibot.Coordinate( + site=repo, lat=12.0, lon=13.0, precision=0.1, + globe_item='http://www.wikidata.org/entity/Q405') + c = pywikibot.Coordinate( + site=repo, lat=12.0, lon=13.0, precision=0.1, + globe_item=ItemPage(repo, 'Q405')) + d = pywikibot.Coordinate( + site=repo, lat=12.0, lon=13.0, precision=0.1, + globe_item='http://test.wikidata.org/entity/Q405') + self.assertEqual(a, b) + self.assertEqual(b, c) + self.assertEqual(c, a) + self.assertNotEqual(a, d) + self.assertNotEqual(b, d) + self.assertNotEqual(c, d) +
class TestWbTime(WikidataTestCase):
@@ -359,7 +381,7 @@ "upperBound=%(val)s, lowerBound=%(val)s, " "unit=1)" % {'val': '0.044405586'})
- def test_WbQuantity_equality(self): + def test_WbQuantity_self_equality(self): """Test WbQuantity equality.""" repo = self.get_repo() q = pywikibot.WbQuantity(amount='0.044405586', error='0', site=repo)