jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/384328 )
Change subject: [IMPR] Introduce suppress_warnings context manager
......................................................................
[IMPR] Introduce suppress_warnings context manager
suppress_warnings context manager temporarily suppresses warnings.
It is derived from warnings.catch_warnings.
It could be used by unittests to hide some deprecation warnings when
deprecated methods are tested explicitly.
Change-Id: Id22d7f296c0159c081a36f851b6372f711d92e11
---
M pywikibot/tools/__init__.py
M tests/user_tests.py
2 files changed, 20 insertions(+), 5 deletions(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
Zoranzoki21: Looks good to me, but someone else must approve
diff --git a/pywikibot/tools/__init__.py b/pywikibot/tools/__init__.py
index 7e1c9c1..8f94f77 100644
--- a/pywikibot/tools/__init__.py
+++ b/pywikibot/tools/__init__.py
@@ -23,7 +23,7 @@
import types
from distutils.version import Version
-from warnings import warn
+from warnings import catch_warnings, simplefilter, warn
PYTHON_VERSION = sys.version_info[:3]
PY2 = (PYTHON_VERSION[0] == 2)
@@ -207,6 +207,16 @@
return self.method(owner)
+class suppress_warnings(catch_warnings): # flake8: disable=N801
+
+ """A context manager that temporarily suppresses
warnings."""
+
+ def __enter__(self):
+ """Catch the old filter settings and ignore all
warnings."""
+ super(suppress_warnings, self).__enter__()
+ simplefilter('ignore')
+
+
class UnicodeMixin(object):
"""Mixin class to add __str__ method in Python 2 or
3."""
diff --git a/tests/user_tests.py b/tests/user_tests.py
index 96b99d9..149cb41 100644
--- a/tests/user_tests.py
+++ b/tests/user_tests.py
@@ -9,6 +9,7 @@
import pywikibot
+from pywikibot.tools import suppress_warnings
from pywikibot import User
from tests.aspects import TestCase, unittest
@@ -24,7 +25,8 @@
def test_registered_user(self):
"""Test registered user."""
user = User(self.site, 'Xqt')
- self.assertEqual(user.name(), user.username)
+ with suppress_warnings():
+ self.assertEqual(user.name(), user.username)
self.assertEqual(user.title(withNamespace=False), user.username)
self.assertTrue(user.isRegistered())
self.assertFalse(user.isAnonymous())
@@ -58,7 +60,8 @@
def test_anonymous_user(self):
"""Test registered user."""
user = User(self.site, '123.45.67.89')
- self.assertEqual(user.name(), user.username)
+ with suppress_warnings():
+ self.assertEqual(user.name(), user.username)
self.assertEqual(user.title(withNamespace=False), user.username)
self.assertFalse(user.isRegistered())
self.assertTrue(user.isAnonymous())
@@ -70,7 +73,8 @@
def test_unregistered_user(self):
"""Test unregistered user."""
user = User(self.site, 'This user name is not registered yet')
- self.assertEqual(user.name(), user.username)
+ with suppress_warnings():
+ self.assertEqual(user.name(), user.username)
self.assertEqual(user.title(withNamespace=False), user.username)
self.assertFalse(user.isRegistered())
self.assertFalse(user.isAnonymous())
@@ -82,7 +86,8 @@
def test_invalid_user(self):
"""Test invalid user."""
user = User(self.site, 'Invalid char\x9f in Name')
- self.assertEqual(user.name(), user.username)
+ with suppress_warnings():
+ self.assertEqual(user.name(), user.username)
self.assertEqual(user.title(withNamespace=False), user.username)
self.assertFalse(user.isRegistered())
self.assertFalse(user.isAnonymous())
--
To view, visit
https://gerrit.wikimedia.org/r/384328
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Id22d7f296c0159c081a36f851b6372f711d92e11
Gerrit-PatchSet: 3
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <info(a)gno.de>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Magul <tomasz.magulski(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: Zoranzoki21 <zorandori4444(a)gmail.com>
Gerrit-Reviewer: jenkins-bot <>