Fix Claims.add() default to preserve existing claims instead of replacing them
Claims.add() was deleting all existing claims with the same property when adding a new claim, contrary to user expectations. The default behavior was REPLACE_ALL, marking existing claims for removal on write.
Changes
- Changed default
action_if_existsparameter inClaims.add()fromREPLACE_ALLtoAPPEND_OR_REPLACE - Added test coverage for the issue scenario and different
action_if_existsbehaviors
Impact
Before:
item = wbi.item.get('Q105624761')
item.claims.get('P1476') # Returns 2 existing titles (de, fr)
title_en = MonolingualText(text='English Title', language='en', prop_nr='P1476')
item.claims.add(title_en)
item.write()
# Result: Only English title exists, German and French deleted ❌
After:
item = wbi.item.get('Q105624761')
item.claims.get('P1476') # Returns 2 existing titles (de, fr)
title_en = MonolingualText(text='English Title', language='en', prop_nr='P1476')
item.claims.add(title_en)
item.write()
# Result: All 3 titles exist (de, fr, en) ✅
Users wanting replace-all behavior can explicitly specify action_if_exists=ActionIfExists.REPLACE_ALL. This aligns Claims.add() with BaseEntity.add_claims(), which already defaulted to APPEND_OR_REPLACE.
[!WARNING]
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
httpbin.org
- Triggering command:
/home/REDACTED/.cache/pypoetry/virtualenvs/wikibaseintegrator-h1tmzOrO-py3.12/bin/python /home/REDACTED/.cache/pypoetry/virtualenvs/wikibaseintegrator-h1tmzOrO-py3.12/bin/pytest test/test_entity_item.py -v(dns block)If you need me to access, download, or install something from one of these locations, you can either:
- Configure Actions setup steps to set up my environment, which run before the firewall is enabled
- Add the appropriate URLs or hosts to the custom allowlist in this repository's Copilot coding agent settings (admins only)
Original prompt
This section details on the original issue you should resolve
<issue_title>Adding claim deletes other claims?</issue_title> <issue_description>I'm trying to add a Claim to a wikidata item, but instead of adding it and keeping all other claims with same property, the tool deletes all other claims and replaces them.
Specifically,
flametti = wbi.item.get(entity_id='Q105624761')flametti.claims.get(PROPS['title'])shows the entity has several claims about regarding title, two of them in German, but none in English (yet) so I'd like to add it:title_en_string = datatypes.MonolingualText(text='Flametti, or The Dandyism of the Poor', language='en', prop_nr=PROPS['title'])flametti.claims.add(title_en_string)If I doflametti.claims.get(PROPS['title'])indeed the local object it has four or so titles, now, as it should. But afterflametti.write(login=login_instance)the entity ends up having only one title, the one I just added. Is this expected behaviour? What am I doing wrong?</issue_description>Comments on the Issue (you are @copilot in this section)
- Fixes LeMyst/WikibaseIntegrator#922
💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.