jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/512415 )
Change subject: [IMPR] [FIX] Introduce Claim.copy and prevent adding already saved claims ......................................................................
[IMPR] [FIX] Introduce Claim.copy and prevent adding already saved claims
This is potentially a breaking change.
Bug: T220131 Change-Id: I48967273c038fa394eb791c575cd796c2a88b2c3 --- M pywikibot/page.py M scripts/claimit.py M tests/wikibase_tests.py 3 files changed, 70 insertions(+), 1 deletion(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/page.py b/pywikibot/page.py index ae28f9d..1e06f64 100644 --- a/pywikibot/page.py +++ b/pywikibot/page.py @@ -4300,6 +4300,9 @@ were successful. @type callback: callable """ + if claim.on_item is not None: + raise ValueError( + 'The provided Claim instance is already used in an entity') self.repo.addClaim(self, claim, bot=bot, **kwargs) claim.on_item = self for snaks in claim.qualifiers.values(): @@ -4960,6 +4963,23 @@ def __ne__(self, other): return not self.__eq__(other)
+ def copy(self): + """ + Create an independent copy of this object. + + @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 + @classmethod def fromJSON(cls, site, data): """ @@ -5209,6 +5229,10 @@ @param claims: the claims to add @type claims: list of pywikibot.Claim """ + for claim in claims: + if claim.on_item is not None: + raise ValueError( + 'The provided Claim instance is already used in an entity') if self.on_item is not None: data = self.repo.editSource(self, claims, new=True, **kwargs) self.on_item.latest_revision_id = data['pageinfo']['lastrevid'] @@ -5249,6 +5273,9 @@ @param qualifier: the qualifier to add @type qualifier: Claim """ + if qualifier.on_item is not None: + raise ValueError( + 'The provided Claim instance is already used in an entity') if self.on_item is not None: data = self.repo.editQualifier(self, qualifier, **kwargs) self.on_item.latest_revision_id = data['pageinfo']['lastrevid'] diff --git a/scripts/claimit.py b/scripts/claimit.py index 3327f83..bdef4e1 100755 --- a/scripts/claimit.py +++ b/scripts/claimit.py @@ -95,7 +95,7 @@ # The generator might yield pages from multiple sites site = page.site if page is not None else None self.user_add_claim_unless_exists( - item, claim, self.exists_arg, site) + item, claim.copy(), self.exists_arg, site)
def main(*args): diff --git a/tests/wikibase_tests.py b/tests/wikibase_tests.py index 95ae131..38787c5 100644 --- a/tests/wikibase_tests.py +++ b/tests/wikibase_tests.py @@ -1559,6 +1559,48 @@ self.assertEqual(claim1, claim2) self.assertEqual(claim2, claim1)
+ def test_claim_copy_is_equal(self): + """ + Test making a copy of a claim. + + The copy of a claim should be always equal to the claim. + """ + wikidata = self.get_repo() + claim = pywikibot.Claim(wikidata, 'P31') + claim.setTarget(pywikibot.ItemPage(wikidata, 'Q5')) + qualifier = pywikibot.Claim(wikidata, 'P214', is_qualifier=True) + qualifier.setTarget('foo') + source = pywikibot.Claim(wikidata, 'P143', is_reference=True) + source.setTarget(pywikibot.ItemPage(wikidata, 'Q328')) + claim.addQualifier(qualifier) + claim.addSource(source) + copy = claim.copy() + self.assertEqual(claim, copy) + + def test_claim_copy_is_equal_qualifier(self): + """ + Test making a copy of a claim. + + The copy of a qualifier should be always equal to the qualifier. + """ + wikidata = self.get_repo() + qualifier = pywikibot.Claim(wikidata, 'P214', is_qualifier=True) + qualifier.setTarget('foo') + copy = qualifier.copy() + self.assertEqual(qualifier, copy) + + def test_claim_copy_is_equal_source(self): + """ + Test making a copy of a claim. + + The copy of a source should be always equal to the source. + """ + wikidata = self.get_repo() + source = pywikibot.Claim(wikidata, 'P143', is_reference=True) + source.setTarget(pywikibot.ItemPage(wikidata, 'Q328')) + copy = source.copy() + self.assertEqual(source, copy) +
class TestClaimSetValue(WikidataTestCase):
pywikibot-commits@lists.wikimedia.org