Page MenuHomePhabricator

AttributeError: 'ItemPage' object has no attribute 'claims'
Closed, ResolvedPublic

Description

When I do something like

item = ItemPage(self.repo, '-1')
item.editLabels({uselang: title})
item.addClaim(claim, bot=True)

(just assume the variables are defined, see https://gerrit.wikimedia.org/r/#/c/166629/14 for context), I get:

site.py", line 5629, in addClaim
    if claim.getID() in item.claims:
AttributeError: 'ItemPage' object has no attribute 'claims'
<type 'exceptions.AttributeError'>

Shouldn't that if check for existence of "item.claims"?

Event Timeline

Nemo_bis raised the priority of this task from to Needs Triage.
Nemo_bis updated the task description. (Show Details)
Nemo_bis added a project: Pywikibot-Wikidata.
Nemo_bis subscribed.
Restricted Application added subscribers: Aklapper, Unknown Object (MLST). · View Herald TranscriptApr 2 2015, 4:17 PM

Hmm I couldn't find such a line in addClaims. And that if has also an else branch, so it would also need an elif.

Sorry, paste error.

In my testing I just made it

# Update the item
try:
    if claim.getID() in item.claims:
        item.claims[claim.getID()].append(claim)
    else:
        item.claims[claim.getID()] = [claim]
except AttributeError:
    # claims were not defined
    pass

Change 201876 had a related patch set uploaded (by John Vandenberg):
Instantiate empty ItemPage with empty attributes

https://gerrit.wikimedia.org/r/201876

Xqt triaged this task as High priority.Feb 12 2019, 3:25 PM
Xqt claimed this task.
Xqt subscribed.

This is already solved:

>>> import pywikibot
>>> repo = pywikibot.Site().data_repository()
>>> item = pywikibot.ItemPage(repo, '-1')
>>> item.claims
<class 'pywikibot.page._collections.ClaimCollection'>({})

Change 201876 abandoned by Xqt:
[pywikibot/core@master] Instantiate empty ItemPage with empty attributes

Reason:
Already solved

https://gerrit.wikimedia.org/r/201876