Skip to content

Commit e0d56b7

Browse files
committed
testcase: Deprecate legacy aliases
These were removed in Python 3.12 released in October 2023. We should follow their lead in an attempt to keep in sync. The implementation is based on that used in Python (before 3.12) [1]. [1] https://github.com/python/cpython/blob/3.11/Lib/unittest/case.py#L1366-L1384 Signed-off-by: Stephen Finucane <[email protected]>
1 parent 59b890d commit e0d56b7

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

testtools/testcase.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,24 @@ def __eq__(self, other):
279279
return False
280280
return self.__dict__ == getattr(other, '__dict__', None)
281281

282+
# We need to explicitly set this since we're overriding __eq__
283+
# https://docs.python.org/3/reference/datamodel.html#object.__hash__
282284
__hash__ = unittest.TestCase.__hash__
283285

284286
def __repr__(self):
285287
# We add id to the repr because it makes testing testtools easier.
286288
return f"<{self.id()} id=0x{id(self):0x}>"
287289

290+
def _deprecate(original_func):
291+
def deprecated_func(*args, **kwargs):
292+
warnings.warn(
293+
'Please use {0} instead.'.format(original_func.__name__),
294+
DeprecationWarning,
295+
stacklevel=2,
296+
)
297+
return original_func(*args, **kwargs)
298+
return deprecated_func
299+
288300
def addDetail(self, name, content_object):
289301
"""Add a detail to be reported with this test's outcome.
290302
@@ -397,7 +409,7 @@ def assertEqual(self, expected, observed, message=''):
397409
matcher = _FlippedEquals(expected)
398410
self.assertThat(observed, matcher, message)
399411

400-
failUnlessEqual = assertEquals = assertEqual
412+
failUnlessEqual = assertEquals = _deprecate(assertEqual)
401413

402414
def assertIn(self, needle, haystack, message=''):
403415
"""Assert that needle is in haystack."""
@@ -471,7 +483,8 @@ def match(self, matchee):
471483
our_callable = Nullary(callableObj, *args, **kwargs)
472484
self.assertThat(our_callable, matcher)
473485
return capture.matchee
474-
failUnlessRaises = assertRaises
486+
487+
failUnlessRaises = _deprecate(assertRaises)
475488

476489
def assertThat(self, matchee, matcher, message='', verbose=False):
477490
"""Assert that matchee is matched by matcher.
@@ -484,7 +497,8 @@ def assertThat(self, matchee, matcher, message='', verbose=False):
484497
if mismatch_error is not None:
485498
raise mismatch_error
486499

487-
assertItemsEqual = unittest.TestCase.assertCountEqual
500+
assertItemsEqual = _deprecate(unittest.TestCase.assertCountEqual)
501+
488502
def addDetailUniqueName(self, name, content_object):
489503
"""Add a detail to the test, but ensure it's name is unique.
490504

0 commit comments

Comments
 (0)