Skip to content

bpo-34318: Convert deprecation warnings to errors in assertRaises() etc. #8623

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions Lib/unittest/case.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,16 +157,11 @@ def handle(self, name, args, kwargs):
if not _is_subtype(self.expected, self._base_type):
raise TypeError('%s() arg 1 must be %s' %
(name, self._base_type_str))
if args and args[0] is None:
warnings.warn("callable is None",
DeprecationWarning, 3)
args = ()
if not args:
self.msg = kwargs.pop('msg', None)
if kwargs:
warnings.warn('%r is an invalid keyword argument for '
'this function' % next(iter(kwargs)),
DeprecationWarning, 3)
raise TypeError('%r is an invalid keyword argument for '
'this function' % (next(iter(kwargs)),))
return self

callable_obj, *args = args
Expand Down
20 changes: 8 additions & 12 deletions Lib/unittest/test/test_case.py
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ def Stub():
with self.assertRaises(self.failureException):
self.assertRaises(ExceptionMock, lambda: 0)
# Failure when the function is None
with self.assertWarns(DeprecationWarning):
with self.assertRaises(TypeError):
self.assertRaises(ExceptionMock, None)
# Failure when another exception is raised
with self.assertRaises(ExceptionMock):
Expand Down Expand Up @@ -1253,8 +1253,7 @@ def Stub():
with self.assertRaises(ExceptionMock, msg='foobar'):
pass
# Invalid keyword argument
with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
self.assertRaises(AssertionError):
with self.assertRaisesRegex(TypeError, 'foobar'):
with self.assertRaises(ExceptionMock, foobar=42):
pass
# Failure when another exception is raised
Expand Down Expand Up @@ -1295,7 +1294,7 @@ def Stub():

self.assertRaisesRegex(ExceptionMock, re.compile('expect$'), Stub)
self.assertRaisesRegex(ExceptionMock, 'expect$', Stub)
with self.assertWarns(DeprecationWarning):
with self.assertRaises(TypeError):
self.assertRaisesRegex(ExceptionMock, 'expect$', None)

def testAssertNotRaisesRegex(self):
Expand All @@ -1312,8 +1311,7 @@ def testAssertNotRaisesRegex(self):
with self.assertRaisesRegex(Exception, 'expect', msg='foobar'):
pass
# Invalid keyword argument
with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
self.assertRaises(AssertionError):
with self.assertRaisesRegex(TypeError, 'foobar'):
with self.assertRaisesRegex(Exception, 'expect', foobar=42):
pass

Expand Down Expand Up @@ -1388,7 +1386,7 @@ def _runtime_warn():
with self.assertRaises(self.failureException):
self.assertWarns(RuntimeWarning, lambda: 0)
# Failure when the function is None
with self.assertWarns(DeprecationWarning):
with self.assertRaises(TypeError):
self.assertWarns(RuntimeWarning, None)
# Failure when another warning is triggered
with warnings.catch_warnings():
Expand Down Expand Up @@ -1433,8 +1431,7 @@ def _runtime_warn():
with self.assertWarns(RuntimeWarning, msg='foobar'):
pass
# Invalid keyword argument
with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
self.assertRaises(AssertionError):
with self.assertRaisesRegex(TypeError, 'foobar'):
with self.assertWarns(RuntimeWarning, foobar=42):
pass
# Failure when another warning is triggered
Expand Down Expand Up @@ -1475,7 +1472,7 @@ def _runtime_warn(msg):
self.assertWarnsRegex(RuntimeWarning, "o+",
lambda: 0)
# Failure when the function is None
with self.assertWarns(DeprecationWarning):
with self.assertRaises(TypeError):
self.assertWarnsRegex(RuntimeWarning, "o+", None)
# Failure when another warning is triggered
with warnings.catch_warnings():
Expand Down Expand Up @@ -1518,8 +1515,7 @@ def _runtime_warn(msg):
with self.assertWarnsRegex(RuntimeWarning, 'o+', msg='foobar'):
pass
# Invalid keyword argument
with self.assertWarnsRegex(DeprecationWarning, 'foobar'), \
self.assertRaises(AssertionError):
with self.assertRaisesRegex(TypeError, 'foobar'):
with self.assertWarnsRegex(RuntimeWarning, 'o+', foobar=42):
pass
# Failure when another warning is triggered
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
:func:`~unittest.TestCase.assertRaises`,
:func:`~unittest.TestCase.assertRaisesRegex`,
:func:`~unittest.TestCase.assertWarns` and
:func:`~unittest.TestCase.assertWarnsRegex` no longer success if the passed
callable is None. They no longer ignore unknown keyword arguments in the
context manager mode. A DeprecationWarning was raised in these cases
since Python 3.5.