jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/512491 )
Change subject: Fix Claim.copy to make tests pass ......................................................................
Fix Claim.copy to make tests pass
I decided to do this in a dirty but quite simple way. The reason is the code, which mixes methods claims, qualifiers and references. This all needs to be rewritten (T186200).
Bug: T220131 Change-Id: I47f8e4859c7a35520753dc29858d324d067ec76a --- M pywikibot/page.py M tests/wikibase_tests.py 2 files changed, 17 insertions(+), 12 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index 026e607..b7f2dbc 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -4981,16 +4981,17 @@
@rtype: Claim """ - data = self.toJSON() - if self.isQualifier: - ret = self.qualifierFromJSON(self.repo, data) - elif self.isReference: - ret = self.referenceFromJSON(self.repo, data) - ret.hash = None - else: - ret = self.fromJSON(self.repo, data) - ret.snak = None - return ret + is_qualifier = self.isQualifier + is_reference = self.isReference + self.isQualifier = False + self.isReference = False + copy = self.fromJSON(self.repo, self.toJSON()) + for cl in (self, copy): + cl.isQualifier = is_qualifier + cl.isReference = is_reference + copy.hash = None + copy.snak = None + return copy
@classmethod def fromJSON(cls, site, data): @@ -5054,7 +5055,7 @@ for prop in prop_list: for claimsnak in data['snaks'][prop]: claim = cls.fromJSON(site, {'mainsnak': claimsnak, - 'hash': data['hash']}) + 'hash': data.get('hash')}) claim.isReference = True if claim.getID() not in source: source[claim.getID()] = [] @@ -5073,7 +5074,7 @@ @rtype: Claim """ claim = cls.fromJSON(site, {'mainsnak': data, - 'hash': data['hash']}) + 'hash': data.get('hash')}) claim.isQualifier = True return claim
diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index 38787c5..80aff1d 100644 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -1588,6 +1588,8 @@ qualifier.setTarget('foo') copy = qualifier.copy() self.assertEqual(qualifier, copy) + self.assertTrue(qualifier.isQualifier) + self.assertTrue(copy.isQualifier)
def test_claim_copy_is_equal_source(self): """ @@ -1600,6 +1602,8 @@ source.setTarget(pywikibot.ItemPage(wikidata, 'Q328')) copy = source.copy() self.assertEqual(source, copy) + self.assertTrue(source.isReference) + self.assertTrue(copy.isReference)
class TestClaimSetValue(WikidataTestCase):
pywikibot-commits@lists.wikimedia.org