Skip to content

Commit

Permalink
Merge "[IMPR] Create a SiteLink with __getitem__ method"
Browse files Browse the repository at this point in the history
  • Loading branch information
jenkins-bot authored and Gerrit Code Review committed Feb 3, 2021
2 parents d7460ad + 51b119a commit 7db23c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
30 changes: 20 additions & 10 deletions pywikibot/page/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3528,23 +3528,33 @@ def __getitem__(self, key):
@rtype: pywikibot.page.SiteLink
"""
key = self.getdbName(key)
return self._data[key]
val = self._data[key]
if isinstance(val, str):
val = SiteLink(val, key)
elif isinstance(val, dict):
val = SiteLink.fromJSON(val, self.repo)
else:
return val
self._data[key] = val
return val

def __setitem__(self, key, val):
"""
Set the SiteLink for a given key.
This only sets the value given as str, dict or SiteLink. If a
str or dict is given the SiteLink object is created later in
__getitem__ method.
@param key: site key as Site instance or db key
@type key: pywikibot.Site or str
@param val: page name as a string or JSON containing SiteLink data
@type val: dict or str
@rtype: pywikibot.page.SiteLink
@param val: page name as a string or JSON containing SiteLink
data or a SiteLink object
@type val: Union[str, dict, SiteLink]
"""
if isinstance(val, str):
val = SiteLink(val, key)
else:
val = SiteLink.fromJSON(val, self.repo)
key = self.getdbName(key)
if isinstance(val, SiteLink):
assert val.site.dbName() == key
self._data[key] = val

def __delitem__(self, key):
Expand Down Expand Up @@ -3851,8 +3861,8 @@ def get(self, force: bool = False) -> dict:
self.latest_revision_id = self._content.get('lastrevid')

data = {}
# todo: this initializes all data,
# make use of lazy initialization (T245809)

# This initializes all data,
for key, cls in self.DATA_ATTRIBUTES.items():
value = cls.fromJSON(self._content.get(key, {}), self.repo)
setattr(self, key, value)
Expand Down
2 changes: 0 additions & 2 deletions tests/wikibase_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2352,8 +2352,6 @@ class TestJSON(WikidataTestCase):

"""Test cases to test toJSON() functions."""

dry = True

def setUp(self):
"""Setup test."""
super().setUp()
Expand Down

0 comments on commit 7db23c2

Please sign in to comment.