Skip to content

Commit 89b1577

Browse files
Use f-strings.
1 parent bfe98aa commit 89b1577

File tree

1 file changed

+25
-37
lines changed

1 file changed

+25
-37
lines changed

Lib/unittest/case.py

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,9 +1322,9 @@ def assertIsInstance(self, obj, cls, msg=None):
13221322
default message."""
13231323
if not isinstance(obj, cls):
13241324
if isinstance(cls, tuple):
1325-
standardMsg = '%s is not an instance of any of %r' % (safe_repr(obj), cls)
1325+
standardMsg = f'{safe_repr(obj)} is not an instance of any of {cls!r}'
13261326
else:
1327-
standardMsg = '%s is not an instance of %r' % (safe_repr(obj), cls)
1327+
standardMsg = f'{safe_repr(obj)} is not an instance of {cls!r}'
13281328
self.fail(self._formatMessage(msg, standardMsg))
13291329

13301330
def assertNotIsInstance(self, obj, cls, msg=None):
@@ -1335,7 +1335,7 @@ def assertNotIsInstance(self, obj, cls, msg=None):
13351335
if isinstance(obj, x):
13361336
cls = x
13371337
break
1338-
standardMsg = '%s is an instance of %r' % (safe_repr(obj), cls)
1338+
standardMsg = f'{safe_repr(obj)} is an instance of {cls!r}'
13391339
self.fail(self._formatMessage(msg, standardMsg))
13401340

13411341
def assertIsSubclass(self, cls, superclass, msg=None):
@@ -1344,13 +1344,12 @@ def assertIsSubclass(self, cls, superclass, msg=None):
13441344
return
13451345
except TypeError:
13461346
if not isinstance(cls, type):
1347-
self.fail(self._formatMessage(msg,
1348-
'%r is not a class' % (cls,)))
1347+
self.fail(self._formatMessage(msg, f'{cls!r} is not a class'))
13491348
raise
13501349
if isinstance(superclass, tuple):
1351-
standardMsg = '%r is not a subclass of any of %r' % (cls, superclass)
1350+
standardMsg = f'{cls!r} is not a subclass of any of {superclass!r}'
13521351
else:
1353-
standardMsg = '%r is not a subclass of %r' % (cls, superclass)
1352+
standardMsg = f'{cls!r} is not a subclass of {superclass!r}'
13541353
self.fail(self._formatMessage(msg, standardMsg))
13551354

13561355
def assertNotIsSubclass(self, cls, superclass, msg=None):
@@ -1359,38 +1358,31 @@ def assertNotIsSubclass(self, cls, superclass, msg=None):
13591358
return
13601359
except TypeError:
13611360
if not isinstance(cls, type):
1362-
self.fail(self._formatMessage(msg,
1363-
'%r is not a class' % (cls,)))
1361+
self.fail(self._formatMessage(msg, f'{cls!r} is not a class'))
13641362
raise
13651363
if isinstance(superclass, tuple):
13661364
for x in superclass:
13671365
if issubclass(cls, x):
13681366
superclass = x
13691367
break
1370-
self.fail(self._formatMessage(msg,
1371-
'%r is a subclass of %r' % (cls, superclass)))
1368+
standardMsg = f'{cls!r} is a subclass of {superclass!r}'
1369+
self.fail(self._formatMessage(msg, standardMsg))
13721370

13731371
def assertHasAttr(self, obj, name, msg=None):
13741372
if not hasattr(obj, name):
13751373
if isinstance(obj, types.ModuleType):
1376-
self.fail(self._formatMessage(msg,
1377-
'module %r has no attribute %r' %
1378-
(obj.__name__, name)))
1374+
standardMsg = f'module {obj.__name__!r} has no attribute {name!r}'
13791375
else:
1380-
self.fail(self._formatMessage(msg,
1381-
'%s instance has no attribute %r' %
1382-
(type(obj).__name__, name)))
1376+
standardMsg = f'{type(obj).__name__} instance has no attribute {name!r}'
1377+
self.fail(self._formatMessage(msg, standardMsg))
13831378

13841379
def assertNotHasAttr(self, obj, name, msg=None):
13851380
if hasattr(obj, name):
13861381
if isinstance(obj, types.ModuleType):
1387-
self.fail(self._formatMessage(msg,
1388-
'module %r has unexpected attribute %r' %
1389-
(obj.__name__, name)))
1382+
standardMsg = f'module {obj.__name__!r} has unexpected attribute {name!r}'
13901383
else:
1391-
self.fail(self._formatMessage(msg,
1392-
'%s instance has unexpected attribute %r' %
1393-
(type(obj).__name__, name)))
1384+
standardMsg = f'{type(obj).__name__} instance has unexpected attribute {name!r}'
1385+
self.fail(self._formatMessage(msg, standardMsg))
13941386

13951387
def assertRaisesRegex(self, expected_exception, expected_regex,
13961388
*args, **kwargs):
@@ -1460,11 +1452,11 @@ def _tail_type_check(self, s, tails, msg):
14601452
if isinstance(tail, str):
14611453
if not isinstance(s, str):
14621454
self.fail(self._formatMessage(msg,
1463-
'Expected str, not %s' % (type(s).__name__,)))
1455+
f'Expected str, not {type(s).__name__}'))
14641456
elif isinstance(tail, (bytes, bytearray)):
14651457
if not isinstance(s, (bytes, bytearray)):
14661458
self.fail(self._formatMessage(msg,
1467-
'Expected bytes, not %s' % (type(s).__name__,)))
1459+
f'Expected bytes, not {type(s).__name__}'))
14681460

14691461
def assertStartsWith(self, s, prefix, msg=None):
14701462
try:
@@ -1476,11 +1468,10 @@ def assertStartsWith(self, s, prefix, msg=None):
14761468
a = safe_repr(s, short=True)
14771469
b = safe_repr(prefix)
14781470
if isinstance(prefix, tuple):
1479-
self.fail(self._formatMessage(msg,
1480-
"%s doesn't start with any of %s" % (a, b)))
1471+
standardMsg = f"{a} doesn't start with any of {b}"
14811472
else:
1482-
self.fail(self._formatMessage(msg,
1483-
"%s doesn't start with %s" % (a, b)))
1473+
standardMsg = f"{a} doesn't start with {b}"
1474+
self.fail(self._formatMessage(msg, standardMsg))
14841475

14851476
def assertNotStartsWith(self, s, prefix, msg=None):
14861477
try:
@@ -1496,8 +1487,7 @@ def assertNotStartsWith(self, s, prefix, msg=None):
14961487
break
14971488
a = safe_repr(s, short=True)
14981489
b = safe_repr(prefix)
1499-
self.fail(self._formatMessage(msg,
1500-
"%s starts with %s" % (a, b)))
1490+
self.fail(self._formatMessage(msg, f"{a} starts with {b}"))
15011491

15021492
def assertEndsWith(self, s, suffix, msg=None):
15031493
try:
@@ -1509,11 +1499,10 @@ def assertEndsWith(self, s, suffix, msg=None):
15091499
a = safe_repr(s, short=True)
15101500
b = safe_repr(suffix)
15111501
if isinstance(suffix, tuple):
1512-
self.fail(self._formatMessage(msg,
1513-
"%s doesn't end with any of %s" % (a, b)))
1502+
standardMsg = f"{a} doesn't end with any of {b}"
15141503
else:
1515-
self.fail(self._formatMessage(msg,
1516-
"%s doesn't end with %s" % (a, b)))
1504+
standardMsg = f"{a} doesn't end with {b}"
1505+
self.fail(self._formatMessage(msg, standardMsg))
15171506

15181507
def assertNotEndsWith(self, s, suffix, msg=None):
15191508
try:
@@ -1529,8 +1518,7 @@ def assertNotEndsWith(self, s, suffix, msg=None):
15291518
break
15301519
a = safe_repr(s, short=True)
15311520
b = safe_repr(suffix)
1532-
self.fail(self._formatMessage(msg,
1533-
"%s ends with %s" % (a, b)))
1521+
self.fail(self._formatMessage(msg, f"{a} ends with {b}"))
15341522

15351523

15361524
class FunctionTestCase(TestCase):

0 commit comments

Comments
 (0)