jenkins-bot has submitted this change. ( https://gerrit.wikimedia.org/r/c/pywikibot/core/+/767481 )
Change subject: [IMPR] Always check for real Siteinfo contents with __contains__ ......................................................................
[IMPR] Always check for real Siteinfo contents with __contains__
- Return whether the value is in Siteinfo container and load the content if necessary - For the old behaviour, Siteindo.is_cached() can be used - add Siteinfo.clear() method to clear the cache - Throttle.log gets a float since release 3.0.20200306 - update tests accordingly
Bug: T302859 Change-Id: I48c83d1becdf665a8df4de33c2ecd595fd7c57fc --- M pywikibot/site/_siteinfo.py M tests/api_tests.py M tests/siteinfo_tests.py 3 files changed, 42 insertions(+), 7 deletions(-)
Approvals: Mpaa: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/site/_siteinfo.py b/pywikibot/site/_siteinfo.py index 90bb9e9..e7c3bc5 100644 --- a/pywikibot/site/_siteinfo.py +++ b/pywikibot/site/_siteinfo.py @@ -63,6 +63,13 @@ self._site = site self._cache = {}
+ def clear(self) -> None: + """Remove all items from Siteinfo. + + .. versionadded: 7.1 + """ + self._cache.clear() + @staticmethod def _get_default(key: str): """ @@ -316,8 +323,11 @@ return self._cache[key] raise KeyError(key)
- def __contains__(self, key: str) -> bool: - """Return whether the value is cached.""" + def is_cached(self, key: str) -> bool: + """Return whether the value is cached. + + .. versionadded:: 7.1 + """ try: self._get_cached(key) except KeyError: @@ -325,6 +335,19 @@ else: return True
+ def __contains__(self, key: str) -> bool: + """Return whether the value is in Siteinfo container. + + .. versionchanged:: 7.1 + Previous implementation only checked for cached keys. + """ + try: + self[key] + except KeyError: + return False + else: + return True + def is_recognised(self, key: str) -> Optional[bool]: """Return if 'key' is a valid property name. 'None' if not cached.""" time = self.get_requested_time(key) diff --git a/tests/api_tests.py b/tests/api_tests.py index 6d6447d..0a6548d 100644 --- a/tests/api_tests.py +++ b/tests/api_tests.py @@ -1007,8 +1007,8 @@ pywikibot.warning( 'Wrong api.lagpattern regex, cannot retrieve lag value') raise e - self.assertIsInstance(mythrottle._lagvalue, int) - self.assertGreaterEqual(mythrottle._lagvalue, 0) + self.assertIsInstance(mythrottle._lagvalue, float) + self.assertGreaterEqual(mythrottle._lagvalue, 0.0) self.assertIsInstance(mythrottle.retry_after, int) self.assertGreaterEqual(mythrottle.retry_after, 0)
diff --git a/tests/siteinfo_tests.py b/tests/siteinfo_tests.py index 7c7c36a..0a3c189 100644 --- a/tests/siteinfo_tests.py +++ b/tests/siteinfo_tests.py @@ -23,6 +23,11 @@
cached = True
+ def setUp(self): + """Clear siteinfo cache.""" + super().setUp() + self.site.siteinfo.clear() + def test_siteinfo(self): """Test the siteinfo property.""" # general enteries @@ -69,7 +74,7 @@ if 'fileextensions' in self.site.siteinfo._cache: del self.site.siteinfo._cache['fileextensions'] self.site.siteinfo.get('fileextensions', cache=False) - self.assertNotIn('fileextensions', self.site.siteinfo) + self.assertFalse(self.site.siteinfo.is_cached('fileextensions'))
def test_not_exists(self): """Test accessing a property not in siteinfo.""" @@ -86,6 +91,13 @@ entered_loop(mysite.siteinfo.get(not_exists).values())) self.assertFalse(entered_loop(mysite.siteinfo.get(not_exists).keys()))
+ def test_container(self): + """Test Siteinfo container methods.""" + self.assertFalse(self.site.siteinfo.is_cached('general')) + self.assertIn('general', self.site.siteinfo) + self.assertTrue(self.site.siteinfo.is_cached('general')) + self.assertNotIn('### key not in siteinfo ###', self.site.siteinfo) +
class TestSiteinfoDry(DefaultDrySiteTestCase):
@@ -110,10 +122,10 @@ def test_async_request(self): """Test async request.""" self.assertTrue(page_put_queue.empty()) - self.assertNotIn('statistics', self.site.siteinfo) + self.assertFalse(self.site.siteinfo.is_cached('statistics')) async_request(self.site.siteinfo.get, 'statistics') page_put_queue.join() - self.assertIn('statistics', self.site.siteinfo) + self.assertTrue(self.site.siteinfo.is_cached('statistics'))
if __name__ == '__main__': # pragma: no cover
pywikibot-commits@lists.wikimedia.org