Page MenuHomePhabricator

PyWikibot Breaks on Q668 because of P7829
Closed, ResolvedPublicBUG REPORT

Description

Steps to replicate the issue (include links if applicable):
Run the following script:

import pywikibot
site = pywikibot.Site('wikidata', 'wikidata')
repo = site.data_repository()
item = pywikibot.ItemPage(repo, 'Q668')
item.claims

What happens?:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
File ~/anaconda3/envs/tr/lib/python3.12/site-packages/pywikibot/tools/__init__.py:771, in cached.<locals>.wrapper(obj, force)
    770 try:
--> 771     return getattr(obj, cache_name)
    772 except AttributeError:

AttributeError: 'Claim' object has no attribute '_type'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
File ~/anaconda3/envs/tr/lib/python3.12/site-packages/pywikibot/site/_datasite.py:272, in DataSite.getPropertyType(self, prop)
    271 try:
--> 272     dtype = data['entities'][prop.getID()]['datatype']
    273 except KeyError:

KeyError: 'datatype'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
Cell In[10], line 5
      3 repo = site.data_repository()
      4 item = pywikibot.ItemPage(repo, 'Q668')
----> 5 item.claims
      6 print('a')

File ~/anaconda3/envs/tr/lib/python3.12/site-packages/pywikibot/page/_wikibase.py:140, in WikibaseEntity.__getattr__(self, name)
    138         self._initialize_empty()
    139         return getattr(self, name)
--> 140     return self.get()[name]
    142 raise AttributeError("'{}' object has no attribute '{}'"
    143                      .format(self.__class__.__name__, name))

File ~/anaconda3/envs/tr/lib/python3.12/site-packages/pywikibot/page/_wikibase.py:1166, in ItemPage.get(self, force, get_redirect, *args, **kwargs)
   1143 def get(
   1144     self,
   1145     force: bool = False,
   (...)
   1148     **kwargs
   1149 ) -> dict[str, Any]:
   1150     """
   1151     Fetch all item data, and cache it.
   1152 
   (...)
   1164        unwanted change to the live content
   1165     """
-> 1166     data = super().get(force, *args, **kwargs)
   1168     if self.isRedirectPage() and not get_redirect:
   1169         raise IsRedirectPageError(self)

File ~/anaconda3/envs/tr/lib/python3.12/site-packages/pywikibot/page/_wikibase.py:737, in WikibasePage.get(self, force, *args, **kwargs)
    735 lazy_loading_id = not hasattr(self, 'id') and hasattr(self, '_site')
    736 try:
--> 737     data = WikibaseEntity.get(self, force=force)
    738 except NoWikibaseEntityError:
    739     if lazy_loading_id:

File ~/anaconda3/envs/tr/lib/python3.12/site-packages/pywikibot/page/_wikibase.py:285, in WikibaseEntity.get(self, force)
    283 # This initializes all data
    284 for key, cls in self.DATA_ATTRIBUTES.items():
--> 285     value = cls.fromJSON(self._content.get(key, {}), self.repo)
    286     setattr(self, key, value)
    287     data[key] = value

File ~/anaconda3/envs/tr/lib/python3.12/site-packages/pywikibot/page/_collections.py:218, in ClaimCollection.fromJSON(cls, data, repo)
    216     return this
    217 for key, claims in data.items():
--> 218     this[key] = [pywikibot.page.Claim.fromJSON(repo, claim)
    219                  for claim in claims]
    220 return this

File ~/anaconda3/envs/tr/lib/python3.12/site-packages/pywikibot/page/_wikibase.py:1720, in Claim.fromJSON(cls, site, data)
   1718 value = data['mainsnak']['datavalue']['value']
   1719 # The default covers string, url types
-> 1720 if claim.type in cls.types:
   1721     claim.target = cls.TARGET_CONVERTER.get(
   1722         claim.type, lambda value, site: value)(value, site)
   1723 else:

File ~/anaconda3/envs/tr/lib/python3.12/site-packages/pywikibot/tools/__init__.py:773, in cached.<locals>.wrapper(obj, force)
    771     return getattr(obj, cache_name)
    772 except AttributeError:
--> 773     val = fn(obj)
    774     setattr(obj, cache_name, val)
    775     return val

File ~/anaconda3/envs/tr/lib/python3.12/site-packages/pywikibot/page/_wikibase.py:1404, in Property.type(self)
   1400 @property
   1401 @cached
   1402 def type(self) -> str:
   1403     """Return the type of this property."""
-> 1404     return self.repo.getPropertyType(self)

File ~/anaconda3/envs/tr/lib/python3.12/site-packages/pywikibot/site/_datasite.py:274, in DataSite.getPropertyType(self, prop)
    272     dtype = data['entities'][prop.getID()]['datatype']
    273 except KeyError:
--> 274     dtype = data['entities'][prop.getID().lower()]['datatype']
    276 return dtype

KeyError: 'p7829'

What should have happened instead?:
Return a pywikibot.page._collections.ClaimCollection with the claims of the Item.

Software version (on Special:Version page; skip for WMF-hosted wikis like Wikipedia):

  • Python 3.12.1
  • Pywikibot 9.2.0

Other information (browser name/version, screenshots, etc.):
I checked the wikidata page of the Q668 (https://www.wikidata.org/wiki/Q668) and p7829 appears marked as (Deleted Property)

Details

Related Changes in Gerrit:

Event Timeline

I removed the property: https://www.wikidata.org/w/index.php?title=Q668&diff=2193039158&oldid=2192338581

Now it works as expected:

import pywikibot
site = pywikibot.Site()
repo = site.data_repository()
item = pywikibot.ItemPage(repo, 'Q668')
claims = item.claims
len(claims)
319

Thank you @WilliamSM94 for finding out.

Change #1051096 had a related patch set uploaded (by Xqt; author: Xqt):

[pywikibot/core@master] [wikibase] give a more informative exception

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

Change #1051096 merged by jenkins-bot:

[pywikibot/core@master] [wikibase] give a more informative exception

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

Xqt claimed this task.