jenkins-bot submitted this change.

View Change

Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
[bugfix] Match WbRepresentation's abstract method signature

WbRepresentation defines an abstract fromWikibase method which most
subclasses don't match. For most the difference is trivial (missing
a 'site' argument or its default).

Our _WbDataPage class has a more substantive difference. I don't
know how we'd care to handle this so simply added a TODO for now.

Change-Id: Ide15a7a0a234010bd6c5989a2601ea62eae514fb
---
M pywikibot/__init__.py
M pywikibot/_wbtypes.py
2 files changed, 48 insertions(+), 27 deletions(-)

diff --git a/pywikibot/__init__.py b/pywikibot/__init__.py
index 9e92d0c..3d96af8 100644
--- a/pywikibot/__init__.py
+++ b/pywikibot/__init__.py
@@ -15,7 +15,7 @@
from contextlib import suppress
from decimal import Decimal
from queue import Queue
-from typing import Optional, Union
+from typing import Any, Optional, Union
from urllib.parse import urlparse
from warnings import warn

@@ -33,7 +33,7 @@
__version__,
)
from pywikibot._wbtypes import WbRepresentation as _WbRepresentation
-from pywikibot.backports import cache, removesuffix, List
+from pywikibot.backports import cache, removesuffix, Dict, List
from pywikibot.bot import (
Bot,
CurrentPageBot,
@@ -288,14 +288,17 @@
}

@classmethod
- def fromWikibase(cls, data: dict, site: DataSite):
+ def fromWikibase(cls, data: Dict[str, Any],
+ site: Optional[DataSite] = None) -> 'Coordinate':
"""
Constructor to create an object from Wikibase's JSON output.

:param data: Wikibase JSON
:param site: The Wikibase site
- :rtype: Coordinate
"""
+ if site is None:
+ site = Site().data_repository()
+
globe = None

if data['globe']:
@@ -634,17 +637,18 @@
return json

@classmethod
- def fromWikibase(cls, wb: dict, site: Optional[DataSite] = None):
+ @_deprecate_arg('wb', 'data')
+ def fromWikibase(cls, data: Dict[str, Any],
+ site: Optional[DataSite] = None) -> 'WbTime':
"""
Create a WbTime from the JSON data given by the Wikibase API.

- :param wb: Wikibase JSON
+ :param data: Wikibase JSON
:param site: The Wikibase site
- :rtype: pywikibot.WbTime
"""
- return cls.fromTimestr(wb['time'], wb['precision'],
- wb['before'], wb['after'],
- wb['timezone'], wb['calendarmodel'], site)
+ return cls.fromTimestr(data['time'], data['precision'],
+ data['before'], data['after'],
+ data['timezone'], data['calendarmodel'], site)


class WbQuantity(_WbRepresentation):
@@ -783,25 +787,26 @@
return json

@classmethod
- def fromWikibase(cls, wb: dict, site: Optional[DataSite] = None):
+ @_deprecate_arg('wb', 'data')
+ def fromWikibase(cls, data: Dict[str, Any],
+ site: Optional[DataSite] = None) -> 'WbQuantity':
"""
Create a WbQuantity from the JSON data given by the Wikibase API.

- :param wb: Wikibase JSON
+ :param data: Wikibase JSON
:param site: The Wikibase site
- :rtype: pywikibot.WbQuantity
"""
- amount = cls._todecimal(wb['amount'])
- upperBound = cls._todecimal(wb.get('upperBound'))
- lowerBound = cls._todecimal(wb.get('lowerBound'))
+ amount = cls._todecimal(data['amount'])
+ upperBound = cls._todecimal(data.get('upperBound'))
+ lowerBound = cls._todecimal(data.get('lowerBound'))
bounds_provided = (upperBound is not None and lowerBound is not None)
error = None
if bounds_provided or cls._require_errors(site):
error = (upperBound - amount, amount - lowerBound)
- if wb['unit'] == '1':
+ if data['unit'] == '1':
unit = None
else:
- unit = wb['unit']
+ unit = data['unit']
return cls(amount, unit, error, site)


@@ -834,14 +839,16 @@
return json

@classmethod
- def fromWikibase(cls, wb: dict):
+ @_deprecate_arg('wb', 'data')
+ def fromWikibase(cls, data: Dict[str, Any],
+ site: Optional[DataSite] = None) -> 'WbMonolingualText':
"""
Create a WbMonolingualText from the JSON data given by Wikibase API.

- :param wb: Wikibase JSON
- :rtype: pywikibot.WbMonolingualText
+ :param data: Wikibase JSON
+ :param site: The Wikibase site
"""
- return cls(wb['text'], wb['language'])
+ return cls(data['text'], data['language'])


class _WbDataPage(_WbRepresentation):
@@ -961,6 +968,10 @@
:param site: The Wikibase site
:rtype: pywikibot._WbDataPage
"""
+ # TODO: This method signature does not match our parent class (which
+ # takes a dictionary argument rather than a string). We should either
+ # change this method's signature or rename this method.
+
data_site = cls._get_data_site(site)
page = Page(data_site, page_name)
return cls(page, site)
@@ -1051,14 +1062,16 @@
return self.json

@classmethod
- def fromWikibase(cls, json: dict):
+ @_deprecate_arg('json', 'data')
+ def fromWikibase(cls, data: Dict[str, Any],
+ site: Optional[DataSite] = None) -> 'WbUnknown':
"""
Create a WbUnknown from the JSON data given by the Wikibase API.

- :param json: Wikibase JSON
- :rtype: pywikibot.WbUnknown
+ :param data: Wikibase JSON
+ :param site: The Wikibase site
"""
- return cls(json)
+ return cls(data)


_sites = {}
diff --git a/pywikibot/_wbtypes.py b/pywikibot/_wbtypes.py
index 8806a65..641484f 100644
--- a/pywikibot/_wbtypes.py
+++ b/pywikibot/_wbtypes.py
@@ -6,6 +6,13 @@
#
import abc
import json
+from typing import Any
+
+from pywikibot.backports import Dict
+
+# TODO: replace these after T286867
+
+OPT_SITE = Any # Optional['pywikibot.site.DataSite']


class WbRepresentation(abc.ABC):
@@ -24,7 +31,8 @@

@classmethod
@abc.abstractmethod
- def fromWikibase(cls, json):
+ def fromWikibase(cls, data: Dict[str, Any], site: OPT_SITE = None
+ ) -> 'WbRepresentation':
"""Create a representation object based on JSON from Wikibase API."""
raise NotImplementedError


To view, visit change 710380. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: Ide15a7a0a234010bd6c5989a2601ea62eae514fb
Gerrit-Change-Number: 710380
Gerrit-PatchSet: 5
Gerrit-Owner: Damian <atagar1@gmail.com>
Gerrit-Reviewer: Matěj Suchánek <matejsuchanek97@gmail.com>
Gerrit-Reviewer: Xqt <info@gno.de>
Gerrit-Reviewer: jenkins-bot
Gerrit-MessageType: merged