Skip to content

Commit 6e3d282

Browse files
committed
better doc example and small test changes
1 parent 0a8640e commit 6e3d282

File tree

2 files changed

+27
-31
lines changed

2 files changed

+27
-31
lines changed

Doc/library/functools.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -380,14 +380,15 @@ The :mod:`functools` module defines the following functions:
380380

381381
>>> from functools import partial, Placeholder as _
382382
>>> remove = partial(str.replace, _, _, '')
383-
>>> remove('Hello, world', 'l')
384-
'Heo, word'
385-
>>> remove_l = partial(remove, _, 'l')
386-
>>> remove_l('Hello, world')
387-
'Heo, word'
388-
>>> remove_first_l = partial(remove_l, _, 1)
389-
>>> remove_first_l('Hello, world')
390-
'Helo, world'
383+
>>> message = 'Hello, dear dear world!'
384+
>>> remove(message, ' dear')
385+
'Hello, world!'
386+
>>> remove_dear = partial(remove, _, ' dear')
387+
>>> remove_dear(message)
388+
'Hello, world!'
389+
>>> remove_first_dear = partial(remove_dear, _, 1)
390+
>>> remove_first_dear(message)
391+
'Hello, dear world!'
391392

392393
Note, :data:`!Placeholder` has no special treatment when used for keyword
393394
argument of :data:`!Placeholder`.

Lib/test/test_functools.py

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -221,35 +221,32 @@ def test_placeholders(self):
221221
# 1 Placeholder
222222
args = (PH, 0)
223223
p = self.partial(capture, *args)
224-
got, empty = p('x')
225-
self.assertEqual(('x', 0), got)
226-
self.assertEqual(empty, {})
224+
actual_args, actual_kwds = p('x')
225+
self.assertEqual(actual_args, ('x', 0))
226+
self.assertEqual(actual_kwds, {})
227227
# 2 Placeholders
228228
args = (PH, 0, PH, 1)
229229
p = self.partial(capture, *args)
230230
with self.assertRaises(TypeError):
231231
p('x')
232-
got, empty = p('x', 'y')
233-
expected = ('x', 0, 'y', 1)
234-
self.assertEqual(expected, got)
235-
self.assertEqual(empty, {})
232+
actual_args, actual_kwds = p('x', 'y')
233+
self.assertEqual(actual_args, ('x', 0, 'y', 1))
234+
self.assertEqual(actual_kwds, {})
236235

237236
def test_placeholders_optimization(self):
238237
PH = self.module.Placeholder
239238
p = self.partial(capture, PH, 0)
240239
p2 = self.partial(p, PH, 1, 2, 3)
241-
expected = (PH, 0, 1, 2, 3)
242-
self.assertEqual(expected, p2.args)
240+
self.assertEqual(p2.args, (PH, 0, 1, 2, 3))
243241
p3 = self.partial(p2, -1, 4)
244-
got, empty = p3(5)
245-
expected = (-1, 0, 1, 2, 3, 4, 5)
246-
self.assertTrue(expected == got and empty == {})
242+
actual_args, actual_kwds = p3(5)
243+
self.assertEqual(actual_args, (-1, 0, 1, 2, 3, 4, 5))
244+
self.assertEqual(actual_kwds, {})
247245
# inner partial has placeholders and outer partial has no args case
248246
p = self.partial(capture, PH, 0)
249247
p2 = self.partial(p)
250-
expected = (PH, 0)
251-
self.assertEqual(expected, p2.args)
252-
self.assertEqual(((1, 0), {}), p2(1))
248+
self.assertEqual(p2.args, (PH, 0))
249+
self.assertEqual(p2(1), ((1, 0), {}))
253250

254251
def test_construct_placeholder_singleton(self):
255252
PH = self.module.Placeholder
@@ -364,19 +361,17 @@ def test_setstate(self):
364361
f = self.partial(signature)
365362
f.__setstate__((capture, (PH, 1), dict(a=10), dict(attr=[])))
366363
self.assertEqual(signature(f), (capture, (PH, 1), dict(a=10), dict(attr=[])))
367-
with self.assertRaises(TypeError) as cm:
368-
self.assertEqual(f(), (PH, 1), dict(a=10))
369-
expected = ("missing positional arguments in 'partial' call; "
370-
"expected at least 1, got 0")
371-
self.assertEqual(cm.exception.args[0], expected)
364+
msg_regex = ("^missing positional arguments in 'partial' call; "
365+
"expected at least 1, got 0$")
366+
with self.assertRaisesRegex(TypeError, msg_regex) as cm:
367+
f()
372368
self.assertEqual(f(2), ((2, 1), dict(a=10)))
373369

374370
# Trailing Placeholder error
375371
f = self.partial(signature)
376-
with self.assertRaises(TypeError) as cm:
372+
msg_regex = "^trailing Placeholders are not allowed$"
373+
with self.assertRaisesRegex(TypeError, msg_regex) as cm:
377374
f.__setstate__((capture, (1, PH), dict(a=10), dict(attr=[])))
378-
expected = "trailing Placeholders are not allowed"
379-
self.assertEqual(cm.exception.args[0], expected)
380375

381376
def test_setstate_errors(self):
382377
f = self.partial(signature)

0 commit comments

Comments
 (0)