Xqt has submitted this change. (
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1226241?usp=email )
Change subject: IMPR: Use singledispatchmethod with
SiteLinkCollection.normalizeData
......................................................................
IMPR: Use singledispatchmethod with SiteLinkCollection.normalizeData
Change-Id: I5dd357ed4545a4403a9cbe92c8c4e2ff1e42c347
---
M pywikibot/page/_collections.py
1 file changed, 36 insertions(+), 21 deletions(-)
Approvals:
Xqt: Verified; Looks good to me, approved
diff --git a/pywikibot/page/_collections.py b/pywikibot/page/_collections.py
index b95b059..a4ea78e 100644
--- a/pywikibot/page/_collections.py
+++ b/pywikibot/page/_collections.py
@@ -9,6 +9,7 @@
import reprlib
from collections import defaultdict
from collections.abc import MutableMapping, MutableSequence
+from functools import singledispatchmethod
from typing import Any
import pywikibot
@@ -416,34 +417,48 @@
return {'site': db_name, 'title': obj.title()}
return obj
+ @singledispatchmethod
@classmethod
- def normalizeData(cls, data: list | dict[str, Any]) -> dict:
+ def normalizeData(cls, data) -> 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 = {}
- 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
+ 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
return norm_data
def toJSON(self, diffto: dict | None = None) -> dict:
--
To view, visit
https://gerrit.wikimedia.org/r/c/pywikibot/core/+/1226241?usp=email
To unsubscribe, or for help writing mail filters, visit
https://gerrit.wikimedia.org/r/settings?usp=email
Gerrit-MessageType: merged
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Change-Id: I5dd357ed4545a4403a9cbe92c8c4e2ff1e42c347
Gerrit-Change-Number: 1226241
Gerrit-PatchSet: 3
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
Pywikibot-commits mailing list -- [email protected]
To unsubscribe send an email to [email protected]