Page MenuHomePhabricator

pywikibot cannot save too precise WbTime claims
Closed, DuplicatePublicBUG REPORT


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

  • generate wbtime with date and time: pywikibot.WbTime(dt.year, dt.month,, dt.hour, dt.minute, dt.second)
  • set wbtime into target: .setTarget(wbtime)
  • this results in error: "Malformed input: +2024-02-13T06:35:51Z"
  • the example works with time (only date): pywikibot.WbTime(dt.year, dt.month,
  • expectation is that when wbtime is generated by given parameters pywikibot would know how to handle what it generates for itself
  • preferred way would be that it would accept python datetime correctly, but it doesn't handle that either

What happens?:

What should have happened instead?:

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

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

Event Timeline

Ipr1 updated the task description. (Show Details)
Ipr1 updated the task description. (Show Details)

Please add example code to ticket howto replicate the bug.

import pywikibot

wikidata_site = pywikibot.Site("wikidata", "wikidata")  
commonssite = pywikibot.Site("commons", "commons")

# note! to trigger bug, must be something that doesn't have SDC data yet
# so make fresh upload if there is already something
page = pywikibot.FilePage(commonssite, 'File:Viktor Heikel 1890s.jpg')

filepage =  pywikibot.FilePage(page)
file_info = filepage.latest_file_info

wditem = page.data_item()  # Get the data item associated with the page
sdcdata = wditem.get_data_for_new_entity() # get new sdc item

hashval = "85849afb31cf446d"
wbtime = pywikibot.WbTime(2023, 8, 27, 5, 48, 00)

p_claim = pywikibot.Claim(wikidata_site, 'P9310')

det_quali = pywikibot.Claim(wikidata_site, 'P459', is_qualifier=True)  # determination method
qualifier_targetimagehash = pywikibot.ItemPage(wikidata_site, "Q104884110")
p_claim.addQualifier(det_quali, summary='Adding reference URL qualifier')

i_claim = pywikibot.Claim(wikidata_site, 'P571', is_qualifier=True) # inception time

commonssite.addClaim(wditem, p_claim)

result from example: pywikibot.exceptions.APIError: modification-failed: Malformed input: +2023-08-27T05:48:00Z
messages: [{'name': 'wikibase-validator-malformed-value', 'parameters': ['+2023-08-27T05:48:00Z'], 'html': {'*': 'Malformed input: +2023-08-27T05:48:00Z'}}];
servedby: mw2285;

It seems that most accurate time what Structure data on Commons allows is 11 ( YYYY-MM-DD ). Hours, minutes and seconds needs to be zero if they are defined. Precision value needs to be 11 or below if it is defined.

Ie these are valid times



pywikibot.WbTime(2023, 8, 27, 0, 0, 0)
Zache renamed this task from pywikibot cannot handle the strings it generates for itself to pywikibot cannot save too precise WbTime claims .Feb 16 2024, 11:57 AM
Zache edited projects, added Fiwiki-Wikidata-Commons, Pywikibot; removed WMFI.

Confirmed that the precision is limited to YYYY-MM-DD (WbTime precision 11) in Wikidata too.

@matej_suchanek @Xqt . Do you think that it would be good idea to set a 1.) exception if user tries to set more exact time than day 2.) visible warning OR 3.) automatically round the WbTime value to date?

3 is a big no-no because the behavior wouldn't match the user's expectations. 1 and 2 are ultimately the same (there would be an exception anyway). So this is just about showing a more user-friendly error message (instead of the default).
Note that this is blocked on T57755, which is an upstream task. If we chose to hardcode something and then upstream changed, it could be problematic because users would be forced to upgrade Pywikibot version.

(Duplicate of T325860?)

Would the WbTime. toWikibase() be suitable place for warning?

I considered placing it on API call result handling, but it was messy as there are multiple ways to do such API calls, and there are no unified error messages from the server.

The error message could be something like this if hour, minute or second are true:

pywikibot.warning('WbTime warning. It is not possible to enter time values more precise than 1 day to Wikibase. Current value is "+2024-02-13T00:10:00Z" . See')