If adding a claim, two types of errors are thrown in case of a deleted item:
item = pywikibot.ItemPage(repo, "Q726") target = pywikibot.ItemPage(repo, "Q14854094") c = pywikibot.Claim(repo, u'P171') c.addTarget(target) item.addClaim(c) # Error if item or target were deleted
At the last line, you get either
pywikibot.exceptions.NoPage: Page [[wikidata:-1]] doesn't exist.
if item (Q726) has been deleted or
pywikibot.data.api.APIError: invalid-snak-value: Q14854094 not found
if the target (Q14854094) has been deleted.
It would be great, if for the latter a specific "InvalidValueError" or similar would be thrown. Then, the error could be caught and handled specifically without the need to parse the (possibly changing) API error message itself.