Xqt has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1234434?usp=email )
Change subject: Revert "IMPR: Use singledispatchmethod with SiteLinkCollection.normalizeData" ......................................................................
Revert "IMPR: Use singledispatchmethod with SiteLinkCollection.normalizeData"
This reverts commit f74c5ff04b71716658d34a0708c4b1c7756bce86.
Reason for revert: There is a bug upstream in Python 3.9: https://github.com/python/cpython/issues/83860
This was fixed in 3.10 and probably in 3.9.8+
Change-Id: I289faf6818d5c5bb525d5a1f448eb7d942ff0e08 --- M pywikibot/page/_collections.py 1 file changed, 21 insertions(+), 36 deletions(-)
Approvals: Xqt: Verified; Looks good to me, approved
diff --git a/pywikibot/page/_collections.py b/pywikibot/page/_collections.py index a4ea78e..b95b059 100644 --- a/pywikibot/page/_collections.py +++ b/pywikibot/page/_collections.py @@ -9,7 +9,6 @@ import reprlib from collections import defaultdict from collections.abc import MutableMapping, MutableSequence -from functools import singledispatchmethod from typing import Any
import pywikibot @@ -417,48 +416,34 @@ return {'site': db_name, 'title': obj.title()} return obj
- @singledispatchmethod @classmethod - def normalizeData(cls, data) -> dict: + def normalizeData(cls, data: list | dict[str, Any]) -> dict: """Helper function to expand data into the Wikibase API structure.
:param data: Data to normalize - :type data: dict | list :return: The dict with normalized data - :raises ValueError: Couldn't determine the site and title or the - key doesn't match the site within *data* collection. - :raises TypeError: Unsupported type for *data* """ - raise TypeError(f'Unsupported type: {type(data)}') - - @normalizeData.register - @classmethod - def _(cls, data: dict) -> dict: norm_data = {} - for key, obj in data.items(): - key = cls.getdbName(key) - json = cls._extract_json(obj) - if isinstance(json, str): - json = {'site': key, 'title': json} - elif key != json['site']: - raise ValueError( - "Key '{}' doesn't match the site of the value: '{}'" - .format(key, json['site'])) - norm_data[key] = json - return norm_data - - @normalizeData.register - @classmethod - def _(cls, data: list) -> dict: - norm_data = {} - for obj in data: - json = cls._extract_json(obj) - if not isinstance(json, dict): - raise ValueError( - "Couldn't determine the site and title of the value: " - f'{json!r}') - db_name = json['site'] - norm_data[db_name] = json + if isinstance(data, dict): + for key, obj in data.items(): + key = cls.getdbName(key) + json = cls._extract_json(obj) + if isinstance(json, str): + json = {'site': key, 'title': json} + elif key != json['site']: + raise ValueError( + "Key '{}' doesn't match the site of the value: '{}'" + .format(key, json['site'])) + norm_data[key] = json + else: + for obj in data: + json = cls._extract_json(obj) + if not isinstance(json, dict): + raise ValueError( + "Couldn't determine the site and title of the value: " + f'{json!r}') + db_name = json['site'] + norm_data[db_name] = json return norm_data
def toJSON(self, diffto: dict | None = None) -> dict:
pywikibot-commits@lists.wikimedia.org