Page MenuHomePhabricator

Unable to repr claim without target
Closed, ResolvedPublicBUG REPORT

Description

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

  • repr(pywikibot.Claim(site, "P31"))

What happens?:

  File "<input>", line 1, in <module>
  File "/Users/User/.local/share/virtualenvs/wikidata-bot-framework-Uh4zljqI/lib/python3.10/site-packages/pywikibot/page/_wikibase.py", line 1437, in __repr__
    repr(self.repo), self.toJSON(), cls_name=type(self).__name__)
  File "/Users/User/.local/share/virtualenvs/wikidata-bot-framework-Uh4zljqI/lib/python3.10/site-packages/pywikibot/page/_wikibase.py", line 1605, in toJSON
    data['mainsnak']['datavalue'] = self._formatDataValue()
  File "/Users/User/.local/share/virtualenvs/wikidata-bot-framework-Uh4zljqI/lib/python3.10/site-packages/pywikibot/page/_wikibase.py", line 1924, in _formatDataValue
    'value': self._formatValue(),
  File "/Users/User/.local/share/virtualenvs/wikidata-bot-framework-Uh4zljqI/lib/python3.10/site-packages/pywikibot/page/_wikibase.py", line 1896, in _formatValue
    value = {'entity-type': self.getTarget().entity_type,
AttributeError: 'NoneType' object has no attribute 'entity_type'

What should have happened instead?: A repr of some sort should show up

Software version (skip for WMF-hosted wikis like Wikipedia):

Other information (browser name/version, screenshots, etc.):

Event Timeline

@RPI2026F1: pywikibot.Claim(site) is invalid because the positional argument pid is missing. Could you please revise this task to be reproducable.

@Xqt alright, sorry I missed that somehow

I could reproduce it with the current master release:

import pywikibot
site = pywikibot.Site('wikidata')
claim = pywikibot.Claim(site, 'P31')
claim
Traceback (most recent call last):
  File "<pyshell#29>", line 1, in <module>
    claim
  File "C:\Python311\Lib\idlelib\rpc.py", line 620, in displayhook
    text = repr(value)
  File "D:\pwb\GIT\core\pywikibot\page\_wikibase.py", line 1462, in __repr__
    repr(self.repo), self.toJSON(), cls_name=type(self).__name__)
  File "D:\pwb\GIT\core\pywikibot\page\_wikibase.py", line 1631, in toJSON
    data['mainsnak']['datavalue'] = self._formatDataValue()
  File "D:\pwb\GIT\core\pywikibot\page\_wikibase.py", line 1963, in _formatDataValue
    'value': self._formatValue(),
  File "D:\pwb\GIT\core\pywikibot\page\_wikibase.py", line 1933, in _formatValue
    value = {'entity-type': self.getTarget().entity_type,
AttributeError: 'NoneType' object has no attribute 'entity_type'

Maybe a solution can be to display a REPR of <Incomplete Claim P31 site=pywikibot.Site(...)> or something along those lines.

Maybe a solution can be to display a REPR of <Incomplete Claim P31 site=pywikibot.Site(...)> or something along those lines.

I think the problem is that P31 is represented by a PropertyPage and not by a Claim. The Claim initializer should raise an InvalidTitleError (or something like that) in this case.

Change 876354 had a related patch set uploaded (by Matěj Suchánek; author: Matěj Suchánek):

[pywikibot/core@master] [bugfix] Fix Claim.__repr__ for stub instances

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

Xqt assigned this task to matej_suchanek.
Xqt triaged this task as High priority.

Change 876354 merged by jenkins-bot:

[pywikibot/core@master] [bugfix] Fix Claim.__repr__ for stub instances

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