Debian Codegraph @

Lib/test/test_functools.py

raw
1 import abc
2 from annotationlib import Format, get_annotations
3 import builtins
4 import collections
5 import collections.abc
6 import copy
7 from itertools import permutations
8 import pickle
9 from random import choice
10 import re
11 import sys
12 from test import support
13 import threading
14 import time
15 import typing
16 import unittest
17 import unittest.mock
18 import weakref
19 import gc
20 from weakref import proxy
21 import contextlib
22 from inspect import Signature
23
24 from test.support import ALWAYS_EQ
25 from test.support import import_helper
26 from test.support import threading_helper
27 from test.support import cpython_only
28 from test.support import EqualToForwardRef
29
30 import functools
31
32 py_functools = import_helper.import_fresh_module('functools',
33 blocked=['_functools'])
34 c_functools = import_helper.import_fresh_module('functools',
35 fresh=['_functools'])
36
37 decimal = import_helper.import_fresh_module('decimal', fresh=['_decimal'])
38
39
40 @contextlib.contextmanager
41 def replaced_module(name, replacement):
42 original_module = sys.modules[name]
43 sys.modules[name] = replacement
44 try:
45 yield
46 finally:
47 sys.modules[name] = original_module
48
49 def capture(*args, **kw):
50 """capture all positional and keyword arguments"""
51 return args, kw
52
53
54 def signature(part):
55 """ return the signature of a partial object """
56 return (part.func, part.args, part.keywords, part.__dict__)
57
58 class MyTuple(tuple):
59 pass
60
61 class BadTuple(tuple):
62 def __add__(self, other):
63 return list(self) + list(other)
64
65 class MyDict(dict):
66 pass
67
68 class TestImportTime(unittest.TestCase):
69
70 @cpython_only
71 def test_lazy_import(self):
72 import_helper.ensure_lazy_imports(
73 "functools", {"os", "weakref", "typing", "annotationlib", "warnings"}
74 )
75
76
77 class TestPartial:
78
79 def test_basic_examples(self):
80 p = self.partial(capture, 1, 2, a=10, b=20)
81 self.assertTrue(callable(p))
82 self.assertEqual(p(3, 4, b=30, c=40),
83 ((1, 2, 3, 4), dict(a=10, b=30, c=40)))
84 p = self.partial(map, lambda x: x*10)
85 self.assertEqual(list(p([1,2,3,4])), [10, 20, 30, 40])
86
87 def test_attributes(self):
88 p = self.partial(capture, 1, 2, a=10, b=20)
89 # attributes should be readable
90 self.assertEqual(p.func, capture)
91 self.assertEqual(p.args, (1, 2))
92 self.assertEqual(p.keywords, dict(a=10, b=20))
93
94 def test_argument_checking(self):
95 self.assertRaises(TypeError, self.partial) # need at least a func arg
96 try:
97 self.partial(2)()
98 except TypeError:
99 pass
100 else:
101 self.fail('First arg not checked for callability')
102
103 def test_protection_of_callers_dict_argument(self):
104 # a caller's dictionary should not be altered by partial
105 def func(a=10, b=20):
106 return a
107 d = {'a':3}
108 p = self.partial(func, a=5)
109 self.assertEqual(p(**d), 3)
110 self.assertEqual(d, {'a':3})
111 p(b=7)
112 self.assertEqual(d, {'a':3})
113
114 def test_kwargs_copy(self):
115 # Issue #29532: Altering a kwarg dictionary passed to a constructor
116 # should not affect a partial object after creation
117 d = {'a': 3}
118 p = self.partial(capture, **d)
119 self.assertEqual(p(), ((), {'a': 3}))
120 d['a'] = 5
121 self.assertEqual(p(), ((), {'a': 3}))
122
123 def test_arg_combinations(self):
124 # exercise special code paths for zero args in either partial
125 # object or the caller
126 p = self.partial(capture)
127 self.assertEqual(p(), ((), {}))
128 self.assertEqual(p(1,2), ((1,2), {}))
129 p = self.partial(capture, 1, 2)
130 self.assertEqual(p(), ((1,2), {}))
131 self.assertEqual(p(3,4), ((1,2,3,4), {}))
132
133 def test_kw_combinations(self):
134 # exercise special code paths for no keyword args in
135 # either the partial object or the caller
136 p = self.partial(capture)
137 self.assertEqual(p.keywords, {})
138 self.assertEqual(p(), ((), {}))
139 self.assertEqual(p(a=1), ((), {'a':1}))
140 p = self.partial(capture, a=1)
141 self.assertEqual(p.keywords, {'a':1})
142 self.assertEqual(p(), ((), {'a':1}))
143 self.assertEqual(p(b=2), ((), {'a':1, 'b':2}))
144 # keyword args in the call override those in the partial object
145 self.assertEqual(p(a=3, b=2), ((), {'a':3, 'b':2}))
146
147 def test_positional(self):
148 # make sure positional arguments are captured correctly
149 for args in [(), (0,), (0,1), (0,1,2), (0,1,2,3)]:
150 p = self.partial(capture, *args)
151 expected = args + ('x',)
152 got, empty = p('x')
153 self.assertTrue(expected == got and empty == {})
154
155 def test_keyword(self):
156 # make sure keyword arguments are captured correctly
157 for a in ['a', 0, None, 3.5]:
158 p = self.partial(capture, a=a)
159 expected = {'a':a,'x':None}
160 empty, got = p(x=None)
161 self.assertTrue(expected == got and empty == ())
162
163 def test_no_side_effects(self):
164 # make sure there are no side effects that affect subsequent calls
165 p = self.partial(capture, 0, a=1)
166 args1, kw1 = p(1, b=2)
167 self.assertTrue(args1 == (0,1) and kw1 == {'a':1,'b':2})
168 args2, kw2 = p()
169 self.assertTrue(args2 == (0,) and kw2 == {'a':1})
170
171 def test_error_propagation(self):
172 def f(x, y):
173 x / y
174 self.assertRaises(ZeroDivisionError, self.partial(f, 1, 0))
175 self.assertRaises(ZeroDivisionError, self.partial(f, 1), 0)
176 self.assertRaises(ZeroDivisionError, self.partial(f), 1, 0)
177 self.assertRaises(ZeroDivisionError, self.partial(f, y=0), 1)
178
179 def test_weakref(self):
180 f = self.partial(int, base=16)
181 p = proxy(f)
182 self.assertEqual(f.func, p.func)
183 f = None
184 support.gc_collect() # For PyPy or other GCs.
185 self.assertRaises(ReferenceError, getattr, p, 'func')
186
187 def test_with_bound_and_unbound_methods(self):
188 data = list(map(str, range(10)))
189 join = self.partial(str.join, '')
190 self.assertEqual(join(data), '0123456789')
191 join = self.partial(''.join)
192 self.assertEqual(join(data), '0123456789')
193
194 def test_nested_optimization(self):
195 partial = self.partial
196 inner = partial(signature, 'asdf')
197 nested = partial(inner, bar=True)
198 flat = partial(signature, 'asdf', bar=True)
199 self.assertEqual(signature(nested), signature(flat))
200
201 def test_nested_optimization_bug(self):
202 partial = self.partial
203 class Builder:
204 def __call__(self, tag, *children, **attrib):
205 return (tag, children, attrib)
206
207 def __getattr__(self, tag):
208 return partial(self, tag)
209
210 B = Builder()
211 m = B.m
212 assert m(1, 2, a=2) == ('m', (1, 2), dict(a=2))
213
214 def test_nested_partial_with_attribute(self):
215 # see issue 25137
216 partial = self.partial
217
218 def foo(bar):
219 return bar
220
221 p = partial(foo, 'first')
222 p2 = partial(p, 'second')
223 p2.new_attr = 'spam'
224 self.assertEqual(p2.new_attr, 'spam')
225
226 def test_placeholders_trailing_raise(self):
227 PH = self.module.Placeholder
228 for args in [(PH,), (0, PH), (0, PH, 1, PH, PH, PH)]:
229 with self.assertRaises(TypeError):
230 self.partial(capture, *args)
231
232 def test_placeholders(self):
233 PH = self.module.Placeholder
234 # 1 Placeholder
235 args = (PH, 0)
236 p = self.partial(capture, *args)
237 actual_args, actual_kwds = p('x')
238 self.assertEqual(actual_args, ('x', 0))
239 self.assertEqual(actual_kwds, {})
240 # 2 Placeholders
241 args = (PH, 0, PH, 1)
242 p = self.partial(capture, *args)
243 with self.assertRaises(TypeError):
244 p('x')
245 actual_args, actual_kwds = p('x', 'y')
246 self.assertEqual(actual_args, ('x', 0, 'y', 1))
247 self.assertEqual(actual_kwds, {})
248 # Checks via `is` and not `eq`
249 # thus ALWAYS_EQ isn't treated as Placeholder
250 p = self.partial(capture, ALWAYS_EQ)
251 actual_args, actual_kwds = p()
252 self.assertEqual(len(actual_args), 1)
253 self.assertIs(actual_args[0], ALWAYS_EQ)
254 self.assertEqual(actual_kwds, {})
255
256 def test_placeholders_optimization(self):
257 PH = self.module.Placeholder
258 p = self.partial(capture, PH, 0)
259 p2 = self.partial(p, PH, 1, 2, 3)
260 self.assertEqual(p2.args, (PH, 0, 1, 2, 3))
261 p3 = self.partial(p2, -1, 4)
262 actual_args, actual_kwds = p3(5)
263 self.assertEqual(actual_args, (-1, 0, 1, 2, 3, 4, 5))
264 self.assertEqual(actual_kwds, {})
265 # inner partial has placeholders and outer partial has no args case
266 p = self.partial(capture, PH, 0)
267 p2 = self.partial(p)
268 self.assertEqual(p2.args, (PH, 0))
269 self.assertEqual(p2(1), ((1, 0), {}))
270
271 def test_placeholders_kw_restriction(self):
272 PH = self.module.Placeholder
273 with self.assertRaisesRegex(TypeError, "Placeholder"):
274 self.partial(capture, a=PH)
275 # Passes, as checks via `is` and not `eq`
276 p = self.partial(capture, a=ALWAYS_EQ)
277 actual_args, actual_kwds = p()
278 self.assertEqual(actual_args, ())
279 self.assertEqual(len(actual_kwds), 1)
280 self.assertIs(actual_kwds['a'], ALWAYS_EQ)
281
282 def test_construct_placeholder_singleton(self):
283 PH = self.module.Placeholder
284 tp = type(PH)
285 self.assertIs(tp(), PH)
286 self.assertRaises(TypeError, tp, 1, 2)
287 self.assertRaises(TypeError, tp, a=1, b=2)
288
289 def test_repr(self):
290 args = (object(), object())
291 args_repr = ', '.join(repr(a) for a in args)
292 kwargs = {'a': object(), 'b': object()}
293 kwargs_reprs = ['a={a!r}, b={b!r}'.format_map(kwargs),
294 'b={b!r}, a={a!r}'.format_map(kwargs)]
295 name = f"{self.partial.__module__}.{self.partial.__qualname__}"
296
297 f = self.partial(capture)
298 self.assertEqual(f'{name}({capture!r})', repr(f))
299
300 f = self.partial(capture, *args)
301 self.assertEqual(f'{name}({capture!r}, {args_repr})', repr(f))
302
303 f = self.partial(capture, **kwargs)
304 self.assertIn(repr(f),
305 [f'{name}({capture!r}, {kwargs_repr})'
306 for kwargs_repr in kwargs_reprs])
307
308 f = self.partial(capture, *args, **kwargs)
309 self.assertIn(repr(f),
310 [f'{name}({capture!r}, {args_repr}, {kwargs_repr})'
311 for kwargs_repr in kwargs_reprs])
312
313 def test_recursive_repr(self):
314 name = f"{self.partial.__module__}.{self.partial.__qualname__}"
315
316 f = self.partial(capture)
317 f.__setstate__((f, (), {}, {}))
318 try:
319 self.assertEqual(repr(f), '%s(...)' % (name,))
320 finally:
321 f.__setstate__((capture, (), {}, {}))
322
323 f = self.partial(capture)
324 f.__setstate__((capture, (f,), {}, {}))
325 try:
326 self.assertEqual(repr(f), '%s(%r, ...)' % (name, capture,))
327 finally:
328 f.__setstate__((capture, (), {}, {}))
329
330 f = self.partial(capture)
331 f.__setstate__((capture, (), {'a': f}, {}))
332 try:
333 self.assertEqual(repr(f), '%s(%r, a=...)' % (name, capture,))
334 finally:
335 f.__setstate__((capture, (), {}, {}))
336
337 def test_pickle(self):
338 with replaced_module('functools', self.module):
339 f = self.partial(signature, ['asdf'], bar=[True])
340 f.attr = []
341 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
342 f_copy = pickle.loads(pickle.dumps(f, proto))
343 self.assertEqual(signature(f_copy), signature(f))
344
345 def test_copy(self):
346 f = self.partial(signature, ['asdf'], bar=[True])
347 f.attr = []
348 f_copy = copy.copy(f)
349 self.assertEqual(signature(f_copy), signature(f))
350 self.assertIs(f_copy.attr, f.attr)
351 self.assertIs(f_copy.args, f.args)
352 self.assertIs(f_copy.keywords, f.keywords)
353
354 def test_deepcopy(self):
355 f = self.partial(signature, ['asdf'], bar=[True])
356 f.attr = []
357 f_copy = copy.deepcopy(f)
358 self.assertEqual(signature(f_copy), signature(f))
359 self.assertIsNot(f_copy.attr, f.attr)
360 self.assertIsNot(f_copy.args, f.args)
361 self.assertIsNot(f_copy.args[0], f.args[0])
362 self.assertIsNot(f_copy.keywords, f.keywords)
363 self.assertIsNot(f_copy.keywords['bar'], f.keywords['bar'])
364
365 def test_setstate(self):
366 f = self.partial(signature)
367 f.__setstate__((capture, (1,), dict(a=10), dict(attr=[])))
368
369 self.assertEqual(signature(f),
370 (capture, (1,), dict(a=10), dict(attr=[])))
371 self.assertEqual(f(2, b=20), ((1, 2), {'a': 10, 'b': 20}))
372
373 f.__setstate__((capture, (1,), dict(a=10), None))
374
375 self.assertEqual(signature(f), (capture, (1,), dict(a=10), {}))
376 self.assertEqual(f(2, b=20), ((1, 2), {'a': 10, 'b': 20}))
377
378 f.__setstate__((capture, (1,), None, None))
379 #self.assertEqual(signature(f), (capture, (1,), {}, {}))
380 self.assertEqual(f(2, b=20), ((1, 2), {'b': 20}))
381 self.assertEqual(f(2), ((1, 2), {}))
382 self.assertEqual(f(), ((1,), {}))
383
384 f.__setstate__((capture, (), {}, None))
385 self.assertEqual(signature(f), (capture, (), {}, {}))
386 self.assertEqual(f(2, b=20), ((2,), {'b': 20}))
387 self.assertEqual(f(2), ((2,), {}))
388 self.assertEqual(f(), ((), {}))
389
390 # Set State with placeholders
391 PH = self.module.Placeholder
392 f = self.partial(signature)
393 f.__setstate__((capture, (PH, 1), dict(a=10), dict(attr=[])))
394 self.assertEqual(signature(f), (capture, (PH, 1), dict(a=10), dict(attr=[])))
395 msg_regex = re.escape("missing positional arguments in 'partial' call; "
396 "expected at least 1, got 0")
397 with self.assertRaisesRegex(TypeError, f'^{msg_regex}$') as cm:
398 f()
399 self.assertEqual(f(2), ((2, 1), dict(a=10)))
400
401 # Trailing Placeholder error
402 f = self.partial(signature)
403 msg_regex = re.escape("trailing Placeholders are not allowed")
404 with self.assertRaisesRegex(TypeError, f'^{msg_regex}$') as cm:
405 f.__setstate__((capture, (1, PH), dict(a=10), dict(attr=[])))
406
407 def test_setstate_errors(self):
408 f = self.partial(signature)
409
410 self.assertRaises(TypeError, f.__setstate__, (capture, (), {}))
411 self.assertRaises(TypeError, f.__setstate__, (capture, (), {}, {}, None))
412 self.assertRaises(TypeError, f.__setstate__, [capture, (), {}, None])
413 self.assertRaises(TypeError, f.__setstate__, (None, (), {}, None))
414 self.assertRaises(TypeError, f.__setstate__, (capture, None, {}, None))
415 self.assertRaises(TypeError, f.__setstate__, (capture, [], {}, None))
416 self.assertRaises(TypeError, f.__setstate__, (capture, (), [], None))
417 self.assertRaises(TypeError, f.__setstate__, (capture, (), {}, ()))
418 self.assertRaises(TypeError, f.__setstate__, (capture, (), {}, 'test'))
419
420 def test_setstate_subclasses(self):
421 f = self.partial(signature)
422 f.__setstate__((capture, MyTuple((1,)), MyDict(a=10), None))
423 s = signature(f)
424 self.assertEqual(s, (capture, (1,), dict(a=10), {}))
425 self.assertIs(type(s[1]), tuple)
426 self.assertIs(type(s[2]), dict)
427 r = f()
428 self.assertEqual(r, ((1,), {'a': 10}))
429 self.assertIs(type(r[0]), tuple)
430 self.assertIs(type(r[1]), dict)
431
432 f.__setstate__((capture, BadTuple((1,)), {}, None))
433 s = signature(f)
434 self.assertEqual(s, (capture, (1,), {}, {}))
435 self.assertIs(type(s[1]), tuple)
436 r = f(2)
437 self.assertEqual(r, ((1, 2), {}))
438 self.assertIs(type(r[0]), tuple)
439
440 @support.skip_if_sanitizer("thread sanitizer crashes in __tsan::FuncEntry", thread=True)
441 @support.skip_if_unlimited_stack_size
442 @support.skip_emscripten_stack_overflow()
443 def test_recursive_pickle(self):
444 with replaced_module('functools', self.module):
445 f = self.partial(capture)
446 f.__setstate__((f, (), {}, {}))
447 try:
448 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
449 # gh-117008: Small limit since pickle uses C stack memory
450 with support.infinite_recursion(100):
451 with self.assertRaises(RecursionError):
452 pickle.dumps(f, proto)
453 finally:
454 f.__setstate__((capture, (), {}, {}))
455
456 f = self.partial(capture)
457 f.__setstate__((capture, (f,), {}, {}))
458 try:
459 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
460 f_copy = pickle.loads(pickle.dumps(f, proto))
461 try:
462 self.assertIs(f_copy.args[0], f_copy)
463 finally:
464 f_copy.__setstate__((capture, (), {}, {}))
465 finally:
466 f.__setstate__((capture, (), {}, {}))
467
468 f = self.partial(capture)
469 f.__setstate__((capture, (), {'a': f}, {}))
470 try:
471 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
472 f_copy = pickle.loads(pickle.dumps(f, proto))
473 try:
474 self.assertIs(f_copy.keywords['a'], f_copy)
475 finally:
476 f_copy.__setstate__((capture, (), {}, {}))
477 finally:
478 f.__setstate__((capture, (), {}, {}))
479
480 # Issue 6083: Reference counting bug
481 def test_setstate_refcount(self):
482 class BadSequence:
483 def __len__(self):
484 return 4
485 def __getitem__(self, key):
486 if key == 0:
487 return max
488 elif key == 1:
489 return tuple(range(1000000))
490 elif key in (2, 3):
491 return {}
492 raise IndexError
493
494 f = self.partial(object)
495 self.assertRaises(TypeError, f.__setstate__, BadSequence())
496
497 def test_partial_as_method(self):
498 class A:
499 meth = self.partial(capture, 1, a=2)
500 cmeth = classmethod(self.partial(capture, 1, a=2))
501 smeth = staticmethod(self.partial(capture, 1, a=2))
502
503 a = A()
504 self.assertEqual(A.meth(3, b=4), ((1, 3), {'a': 2, 'b': 4}))
505 self.assertEqual(A.cmeth(3, b=4), ((1, A, 3), {'a': 2, 'b': 4}))
506 self.assertEqual(A.smeth(3, b=4), ((1, 3), {'a': 2, 'b': 4}))
507 self.assertEqual(a.meth(3, b=4), ((1, a, 3), {'a': 2, 'b': 4}))
508 self.assertEqual(a.cmeth(3, b=4), ((1, A, 3), {'a': 2, 'b': 4}))
509 self.assertEqual(a.smeth(3, b=4), ((1, 3), {'a': 2, 'b': 4}))
510
511 def test_partial_genericalias(self):
512 alias = self.partial[int]
513 self.assertIs(alias.__origin__, self.partial)
514 self.assertEqual(alias.__args__, (int,))
515 self.assertEqual(alias.__parameters__, ())
516
517 # GH-144475: Tests that the partial object does not change until repr finishes
518 def test_repr_safety_against_reentrant_mutation(self):
519 g_partial = None
520
521 class Function:
522 def __init__(self, name):
523 self.name = name
524
525 def __call__(self):
526 return None
527
528 def __repr__(self):
529 return f"Function({self.name})"
530
531 class EvilObject:
532 def __init__(self):
533 self.triggered = False
534
535 def __repr__(self):
536 if not self.triggered and g_partial is not None:
537 self.triggered = True
538 new_args_tuple = (None,)
539 new_keywords_dict = {"keyword": None}
540 new_tuple_state = (Function("new_function"), new_args_tuple, new_keywords_dict, None)
541 g_partial.__setstate__(new_tuple_state)
542 gc.collect()
543 return f"EvilObject"
544
545 trigger = EvilObject()
546 func = Function("old_function")
547
548 g_partial = functools.partial(func, None, trigger=trigger)
549 self.assertEqual(repr(g_partial),"functools.partial(Function(old_function), None, trigger=EvilObject)")
550
551 trigger.triggered = False
552 g_partial = functools.partial(func, trigger, arg=None)
553 self.assertEqual(repr(g_partial),"functools.partial(Function(old_function), EvilObject, arg=None)")
554
555
556 trigger.triggered = False
557 g_partial = functools.partial(func, trigger, None)
558 self.assertEqual(repr(g_partial),"functools.partial(Function(old_function), EvilObject, None)")
559
560 trigger.triggered = False
561 g_partial = functools.partial(func, trigger=trigger, arg=None)
562 self.assertEqual(repr(g_partial),"functools.partial(Function(old_function), trigger=EvilObject, arg=None)")
563
564 trigger.triggered = False
565 g_partial = functools.partial(func, trigger, None, None, None, None, arg=None)
566 self.assertEqual(repr(g_partial),"functools.partial(Function(old_function), EvilObject, None, None, None, None, arg=None)")
567
568
569
570 @unittest.skipUnless(c_functools, 'requires the C _functools module')
571 class TestPartialC(TestPartial, unittest.TestCase):
572 if c_functools:
573 module = c_functools
574 partial = c_functools.partial
575
576 def test_attributes_unwritable(self):
577 # attributes should not be writable
578 p = self.partial(capture, 1, 2, a=10, b=20)
579 self.assertRaises(AttributeError, setattr, p, 'func', map)
580 self.assertRaises(AttributeError, setattr, p, 'args', (1, 2))
581 self.assertRaises(AttributeError, setattr, p, 'keywords', dict(a=1, b=2))
582
583 p = self.partial(hex)
584 try:
585 del p.__dict__
586 except TypeError:
587 pass
588 else:
589 self.fail('partial object allowed __dict__ to be deleted')
590
591 def test_manually_adding_non_string_keyword(self):
592 p = self.partial(capture)
593 # Adding a non-string/unicode keyword to partial kwargs
594 p.keywords[1234] = 'value'
595 r = repr(p)
596 self.assertIn('1234', r)
597 self.assertIn("'value'", r)
598 with self.assertRaises(TypeError):
599 p()
600
601 def test_keystr_replaces_value(self):
602 p = self.partial(capture)
603
604 class MutatesYourDict(object):
605 def __str__(self):
606 p.keywords[self] = ['sth2']
607 return 'astr'
608
609 # Replacing the value during key formatting should keep the original
610 # value alive (at least long enough).
611 p.keywords[MutatesYourDict()] = ['sth']
612 r = repr(p)
613 self.assertIn('astr', r)
614 self.assertIn("['sth']", r)
615
616 def test_placeholders_refcount_smoke(self):
617 PH = self.module.Placeholder
618 # sum supports vector call
619 lst1, start = [], []
620 sum_lists = self.partial(sum, PH, start)
621 for i in range(10):
622 sum_lists([lst1, lst1])
623 # collections.ChainMap initializer does not support vectorcall
624 map1, map2 = {}, {}
625 partial_cm = self.partial(collections.ChainMap, PH, map1)
626 for i in range(10):
627 partial_cm(map2, map2)
628
629
630 class TestPartialPy(TestPartial, unittest.TestCase):
631 module = py_functools
632 partial = py_functools.partial
633
634
635 if c_functools:
636 class CPartialSubclass(c_functools.partial):
637 pass
638
639 class PyPartialSubclass(py_functools.partial):
640 pass
641
642 @unittest.skipUnless(c_functools, 'requires the C _functools module')
643 class TestPartialCSubclass(TestPartialC):
644 if c_functools:
645 partial = CPartialSubclass
646
647 # partial subclasses are not optimized for nested calls
648 test_nested_optimization = None
649
650 class TestPartialPySubclass(TestPartialPy):
651 partial = PyPartialSubclass
652
653 def test_subclass_optimization(self):
654 # `partial` input to `partial` subclass
655 p = py_functools.partial(min, 2)
656 p2 = self.partial(p, 1)
657 self.assertIs(p2.func, min)
658 self.assertEqual(p2(0), 0)
659 # `partial` subclass input to `partial` subclass
660 p = self.partial(min, 2)
661 p2 = self.partial(p, 1)
662 self.assertIs(p2.func, min)
663 self.assertEqual(p2(0), 0)
664
665
666 class TestPartialMethod(unittest.TestCase):
667
668 class A(object):
669 nothing = functools.partialmethod(capture)
670 positional = functools.partialmethod(capture, 1)
671 keywords = functools.partialmethod(capture, a=2)
672 both = functools.partialmethod(capture, 3, b=4)
673 spec_keywords = functools.partialmethod(capture, self=1, func=2)
674
675 nested = functools.partialmethod(positional, 5)
676
677 over_partial = functools.partialmethod(functools.partial(capture, c=6), 7)
678
679 static = functools.partialmethod(staticmethod(capture), 8)
680 cls = functools.partialmethod(classmethod(capture), d=9)
681
682 a = A()
683
684 def test_arg_combinations(self):
685 self.assertEqual(self.a.nothing(), ((self.a,), {}))
686 self.assertEqual(self.a.nothing(5), ((self.a, 5), {}))
687 self.assertEqual(self.a.nothing(c=6), ((self.a,), {'c': 6}))
688 self.assertEqual(self.a.nothing(5, c=6), ((self.a, 5), {'c': 6}))
689
690 self.assertEqual(self.a.positional(), ((self.a, 1), {}))
691 self.assertEqual(self.a.positional(5), ((self.a, 1, 5), {}))
692 self.assertEqual(self.a.positional(c=6), ((self.a, 1), {'c': 6}))
693 self.assertEqual(self.a.positional(5, c=6), ((self.a, 1, 5), {'c': 6}))
694
695 self.assertEqual(self.a.keywords(), ((self.a,), {'a': 2}))
696 self.assertEqual(self.a.keywords(5), ((self.a, 5), {'a': 2}))
697 self.assertEqual(self.a.keywords(c=6), ((self.a,), {'a': 2, 'c': 6}))
698 self.assertEqual(self.a.keywords(5, c=6), ((self.a, 5), {'a': 2, 'c': 6}))
699
700 self.assertEqual(self.a.both(), ((self.a, 3), {'b': 4}))
701 self.assertEqual(self.a.both(5), ((self.a, 3, 5), {'b': 4}))
702 self.assertEqual(self.a.both(c=6), ((self.a, 3), {'b': 4, 'c': 6}))
703 self.assertEqual(self.a.both(5, c=6), ((self.a, 3, 5), {'b': 4, 'c': 6}))
704
705 self.assertEqual(self.A.both(self.a, 5, c=6), ((self.a, 3, 5), {'b': 4, 'c': 6}))
706
707 self.assertEqual(self.a.spec_keywords(), ((self.a,), {'self': 1, 'func': 2}))
708
709 def test_nested(self):
710 self.assertEqual(self.a.nested(), ((self.a, 1, 5), {}))
711 self.assertEqual(self.a.nested(6), ((self.a, 1, 5, 6), {}))
712 self.assertEqual(self.a.nested(d=7), ((self.a, 1, 5), {'d': 7}))
713 self.assertEqual(self.a.nested(6, d=7), ((self.a, 1, 5, 6), {'d': 7}))
714
715 self.assertEqual(self.A.nested(self.a, 6, d=7), ((self.a, 1, 5, 6), {'d': 7}))
716
717 def test_over_partial(self):
718 self.assertEqual(self.a.over_partial(), ((self.a, 7), {'c': 6}))
719 self.assertEqual(self.a.over_partial(5), ((self.a, 7, 5), {'c': 6}))
720 self.assertEqual(self.a.over_partial(d=8), ((self.a, 7), {'c': 6, 'd': 8}))
721 self.assertEqual(self.a.over_partial(5, d=8), ((self.a, 7, 5), {'c': 6, 'd': 8}))
722
723 self.assertEqual(self.A.over_partial(self.a, 5, d=8), ((self.a, 7, 5), {'c': 6, 'd': 8}))
724
725 def test_bound_method_introspection(self):
726 obj = self.a
727 self.assertIs(obj.both.__self__, obj)
728 self.assertIs(obj.nested.__self__, obj)
729 self.assertIs(obj.over_partial.__self__, obj)
730 self.assertIs(obj.cls.__self__, self.A)
731 self.assertIs(self.A.cls.__self__, self.A)
732
733 def test_unbound_method_retrieval(self):
734 obj = self.A
735 self.assertNotHasAttr(obj.both, "__self__")
736 self.assertNotHasAttr(obj.nested, "__self__")
737 self.assertNotHasAttr(obj.over_partial, "__self__")
738 self.assertNotHasAttr(obj.static, "__self__")
739 self.assertNotHasAttr(self.a.static, "__self__")
740
741 def test_descriptors(self):
742 for obj in [self.A, self.a]:
743 with self.subTest(obj=obj):
744 self.assertEqual(obj.static(), ((8,), {}))
745 self.assertEqual(obj.static(5), ((8, 5), {}))
746 self.assertEqual(obj.static(d=8), ((8,), {'d': 8}))
747 self.assertEqual(obj.static(5, d=8), ((8, 5), {'d': 8}))
748
749 self.assertEqual(obj.cls(), ((self.A,), {'d': 9}))
750 self.assertEqual(obj.cls(5), ((self.A, 5), {'d': 9}))
751 self.assertEqual(obj.cls(c=8), ((self.A,), {'c': 8, 'd': 9}))
752 self.assertEqual(obj.cls(5, c=8), ((self.A, 5), {'c': 8, 'd': 9}))
753
754 def test_overriding_keywords(self):
755 self.assertEqual(self.a.keywords(a=3), ((self.a,), {'a': 3}))
756 self.assertEqual(self.A.keywords(self.a, a=3), ((self.a,), {'a': 3}))
757
758 def test_invalid_args(self):
759 with self.assertRaises(TypeError):
760 class B(object):
761 method = functools.partialmethod(None, 1)
762 with self.assertRaises(TypeError):
763 class B:
764 method = functools.partialmethod()
765 with self.assertRaises(TypeError):
766 class B:
767 method = functools.partialmethod(func=capture, a=1)
768
769 def test_repr(self):
770 self.assertEqual(repr(vars(self.A)['nothing']),
771 'functools.partialmethod({})'.format(capture))
772 self.assertEqual(repr(vars(self.A)['positional']),
773 'functools.partialmethod({}, 1)'.format(capture))
774 self.assertEqual(repr(vars(self.A)['keywords']),
775 'functools.partialmethod({}, a=2)'.format(capture))
776 self.assertEqual(repr(vars(self.A)['spec_keywords']),
777 'functools.partialmethod({}, self=1, func=2)'.format(capture))
778 self.assertEqual(repr(vars(self.A)['both']),
779 'functools.partialmethod({}, 3, b=4)'.format(capture))
780
781 def test_abstract(self):
782 class Abstract(abc.ABCMeta):
783
784 @abc.abstractmethod
785 def add(self, x, y):
786 pass
787
788 add5 = functools.partialmethod(add, 5)
789
790 self.assertTrue(Abstract.add.__isabstractmethod__)
791 self.assertTrue(Abstract.add5.__isabstractmethod__)
792
793 for func in [self.A.static, self.A.cls, self.A.over_partial, self.A.nested, self.A.both]:
794 self.assertFalse(getattr(func, '__isabstractmethod__', False))
795
796 def test_positional_only(self):
797 def f(a, b, /):
798 return a + b
799
800 p = functools.partial(f, 1)
801 self.assertEqual(p(2), f(1, 2))
802
803 def test_subclass_optimization(self):
804 class PartialMethodSubclass(functools.partialmethod):
805 pass
806 # `partialmethod` input to `partialmethod` subclass
807 p = functools.partialmethod(min, 2)
808 p2 = PartialMethodSubclass(p, 1)
809 self.assertIs(p2.func, min)
810 self.assertEqual(p2.__get__(0)(), 0)
811 # `partialmethod` subclass input to `partialmethod` subclass
812 p = PartialMethodSubclass(min, 2)
813 p2 = PartialMethodSubclass(p, 1)
814 self.assertIs(p2.func, min)
815 self.assertEqual(p2.__get__(0)(), 0)
816
817
818 class TestUpdateWrapper(unittest.TestCase):
819
820 def check_wrapper(self, wrapper, wrapped,
821 assigned=functools.WRAPPER_ASSIGNMENTS,
822 updated=functools.WRAPPER_UPDATES):
823 # Check attributes were assigned
824 for name in assigned:
825 self.assertIs(getattr(wrapper, name), getattr(wrapped, name))
826 # Check attributes were updated
827 for name in updated:
828 wrapper_attr = getattr(wrapper, name)
829 wrapped_attr = getattr(wrapped, name)
830 for key in wrapped_attr:
831 if name == "__dict__" and key == "__wrapped__":
832 # __wrapped__ is overwritten by the update code
833 continue
834 self.assertIs(wrapped_attr[key], wrapper_attr[key])
835 # Check __wrapped__
836 self.assertIs(wrapper.__wrapped__, wrapped)
837
838
839 def _default_update(self):
840 def f[T](a:'This is a new annotation'):
841 """This is a test"""
842 pass
843 f.attr = 'This is also a test'
844 f.__wrapped__ = "This is a bald faced lie"
845 def wrapper(b:'This is the prior annotation'):
846 pass
847 functools.update_wrapper(wrapper, f)
848 return wrapper, f
849
850 def test_default_update(self):
851 wrapper, f = self._default_update()
852 self.check_wrapper(wrapper, f)
853 T, = f.__type_params__
854 self.assertIs(wrapper.__wrapped__, f)
855 self.assertEqual(wrapper.__name__, 'f')
856 self.assertEqual(wrapper.__qualname__, f.__qualname__)
857 self.assertEqual(wrapper.attr, 'This is also a test')
858 self.assertEqual(wrapper.__annotations__['a'], 'This is a new annotation')
859 self.assertNotIn('b', wrapper.__annotations__)
860 self.assertEqual(wrapper.__type_params__, (T,))
861
862 @unittest.skipIf(sys.flags.optimize >= 2,
863 "Docstrings are omitted with -O2 and above")
864 def test_default_update_doc(self):
865 wrapper, f = self._default_update()
866 self.assertEqual(wrapper.__doc__, 'This is a test')
867
868 def test_no_update(self):
869 def f():
870 """This is a test"""
871 pass
872 f.attr = 'This is also a test'
873 def wrapper():
874 pass
875 functools.update_wrapper(wrapper, f, (), ())
876 self.check_wrapper(wrapper, f, (), ())
877 self.assertEqual(wrapper.__name__, 'wrapper')
878 self.assertNotEqual(wrapper.__qualname__, f.__qualname__)
879 self.assertEqual(wrapper.__doc__, None)
880 self.assertEqual(wrapper.__annotations__, {})
881 self.assertNotHasAttr(wrapper, 'attr')
882
883 def test_selective_update(self):
884 def f():
885 pass
886 f.attr = 'This is a different test'
887 f.dict_attr = dict(a=1, b=2, c=3)
888 def wrapper():
889 pass
890 wrapper.dict_attr = {}
891 assign = ('attr',)
892 update = ('dict_attr',)
893 functools.update_wrapper(wrapper, f, assign, update)
894 self.check_wrapper(wrapper, f, assign, update)
895 self.assertEqual(wrapper.__name__, 'wrapper')
896 self.assertNotEqual(wrapper.__qualname__, f.__qualname__)
897 self.assertEqual(wrapper.__doc__, None)
898 self.assertEqual(wrapper.attr, 'This is a different test')
899 self.assertEqual(wrapper.dict_attr, f.dict_attr)
900
901 def test_missing_attributes(self):
902 def f():
903 pass
904 def wrapper():
905 pass
906 wrapper.dict_attr = {}
907 assign = ('attr',)
908 update = ('dict_attr',)
909 # Missing attributes on wrapped object are ignored
910 functools.update_wrapper(wrapper, f, assign, update)
911 self.assertNotIn('attr', wrapper.__dict__)
912 self.assertEqual(wrapper.dict_attr, {})
913 # Wrapper must have expected attributes for updating
914 del wrapper.dict_attr
915 with self.assertRaises(AttributeError):
916 functools.update_wrapper(wrapper, f, assign, update)
917 wrapper.dict_attr = 1
918 with self.assertRaises(AttributeError):
919 functools.update_wrapper(wrapper, f, assign, update)
920
921 @support.requires_docstrings
922 @unittest.skipIf(sys.flags.optimize >= 2,
923 "Docstrings are omitted with -O2 and above")
924 def test_builtin_update(self):
925 # Test for bug #1576241
926 def wrapper():
927 pass
928 functools.update_wrapper(wrapper, max)
929 self.assertEqual(wrapper.__name__, 'max')
930 self.assertStartsWith(wrapper.__doc__, 'max(')
931 self.assertEqual(wrapper.__annotations__, {})
932
933 def test_update_type_wrapper(self):
934 def wrapper(*args): pass
935
936 functools.update_wrapper(wrapper, type)
937 self.assertEqual(wrapper.__name__, 'type')
938 self.assertEqual(wrapper.__annotations__, {})
939 self.assertEqual(wrapper.__type_params__, ())
940
941 def test_update_wrapper_annotations(self):
942 def inner(x: int): pass
943 def wrapper(*args): pass
944
945 functools.update_wrapper(wrapper, inner)
946 self.assertEqual(wrapper.__annotations__, {'x': int})
947 self.assertIs(wrapper.__annotate__, inner.__annotate__)
948
949 def with_forward_ref(x: undefined): pass
950 def wrapper(*args): pass
951
952 functools.update_wrapper(wrapper, with_forward_ref)
953
954 self.assertIs(wrapper.__annotate__, with_forward_ref.__annotate__)
955 with self.assertRaises(NameError):
956 wrapper.__annotations__
957
958 undefined = str
959 self.assertEqual(wrapper.__annotations__, {'x': undefined})
960
961
962 class TestWraps(TestUpdateWrapper):
963
964 def _default_update(self):
965 def f():
966 """This is a test"""
967 pass
968 f.attr = 'This is also a test'
969 f.__wrapped__ = "This is still a bald faced lie"
970 @functools.wraps(f)
971 def wrapper():
972 pass
973 return wrapper, f
974
975 def test_default_update(self):
976 wrapper, f = self._default_update()
977 self.check_wrapper(wrapper, f)
978 self.assertEqual(wrapper.__name__, 'f')
979 self.assertEqual(wrapper.__qualname__, f.__qualname__)
980 self.assertEqual(wrapper.attr, 'This is also a test')
981
982 @unittest.skipIf(sys.flags.optimize >= 2,
983 "Docstrings are omitted with -O2 and above")
984 def test_default_update_doc(self):
985 wrapper, _ = self._default_update()
986 self.assertEqual(wrapper.__doc__, 'This is a test')
987
988 def test_no_update(self):
989 def f():
990 """This is a test"""
991 pass
992 f.attr = 'This is also a test'
993 @functools.wraps(f, (), ())
994 def wrapper():
995 pass
996 self.check_wrapper(wrapper, f, (), ())
997 self.assertEqual(wrapper.__name__, 'wrapper')
998 self.assertNotEqual(wrapper.__qualname__, f.__qualname__)
999 self.assertEqual(wrapper.__doc__, None)
1000 self.assertNotHasAttr(wrapper, 'attr')
1001
1002 def test_selective_update(self):
1003 def f():
1004 pass
1005 f.attr = 'This is a different test'
1006 f.dict_attr = dict(a=1, b=2, c=3)
1007 def add_dict_attr(f):
1008 f.dict_attr = {}
1009 return f
1010 assign = ('attr',)
1011 update = ('dict_attr',)
1012 @functools.wraps(f, assign, update)
1013 @add_dict_attr
1014 def wrapper():
1015 pass
1016 self.check_wrapper(wrapper, f, assign, update)
1017 self.assertEqual(wrapper.__name__, 'wrapper')
1018 self.assertNotEqual(wrapper.__qualname__, f.__qualname__)
1019 self.assertEqual(wrapper.__doc__, None)
1020 self.assertEqual(wrapper.attr, 'This is a different test')
1021 self.assertEqual(wrapper.dict_attr, f.dict_attr)
1022
1023
1024 class TestReduce:
1025 def test_reduce(self):
1026 class Squares:
1027 def __init__(self, max):
1028 self.max = max
1029 self.sofar = []
1030
1031 def __len__(self):
1032 return len(self.sofar)
1033
1034 def __getitem__(self, i):
1035 if not 0 <= i < self.max: raise IndexError
1036 n = len(self.sofar)
1037 while n <= i:
1038 self.sofar.append(n*n)
1039 n += 1
1040 return self.sofar[i]
1041 def add(x, y):
1042 return x + y
1043 self.assertEqual(self.reduce(add, ['a', 'b', 'c'], ''), 'abc')
1044 self.assertEqual(
1045 self.reduce(add, [['a', 'c'], [], ['d', 'w']], []),
1046 ['a','c','d','w']
1047 )
1048 self.assertEqual(self.reduce(lambda x, y: x*y, range(2,8), 1), 5040)
1049 self.assertEqual(
1050 self.reduce(lambda x, y: x*y, range(2,21), 1),
1051 2432902008176640000
1052 )
1053 self.assertEqual(self.reduce(add, Squares(10)), 285)
1054 self.assertEqual(self.reduce(add, Squares(10), 0), 285)
1055 self.assertEqual(self.reduce(add, Squares(0), 0), 0)
1056 self.assertRaises(TypeError, self.reduce)
1057 self.assertRaises(TypeError, self.reduce, 42, 42)
1058 self.assertRaises(TypeError, self.reduce, 42, 42, 42)
1059 self.assertEqual(self.reduce(42, "1"), "1") # func is never called with one item
1060 self.assertEqual(self.reduce(42, "", "1"), "1") # func is never called with one item
1061 self.assertRaises(TypeError, self.reduce, 42, (42, 42))
1062 self.assertRaises(TypeError, self.reduce, add, []) # arg 2 must not be empty sequence with no initial value
1063 self.assertRaises(TypeError, self.reduce, add, "")
1064 self.assertRaises(TypeError, self.reduce, add, ())
1065 self.assertRaises(TypeError, self.reduce, add, object())
1066
1067 class TestFailingIter:
1068 def __iter__(self):
1069 raise RuntimeError
1070 self.assertRaises(RuntimeError, self.reduce, add, TestFailingIter())
1071
1072 self.assertEqual(self.reduce(add, [], None), None)
1073 self.assertEqual(self.reduce(add, [], 42), 42)
1074
1075 class BadSeq:
1076 def __getitem__(self, index):
1077 raise ValueError
1078 self.assertRaises(ValueError, self.reduce, 42, BadSeq())
1079
1080 # Test reduce()'s use of iterators.
1081 def test_iterator_usage(self):
1082 class SequenceClass:
1083 def __init__(self, n):
1084 self.n = n
1085 def __getitem__(self, i):
1086 if 0 <= i < self.n:
1087 return i
1088 else:
1089 raise IndexError
1090
1091 from operator import add
1092 self.assertEqual(self.reduce(add, SequenceClass(5)), 10)
1093 self.assertEqual(self.reduce(add, SequenceClass(5), 42), 52)
1094 self.assertRaises(TypeError, self.reduce, add, SequenceClass(0))
1095 self.assertEqual(self.reduce(add, SequenceClass(0), 42), 42)
1096 self.assertEqual(self.reduce(add, SequenceClass(1)), 0)
1097 self.assertEqual(self.reduce(add, SequenceClass(1), 42), 42)
1098
1099 d = {"one": 1, "two": 2, "three": 3}
1100 self.assertEqual(self.reduce(add, d), "".join(d.keys()))
1101
1102 # test correctness of keyword usage of `initial` in `reduce`
1103 def test_initial_keyword(self):
1104 def add(x, y):
1105 return x + y
1106 self.assertEqual(
1107 self.reduce(add, ['a', 'b', 'c'], ''),
1108 self.reduce(add, ['a', 'b', 'c'], initial=''),
1109 )
1110 self.assertEqual(
1111 self.reduce(add, [['a', 'c'], [], ['d', 'w']], []),
1112 self.reduce(add, [['a', 'c'], [], ['d', 'w']], initial=[]),
1113 )
1114 self.assertEqual(
1115 self.reduce(lambda x, y: x*y, range(2,8), 1),
1116 self.reduce(lambda x, y: x*y, range(2,8), initial=1),
1117 )
1118 self.assertEqual(
1119 self.reduce(lambda x, y: x*y, range(2,21), 1),
1120 self.reduce(lambda x, y: x*y, range(2,21), initial=1),
1121 )
1122 self.assertRaises(TypeError, self.reduce, add, [0, 1], initial="")
1123 self.assertEqual(self.reduce(42, "", initial="1"), "1") # func is never called with one item
1124
1125
1126 @unittest.skipUnless(c_functools, 'requires the C _functools module')
1127 class TestReduceC(TestReduce, unittest.TestCase):
1128 if c_functools:
1129 reduce = c_functools.reduce
1130
1131
1132 class TestReducePy(TestReduce, unittest.TestCase):
1133 reduce = staticmethod(py_functools.reduce)
1134
1135 def test_reduce_with_kwargs(self):
1136 with self.assertWarns(DeprecationWarning):
1137 self.reduce(function=lambda x, y: x + y, sequence=[1, 2, 3, 4, 5], initial=1)
1138 with self.assertWarns(DeprecationWarning):
1139 self.reduce(lambda x, y: x + y, sequence=[1, 2, 3, 4, 5], initial=1)
1140
1141
1142 class TestCmpToKey:
1143
1144 def test_cmp_to_key(self):
1145 def cmp1(x, y):
1146 return (x > y) - (x < y)
1147 key = self.cmp_to_key(cmp1)
1148 self.assertEqual(key(3), key(3))
1149 self.assertGreater(key(3), key(1))
1150 self.assertGreaterEqual(key(3), key(3))
1151
1152 def cmp2(x, y):
1153 return int(x) - int(y)
1154 key = self.cmp_to_key(cmp2)
1155 self.assertEqual(key(4.0), key('4'))
1156 self.assertLess(key(2), key('35'))
1157 self.assertLessEqual(key(2), key('35'))
1158 self.assertNotEqual(key(2), key('35'))
1159
1160 def test_cmp_to_key_arguments(self):
1161 def cmp1(x, y):
1162 return (x > y) - (x < y)
1163 key = self.cmp_to_key(mycmp=cmp1)
1164 self.assertEqual(key(obj=3), key(obj=3))
1165 self.assertGreater(key(obj=3), key(obj=1))
1166 with self.assertRaises((TypeError, AttributeError)):
1167 key(3) > 1 # rhs is not a K object
1168 with self.assertRaises((TypeError, AttributeError)):
1169 1 < key(3) # lhs is not a K object
1170 with self.assertRaises(TypeError):
1171 key = self.cmp_to_key() # too few args
1172 with self.assertRaises(TypeError):
1173 key = self.cmp_to_key(cmp1, None) # too many args
1174 key = self.cmp_to_key(cmp1)
1175 with self.assertRaises(TypeError):
1176 key() # too few args
1177 with self.assertRaises(TypeError):
1178 key(None, None) # too many args
1179
1180 def test_bad_cmp(self):
1181 def cmp1(x, y):
1182 raise ZeroDivisionError
1183 key = self.cmp_to_key(cmp1)
1184 with self.assertRaises(ZeroDivisionError):
1185 key(3) > key(1)
1186
1187 class BadCmp:
1188 def __lt__(self, other):
1189 raise ZeroDivisionError
1190 def cmp1(x, y):
1191 return BadCmp()
1192 with self.assertRaises(ZeroDivisionError):
1193 key(3) > key(1)
1194
1195 def test_obj_field(self):
1196 def cmp1(x, y):
1197 return (x > y) - (x < y)
1198 key = self.cmp_to_key(mycmp=cmp1)
1199 self.assertEqual(key(50).obj, 50)
1200
1201 def test_sort_int(self):
1202 def mycmp(x, y):
1203 return y - x
1204 self.assertEqual(sorted(range(5), key=self.cmp_to_key(mycmp)),
1205 [4, 3, 2, 1, 0])
1206
1207 def test_sort_int_str(self):
1208 def mycmp(x, y):
1209 x, y = int(x), int(y)
1210 return (x > y) - (x < y)
1211 values = [5, '3', 7, 2, '0', '1', 4, '10', 1]
1212 values = sorted(values, key=self.cmp_to_key(mycmp))
1213 self.assertEqual([int(value) for value in values],
1214 [0, 1, 1, 2, 3, 4, 5, 7, 10])
1215
1216 def test_hash(self):
1217 def mycmp(x, y):
1218 return y - x
1219 key = self.cmp_to_key(mycmp)
1220 k = key(10)
1221 self.assertRaises(TypeError, hash, k)
1222 self.assertNotIsInstance(k, collections.abc.Hashable)
1223
1224 @unittest.skipIf(support.MISSING_C_DOCSTRINGS,
1225 "Signature information for builtins requires docstrings")
1226 def test_cmp_to_signature(self):
1227 sig = Signature.from_callable(self.cmp_to_key)
1228 self.assertEqual(str(sig), '(mycmp)')
1229 def mycmp(x, y):
1230 return y - x
1231 sig = Signature.from_callable(self.cmp_to_key(mycmp))
1232 self.assertEqual(str(sig), '(obj)')
1233
1234
1235
1236 @unittest.skipUnless(c_functools, 'requires the C _functools module')
1237 class TestCmpToKeyC(TestCmpToKey, unittest.TestCase):
1238 if c_functools:
1239 cmp_to_key = c_functools.cmp_to_key
1240
1241 @support.cpython_only
1242 def test_disallow_instantiation(self):
1243 # Ensure that the type disallows instantiation (bpo-43916)
1244 support.check_disallow_instantiation(
1245 self, type(c_functools.cmp_to_key(None))
1246 )
1247
1248
1249 class TestCmpToKeyPy(TestCmpToKey, unittest.TestCase):
1250 cmp_to_key = staticmethod(py_functools.cmp_to_key)
1251
1252
1253 class TestTotalOrdering(unittest.TestCase):
1254
1255 def test_total_ordering_lt(self):
1256 @functools.total_ordering
1257 class A:
1258 def __init__(self, value):
1259 self.value = value
1260 def __lt__(self, other):
1261 return self.value < other.value
1262 def __eq__(self, other):
1263 return self.value == other.value
1264 self.assertTrue(A(1) < A(2))
1265 self.assertTrue(A(2) > A(1))
1266 self.assertTrue(A(1) <= A(2))
1267 self.assertTrue(A(2) >= A(1))
1268 self.assertTrue(A(2) <= A(2))
1269 self.assertTrue(A(2) >= A(2))
1270 self.assertFalse(A(1) > A(2))
1271
1272 def test_total_ordering_le(self):
1273 @functools.total_ordering
1274 class A:
1275 def __init__(self, value):
1276 self.value = value
1277 def __le__(self, other):
1278 return self.value <= other.value
1279 def __eq__(self, other):
1280 return self.value == other.value
1281 self.assertTrue(A(1) < A(2))
1282 self.assertTrue(A(2) > A(1))
1283 self.assertTrue(A(1) <= A(2))
1284 self.assertTrue(A(2) >= A(1))
1285 self.assertTrue(A(2) <= A(2))
1286 self.assertTrue(A(2) >= A(2))
1287 self.assertFalse(A(1) >= A(2))
1288
1289 def test_total_ordering_gt(self):
1290 @functools.total_ordering
1291 class A:
1292 def __init__(self, value):
1293 self.value = value
1294 def __gt__(self, other):
1295 return self.value > other.value
1296 def __eq__(self, other):
1297 return self.value == other.value
1298 self.assertTrue(A(1) < A(2))
1299 self.assertTrue(A(2) > A(1))
1300 self.assertTrue(A(1) <= A(2))
1301 self.assertTrue(A(2) >= A(1))
1302 self.assertTrue(A(2) <= A(2))
1303 self.assertTrue(A(2) >= A(2))
1304 self.assertFalse(A(2) < A(1))
1305
1306 def test_total_ordering_ge(self):
1307 @functools.total_ordering
1308 class A:
1309 def __init__(self, value):
1310 self.value = value
1311 def __ge__(self, other):
1312 return self.value >= other.value
1313 def __eq__(self, other):
1314 return self.value == other.value
1315 self.assertTrue(A(1) < A(2))
1316 self.assertTrue(A(2) > A(1))
1317 self.assertTrue(A(1) <= A(2))
1318 self.assertTrue(A(2) >= A(1))
1319 self.assertTrue(A(2) <= A(2))
1320 self.assertTrue(A(2) >= A(2))
1321 self.assertFalse(A(2) <= A(1))
1322
1323 def test_total_ordering_no_overwrite(self):
1324 # new methods should not overwrite existing
1325 @functools.total_ordering
1326 class A(int):
1327 pass
1328 self.assertTrue(A(1) < A(2))
1329 self.assertTrue(A(2) > A(1))
1330 self.assertTrue(A(1) <= A(2))
1331 self.assertTrue(A(2) >= A(1))
1332 self.assertTrue(A(2) <= A(2))
1333 self.assertTrue(A(2) >= A(2))
1334
1335 def test_no_operations_defined(self):
1336 with self.assertRaises(ValueError):
1337 @functools.total_ordering
1338 class A:
1339 pass
1340
1341 def test_notimplemented(self):
1342 # Verify NotImplemented results are correctly handled
1343 @functools.total_ordering
1344 class ImplementsLessThan:
1345 def __init__(self, value):
1346 self.value = value
1347 def __eq__(self, other):
1348 if isinstance(other, ImplementsLessThan):
1349 return self.value == other.value
1350 return False
1351 def __lt__(self, other):
1352 if isinstance(other, ImplementsLessThan):
1353 return self.value < other.value
1354 return NotImplemented
1355
1356 @functools.total_ordering
1357 class ImplementsLessThanEqualTo:
1358 def __init__(self, value):
1359 self.value = value
1360 def __eq__(self, other):
1361 if isinstance(other, ImplementsLessThanEqualTo):
1362 return self.value == other.value
1363 return False
1364 def __le__(self, other):
1365 if isinstance(other, ImplementsLessThanEqualTo):
1366 return self.value <= other.value
1367 return NotImplemented
1368
1369 @functools.total_ordering
1370 class ImplementsGreaterThan:
1371 def __init__(self, value):
1372 self.value = value
1373 def __eq__(self, other):
1374 if isinstance(other, ImplementsGreaterThan):
1375 return self.value == other.value
1376 return False
1377 def __gt__(self, other):
1378 if isinstance(other, ImplementsGreaterThan):
1379 return self.value > other.value
1380 return NotImplemented
1381
1382 @functools.total_ordering
1383 class ImplementsGreaterThanEqualTo:
1384 def __init__(self, value):
1385 self.value = value
1386 def __eq__(self, other):
1387 if isinstance(other, ImplementsGreaterThanEqualTo):
1388 return self.value == other.value
1389 return False
1390 def __ge__(self, other):
1391 if isinstance(other, ImplementsGreaterThanEqualTo):
1392 return self.value >= other.value
1393 return NotImplemented
1394
1395 self.assertIs(ImplementsLessThan(1).__le__(1), NotImplemented)
1396 self.assertIs(ImplementsLessThan(1).__gt__(1), NotImplemented)
1397 self.assertIs(ImplementsLessThan(1).__ge__(1), NotImplemented)
1398 self.assertIs(ImplementsLessThanEqualTo(1).__lt__(1), NotImplemented)
1399 self.assertIs(ImplementsLessThanEqualTo(1).__gt__(1), NotImplemented)
1400 self.assertIs(ImplementsLessThanEqualTo(1).__ge__(1), NotImplemented)
1401 self.assertIs(ImplementsGreaterThan(1).__lt__(1), NotImplemented)
1402 self.assertIs(ImplementsGreaterThan(1).__gt__(1), NotImplemented)
1403 self.assertIs(ImplementsGreaterThan(1).__ge__(1), NotImplemented)
1404 self.assertIs(ImplementsGreaterThanEqualTo(1).__lt__(1), NotImplemented)
1405 self.assertIs(ImplementsGreaterThanEqualTo(1).__le__(1), NotImplemented)
1406 self.assertIs(ImplementsGreaterThanEqualTo(1).__gt__(1), NotImplemented)
1407
1408 def test_type_error_when_not_implemented(self):
1409 # bug 10042; ensure stack overflow does not occur
1410 # when decorated types return NotImplemented
1411 @functools.total_ordering
1412 class ImplementsLessThan:
1413 def __init__(self, value):
1414 self.value = value
1415 def __eq__(self, other):
1416 if isinstance(other, ImplementsLessThan):
1417 return self.value == other.value
1418 return False
1419 def __lt__(self, other):
1420 if isinstance(other, ImplementsLessThan):
1421 return self.value < other.value
1422 return NotImplemented
1423
1424 @functools.total_ordering
1425 class ImplementsGreaterThan:
1426 def __init__(self, value):
1427 self.value = value
1428 def __eq__(self, other):
1429 if isinstance(other, ImplementsGreaterThan):
1430 return self.value == other.value
1431 return False
1432 def __gt__(self, other):
1433 if isinstance(other, ImplementsGreaterThan):
1434 return self.value > other.value
1435 return NotImplemented
1436
1437 @functools.total_ordering
1438 class ImplementsLessThanEqualTo:
1439 def __init__(self, value):
1440 self.value = value
1441 def __eq__(self, other):
1442 if isinstance(other, ImplementsLessThanEqualTo):
1443 return self.value == other.value
1444 return False
1445 def __le__(self, other):
1446 if isinstance(other, ImplementsLessThanEqualTo):
1447 return self.value <= other.value
1448 return NotImplemented
1449
1450 @functools.total_ordering
1451 class ImplementsGreaterThanEqualTo:
1452 def __init__(self, value):
1453 self.value = value
1454 def __eq__(self, other):
1455 if isinstance(other, ImplementsGreaterThanEqualTo):
1456 return self.value == other.value
1457 return False
1458 def __ge__(self, other):
1459 if isinstance(other, ImplementsGreaterThanEqualTo):
1460 return self.value >= other.value
1461 return NotImplemented
1462
1463 @functools.total_ordering
1464 class ComparatorNotImplemented:
1465 def __init__(self, value):
1466 self.value = value
1467 def __eq__(self, other):
1468 if isinstance(other, ComparatorNotImplemented):
1469 return self.value == other.value
1470 return False
1471 def __lt__(self, other):
1472 return NotImplemented
1473
1474 with self.subTest("LT < 1"), self.assertRaises(TypeError):
1475 ImplementsLessThan(-1) < 1
1476
1477 with self.subTest("LT < LE"), self.assertRaises(TypeError):
1478 ImplementsLessThan(0) < ImplementsLessThanEqualTo(0)
1479
1480 with self.subTest("LT < GT"), self.assertRaises(TypeError):
1481 ImplementsLessThan(1) < ImplementsGreaterThan(1)
1482
1483 with self.subTest("LE <= LT"), self.assertRaises(TypeError):
1484 ImplementsLessThanEqualTo(2) <= ImplementsLessThan(2)
1485
1486 with self.subTest("LE <= GE"), self.assertRaises(TypeError):
1487 ImplementsLessThanEqualTo(3) <= ImplementsGreaterThanEqualTo(3)
1488
1489 with self.subTest("GT > GE"), self.assertRaises(TypeError):
1490 ImplementsGreaterThan(4) > ImplementsGreaterThanEqualTo(4)
1491
1492 with self.subTest("GT > LT"), self.assertRaises(TypeError):
1493 ImplementsGreaterThan(5) > ImplementsLessThan(5)
1494
1495 with self.subTest("GE >= GT"), self.assertRaises(TypeError):
1496 ImplementsGreaterThanEqualTo(6) >= ImplementsGreaterThan(6)
1497
1498 with self.subTest("GE >= LE"), self.assertRaises(TypeError):
1499 ImplementsGreaterThanEqualTo(7) >= ImplementsLessThanEqualTo(7)
1500
1501 with self.subTest("GE when equal"):
1502 a = ComparatorNotImplemented(8)
1503 b = ComparatorNotImplemented(8)
1504 self.assertEqual(a, b)
1505 with self.assertRaises(TypeError):
1506 a >= b
1507
1508 with self.subTest("LE when equal"):
1509 a = ComparatorNotImplemented(9)
1510 b = ComparatorNotImplemented(9)
1511 self.assertEqual(a, b)
1512 with self.assertRaises(TypeError):
1513 a <= b
1514
1515 def test_pickle(self):
1516 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
1517 for name in '__lt__', '__gt__', '__le__', '__ge__':
1518 with self.subTest(method=name, proto=proto):
1519 method = getattr(Orderable_LT, name)
1520 method_copy = pickle.loads(pickle.dumps(method, proto))
1521 self.assertIs(method_copy, method)
1522
1523
1524 def test_total_ordering_for_metaclasses_issue_44605(self):
1525
1526 @functools.total_ordering
1527 class SortableMeta(type):
1528 def __new__(cls, name, bases, ns):
1529 return super().__new__(cls, name, bases, ns)
1530
1531 def __lt__(self, other):
1532 if not isinstance(other, SortableMeta):
1533 pass
1534 return self.__name__ < other.__name__
1535
1536 def __eq__(self, other):
1537 if not isinstance(other, SortableMeta):
1538 pass
1539 return self.__name__ == other.__name__
1540
1541 class B(metaclass=SortableMeta):
1542 pass
1543
1544 class A(metaclass=SortableMeta):
1545 pass
1546
1547 self.assertTrue(A < B)
1548 self.assertFalse(A > B)
1549
1550
1551 @functools.total_ordering
1552 class Orderable_LT:
1553 def __init__(self, value):
1554 self.value = value
1555 def __lt__(self, other):
1556 return self.value < other.value
1557 def __eq__(self, other):
1558 return self.value == other.value
1559
1560
1561 class TestCache:
1562 # This tests that the pass-through is working as designed.
1563 # The underlying functionality is tested in TestLRU.
1564
1565 def test_cache(self):
1566 @self.module.cache
1567 def fib(n):
1568 if n < 2:
1569 return n
1570 return fib(n-1) + fib(n-2)
1571 self.assertEqual([fib(n) for n in range(16)],
1572 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610])
1573 self.assertEqual(fib.cache_info(),
1574 self.module._CacheInfo(hits=28, misses=16, maxsize=None, currsize=16))
1575 fib.cache_clear()
1576 self.assertEqual(fib.cache_info(),
1577 self.module._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0))
1578
1579
1580 class TestCachePy(TestCache, unittest.TestCase):
1581 module = py_functools
1582
1583
1584 @unittest.skipUnless(c_functools, 'requires the C _functools module')
1585 class TestCacheC(TestCache, unittest.TestCase):
1586 if c_functools:
1587 module = c_functools
1588
1589
1590 class TestLRU:
1591
1592 def test_lru(self):
1593 def orig(x, y):
1594 return 3 * x + y
1595 f = self.module.lru_cache(maxsize=20)(orig)
1596 hits, misses, maxsize, currsize = f.cache_info()
1597 self.assertEqual(maxsize, 20)
1598 self.assertEqual(currsize, 0)
1599 self.assertEqual(hits, 0)
1600 self.assertEqual(misses, 0)
1601
1602 domain = range(5)
1603 for i in range(1000):
1604 x, y = choice(domain), choice(domain)
1605 actual = f(x, y)
1606 expected = orig(x, y)
1607 self.assertEqual(actual, expected)
1608 hits, misses, maxsize, currsize = f.cache_info()
1609 self.assertTrue(hits > misses)
1610 self.assertEqual(hits + misses, 1000)
1611 self.assertEqual(currsize, 20)
1612
1613 f.cache_clear() # test clearing
1614 hits, misses, maxsize, currsize = f.cache_info()
1615 self.assertEqual(hits, 0)
1616 self.assertEqual(misses, 0)
1617 self.assertEqual(currsize, 0)
1618 f(x, y)
1619 hits, misses, maxsize, currsize = f.cache_info()
1620 self.assertEqual(hits, 0)
1621 self.assertEqual(misses, 1)
1622 self.assertEqual(currsize, 1)
1623
1624 # Test bypassing the cache
1625 self.assertIs(f.__wrapped__, orig)
1626 f.__wrapped__(x, y)
1627 hits, misses, maxsize, currsize = f.cache_info()
1628 self.assertEqual(hits, 0)
1629 self.assertEqual(misses, 1)
1630 self.assertEqual(currsize, 1)
1631
1632 # test size zero (which means "never-cache")
1633 @self.module.lru_cache(0)
1634 def f():
1635 nonlocal f_cnt
1636 f_cnt += 1
1637 return 20
1638 self.assertEqual(f.cache_info().maxsize, 0)
1639 f_cnt = 0
1640 for i in range(5):
1641 self.assertEqual(f(), 20)
1642 self.assertEqual(f_cnt, 5)
1643 hits, misses, maxsize, currsize = f.cache_info()
1644 self.assertEqual(hits, 0)
1645 self.assertEqual(misses, 5)
1646 self.assertEqual(currsize, 0)
1647
1648 # test size one
1649 @self.module.lru_cache(1)
1650 def f():
1651 nonlocal f_cnt
1652 f_cnt += 1
1653 return 20
1654 self.assertEqual(f.cache_info().maxsize, 1)
1655 f_cnt = 0
1656 for i in range(5):
1657 self.assertEqual(f(), 20)
1658 self.assertEqual(f_cnt, 1)
1659 hits, misses, maxsize, currsize = f.cache_info()
1660 self.assertEqual(hits, 4)
1661 self.assertEqual(misses, 1)
1662 self.assertEqual(currsize, 1)
1663
1664 # test size two
1665 @self.module.lru_cache(2)
1666 def f(x):
1667 nonlocal f_cnt
1668 f_cnt += 1
1669 return x*10
1670 self.assertEqual(f.cache_info().maxsize, 2)
1671 f_cnt = 0
1672 for x in 7, 9, 7, 9, 7, 9, 8, 8, 8, 9, 9, 9, 8, 8, 8, 7:
1673 # * * * *
1674 self.assertEqual(f(x), x*10)
1675 self.assertEqual(f_cnt, 4)
1676 hits, misses, maxsize, currsize = f.cache_info()
1677 self.assertEqual(hits, 12)
1678 self.assertEqual(misses, 4)
1679 self.assertEqual(currsize, 2)
1680
1681 def test_lru_no_args(self):
1682 @self.module.lru_cache
1683 def square(x):
1684 return x ** 2
1685
1686 self.assertEqual(list(map(square, [10, 20, 10])),
1687 [100, 400, 100])
1688 self.assertEqual(square.cache_info().hits, 1)
1689 self.assertEqual(square.cache_info().misses, 2)
1690 self.assertEqual(square.cache_info().maxsize, 128)
1691 self.assertEqual(square.cache_info().currsize, 2)
1692
1693 def test_lru_bug_35780(self):
1694 # C version of the lru_cache was not checking to see if
1695 # the user function call has already modified the cache
1696 # (this arises in recursive calls and in multi-threading).
1697 # This cause the cache to have orphan links not referenced
1698 # by the cache dictionary.
1699
1700 once = True # Modified by f(x) below
1701
1702 @self.module.lru_cache(maxsize=10)
1703 def f(x):
1704 nonlocal once
1705 rv = f'.{x}.'
1706 if x == 20 and once:
1707 once = False
1708 rv = f(x)
1709 return rv
1710
1711 # Fill the cache
1712 for x in range(15):
1713 self.assertEqual(f(x), f'.{x}.')
1714 self.assertEqual(f.cache_info().currsize, 10)
1715
1716 # Make a recursive call and make sure the cache remains full
1717 self.assertEqual(f(20), '.20.')
1718 self.assertEqual(f.cache_info().currsize, 10)
1719
1720 def test_lru_bug_36650(self):
1721 # C version of lru_cache was treating a call with an empty **kwargs
1722 # dictionary as being distinct from a call with no keywords at all.
1723 # This did not result in an incorrect answer, but it did trigger
1724 # an unexpected cache miss.
1725
1726 @self.module.lru_cache()
1727 def f(x):
1728 pass
1729
1730 f(0)
1731 f(0, **{})
1732 self.assertEqual(f.cache_info().hits, 1)
1733
1734 def test_lru_hash_only_once(self):
1735 # To protect against weird reentrancy bugs and to improve
1736 # efficiency when faced with slow __hash__ methods, the
1737 # LRU cache guarantees that it will only call __hash__
1738 # only once per use as an argument to the cached function.
1739
1740 @self.module.lru_cache(maxsize=1)
1741 def f(x, y):
1742 return x * 3 + y
1743
1744 # Simulate the integer 5
1745 mock_int = unittest.mock.Mock()
1746 mock_int.__mul__ = unittest.mock.Mock(return_value=15)
1747 mock_int.__hash__ = unittest.mock.Mock(return_value=999)
1748
1749 # Add to cache: One use as an argument gives one call
1750 self.assertEqual(f(mock_int, 1), 16)
1751 self.assertEqual(mock_int.__hash__.call_count, 1)
1752 self.assertEqual(f.cache_info(), (0, 1, 1, 1))
1753
1754 # Cache hit: One use as an argument gives one additional call
1755 self.assertEqual(f(mock_int, 1), 16)
1756 self.assertEqual(mock_int.__hash__.call_count, 2)
1757 self.assertEqual(f.cache_info(), (1, 1, 1, 1))
1758
1759 # Cache eviction: No use as an argument gives no additional call
1760 self.assertEqual(f(6, 2), 20)
1761 self.assertEqual(mock_int.__hash__.call_count, 2)
1762 self.assertEqual(f.cache_info(), (1, 2, 1, 1))
1763
1764 # Cache miss: One use as an argument gives one additional call
1765 self.assertEqual(f(mock_int, 1), 16)
1766 self.assertEqual(mock_int.__hash__.call_count, 3)
1767 self.assertEqual(f.cache_info(), (1, 3, 1, 1))
1768
1769 def test_lru_reentrancy_with_len(self):
1770 # Test to make sure the LRU cache code isn't thrown-off by
1771 # caching the built-in len() function. Since len() can be
1772 # cached, we shouldn't use it inside the lru code itself.
1773 old_len = builtins.len
1774 try:
1775 builtins.len = self.module.lru_cache(4)(len)
1776 for i in [0, 0, 1, 2, 3, 3, 4, 5, 6, 1, 7, 2, 1]:
1777 self.assertEqual(len('abcdefghijklmn'[:i]), i)
1778 finally:
1779 builtins.len = old_len
1780
1781 def test_lru_star_arg_handling(self):
1782 # Test regression that arose in ea064ff3c10f
1783 @self.module.lru_cache()
1784 def f(*args):
1785 return args
1786
1787 self.assertEqual(f(1, 2), (1, 2))
1788 self.assertEqual(f((1, 2)), ((1, 2),))
1789
1790 def test_lru_type_error(self):
1791 # Regression test for issue #28653.
1792 # lru_cache was leaking when one of the arguments
1793 # wasn't cacheable.
1794
1795 @self.module.lru_cache(maxsize=None)
1796 def infinite_cache(o):
1797 pass
1798
1799 @self.module.lru_cache(maxsize=10)
1800 def limited_cache(o):
1801 pass
1802
1803 with self.assertRaises(TypeError):
1804 infinite_cache([])
1805
1806 with self.assertRaises(TypeError):
1807 limited_cache([])
1808
1809 def test_lru_with_maxsize_none(self):
1810 @self.module.lru_cache(maxsize=None)
1811 def fib(n):
1812 if n < 2:
1813 return n
1814 return fib(n-1) + fib(n-2)
1815 self.assertEqual([fib(n) for n in range(16)],
1816 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610])
1817 self.assertEqual(fib.cache_info(),
1818 self.module._CacheInfo(hits=28, misses=16, maxsize=None, currsize=16))
1819 fib.cache_clear()
1820 self.assertEqual(fib.cache_info(),
1821 self.module._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0))
1822
1823 def test_lru_with_maxsize_negative(self):
1824 @self.module.lru_cache(maxsize=-10)
1825 def eq(n):
1826 return n
1827 for i in (0, 1):
1828 self.assertEqual([eq(n) for n in range(150)], list(range(150)))
1829 self.assertEqual(eq.cache_info(),
1830 self.module._CacheInfo(hits=0, misses=300, maxsize=0, currsize=0))
1831
1832 def test_lru_with_exceptions(self):
1833 # Verify that user_function exceptions get passed through without
1834 # creating a hard-to-read chained exception.
1835 # http://bugs.python.org/issue13177
1836 for maxsize in (None, 128):
1837 @self.module.lru_cache(maxsize)
1838 def func(i):
1839 return 'abc'[i]
1840 self.assertEqual(func(0), 'a')
1841 with self.assertRaises(IndexError) as cm:
1842 func(15)
1843 self.assertIsNone(cm.exception.__context__)
1844 # Verify that the previous exception did not result in a cached entry
1845 with self.assertRaises(IndexError):
1846 func(15)
1847
1848 def test_lru_with_types(self):
1849 for maxsize in (None, 128):
1850 @self.module.lru_cache(maxsize=maxsize, typed=True)
1851 def square(x):
1852 return x * x
1853 self.assertEqual(square(3), 9)
1854 self.assertEqual(type(square(3)), type(9))
1855 self.assertEqual(square(3.0), 9.0)
1856 self.assertEqual(type(square(3.0)), type(9.0))
1857 self.assertEqual(square(x=3), 9)
1858 self.assertEqual(type(square(x=3)), type(9))
1859 self.assertEqual(square(x=3.0), 9.0)
1860 self.assertEqual(type(square(x=3.0)), type(9.0))
1861 self.assertEqual(square.cache_info().hits, 4)
1862 self.assertEqual(square.cache_info().misses, 4)
1863
1864 def test_lru_cache_typed_is_not_recursive(self):
1865 cached = self.module.lru_cache(typed=True)(repr)
1866
1867 self.assertEqual(cached(1), '1')
1868 self.assertEqual(cached(True), 'True')
1869 self.assertEqual(cached(1.0), '1.0')
1870 self.assertEqual(cached(0), '0')
1871 self.assertEqual(cached(False), 'False')
1872 self.assertEqual(cached(0.0), '0.0')
1873
1874 self.assertEqual(cached((1,)), '(1,)')
1875 self.assertEqual(cached((True,)), '(1,)')
1876 self.assertEqual(cached((1.0,)), '(1,)')
1877 self.assertEqual(cached((0,)), '(0,)')
1878 self.assertEqual(cached((False,)), '(0,)')
1879 self.assertEqual(cached((0.0,)), '(0,)')
1880
1881 class T(tuple):
1882 pass
1883
1884 self.assertEqual(cached(T((1,))), '(1,)')
1885 self.assertEqual(cached(T((True,))), '(1,)')
1886 self.assertEqual(cached(T((1.0,))), '(1,)')
1887 self.assertEqual(cached(T((0,))), '(0,)')
1888 self.assertEqual(cached(T((False,))), '(0,)')
1889 self.assertEqual(cached(T((0.0,))), '(0,)')
1890
1891 def test_lru_with_keyword_args(self):
1892 @self.module.lru_cache()
1893 def fib(n):
1894 if n < 2:
1895 return n
1896 return fib(n=n-1) + fib(n=n-2)
1897 self.assertEqual(
1898 [fib(n=number) for number in range(16)],
1899 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610]
1900 )
1901 self.assertEqual(fib.cache_info(),
1902 self.module._CacheInfo(hits=28, misses=16, maxsize=128, currsize=16))
1903 fib.cache_clear()
1904 self.assertEqual(fib.cache_info(),
1905 self.module._CacheInfo(hits=0, misses=0, maxsize=128, currsize=0))
1906
1907 def test_lru_with_keyword_args_maxsize_none(self):
1908 @self.module.lru_cache(maxsize=None)
1909 def fib(n):
1910 if n < 2:
1911 return n
1912 return fib(n=n-1) + fib(n=n-2)
1913 self.assertEqual([fib(n=number) for number in range(16)],
1914 [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610])
1915 self.assertEqual(fib.cache_info(),
1916 self.module._CacheInfo(hits=28, misses=16, maxsize=None, currsize=16))
1917 fib.cache_clear()
1918 self.assertEqual(fib.cache_info(),
1919 self.module._CacheInfo(hits=0, misses=0, maxsize=None, currsize=0))
1920
1921 def test_kwargs_order(self):
1922 # PEP 468: Preserving Keyword Argument Order
1923 @self.module.lru_cache(maxsize=10)
1924 def f(**kwargs):
1925 return list(kwargs.items())
1926 self.assertEqual(f(a=1, b=2), [('a', 1), ('b', 2)])
1927 self.assertEqual(f(b=2, a=1), [('b', 2), ('a', 1)])
1928 self.assertEqual(f.cache_info(),
1929 self.module._CacheInfo(hits=0, misses=2, maxsize=10, currsize=2))
1930
1931 def test_lru_cache_decoration(self):
1932 def f(zomg: 'zomg_annotation'):
1933 """f doc string"""
1934 return 42
1935 g = self.module.lru_cache()(f)
1936 for attr in self.module.WRAPPER_ASSIGNMENTS:
1937 self.assertEqual(getattr(g, attr), getattr(f, attr))
1938
1939 @threading_helper.requires_working_threading()
1940 def test_lru_cache_threaded(self):
1941 n, m = 5, 11
1942 def orig(x, y):
1943 return 3 * x + y
1944 f = self.module.lru_cache(maxsize=n*m)(orig)
1945 hits, misses, maxsize, currsize = f.cache_info()
1946 self.assertEqual(currsize, 0)
1947
1948 start = threading.Event()
1949 def full(k):
1950 start.wait(10)
1951 for _ in range(m):
1952 self.assertEqual(f(k, 0), orig(k, 0))
1953
1954 def clear():
1955 start.wait(10)
1956 for _ in range(2*m):
1957 f.cache_clear()
1958
1959 orig_si = sys.getswitchinterval()
1960 support.setswitchinterval(1e-6)
1961 try:
1962 # create n threads in order to fill cache
1963 threads = [threading.Thread(target=full, args=[k])
1964 for k in range(n)]
1965 with threading_helper.start_threads(threads):
1966 start.set()
1967
1968 hits, misses, maxsize, currsize = f.cache_info()
1969 if self.module is py_functools:
1970 # XXX: Why can be not equal?
1971 self.assertLessEqual(misses, n)
1972 self.assertLessEqual(hits, m*n - misses)
1973 else:
1974 self.assertEqual(misses, n)
1975 self.assertEqual(hits, m*n - misses)
1976 self.assertEqual(currsize, n)
1977
1978 # create n threads in order to fill cache and 1 to clear it
1979 threads = [threading.Thread(target=clear)]
1980 threads += [threading.Thread(target=full, args=[k])
1981 for k in range(n)]
1982 start.clear()
1983 with threading_helper.start_threads(threads):
1984 start.set()
1985 finally:
1986 sys.setswitchinterval(orig_si)
1987
1988 @threading_helper.requires_working_threading()
1989 def test_lru_cache_threaded2(self):
1990 # Simultaneous call with the same arguments
1991 n, m = 5, 7
1992 start = threading.Barrier(n+1)
1993 pause = threading.Barrier(n+1)
1994 stop = threading.Barrier(n+1)
1995 @self.module.lru_cache(maxsize=m*n)
1996 def f(x):
1997 pause.wait(10)
1998 return 3 * x
1999 self.assertEqual(f.cache_info(), (0, 0, m*n, 0))
2000 def test():
2001 for i in range(m):
2002 start.wait(10)
2003 self.assertEqual(f(i), 3 * i)
2004 stop.wait(10)
2005 threads = [threading.Thread(target=test) for k in range(n)]
2006 with threading_helper.start_threads(threads):
2007 for i in range(m):
2008 start.wait(10)
2009 stop.reset()
2010 pause.wait(10)
2011 start.reset()
2012 stop.wait(10)
2013 pause.reset()
2014 self.assertEqual(f.cache_info(), (0, (i+1)*n, m*n, i+1))
2015
2016 @threading_helper.requires_working_threading()
2017 def test_lru_cache_threaded3(self):
2018 @self.module.lru_cache(maxsize=2)
2019 def f(x):
2020 time.sleep(.01)
2021 return 3 * x
2022 def test(i, x):
2023 self.assertEqual(f(x), 3 * x, i)
2024 threads = [threading.Thread(target=test, args=(i, v))
2025 for i, v in enumerate([1, 2, 2, 3, 2])]
2026 with threading_helper.start_threads(threads):
2027 pass
2028
2029 def test_need_for_rlock(self):
2030 # This will deadlock on an LRU cache that uses a regular lock
2031
2032 @self.module.lru_cache(maxsize=10)
2033 def test_func(x):
2034 'Used to demonstrate a reentrant lru_cache call within a single thread'
2035 return x
2036
2037 class DoubleEq:
2038 'Demonstrate a reentrant lru_cache call within a single thread'
2039 def __init__(self, x):
2040 self.x = x
2041 def __hash__(self):
2042 return self.x
2043 def __eq__(self, other):
2044 if self.x == 2:
2045 test_func(DoubleEq(1))
2046 return self.x == other.x
2047
2048 test_func(DoubleEq(1)) # Load the cache
2049 test_func(DoubleEq(2)) # Load the cache
2050 self.assertEqual(test_func(DoubleEq(2)), # Trigger a re-entrant __eq__ call
2051 DoubleEq(2)) # Verify the correct return value
2052
2053 def test_lru_method(self):
2054 class X(int):
2055 f_cnt = 0
2056 @self.module.lru_cache(2)
2057 def f(self, x):
2058 self.f_cnt += 1
2059 return x*10+self
2060 a = X(5)
2061 b = X(5)
2062 c = X(7)
2063 self.assertEqual(X.f.cache_info(), (0, 0, 2, 0))
2064
2065 for x in 1, 2, 2, 3, 1, 1, 1, 2, 3, 3:
2066 self.assertEqual(a.f(x), x*10 + 5)
2067 self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 0, 0))
2068 self.assertEqual(X.f.cache_info(), (4, 6, 2, 2))
2069
2070 for x in 1, 2, 1, 1, 1, 1, 3, 2, 2, 2:
2071 self.assertEqual(b.f(x), x*10 + 5)
2072 self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 4, 0))
2073 self.assertEqual(X.f.cache_info(), (10, 10, 2, 2))
2074
2075 for x in 2, 1, 1, 1, 1, 2, 1, 3, 2, 1:
2076 self.assertEqual(c.f(x), x*10 + 7)
2077 self.assertEqual((a.f_cnt, b.f_cnt, c.f_cnt), (6, 4, 5))
2078 self.assertEqual(X.f.cache_info(), (15, 15, 2, 2))
2079
2080 self.assertEqual(a.f.cache_info(), X.f.cache_info())
2081 self.assertEqual(b.f.cache_info(), X.f.cache_info())
2082 self.assertEqual(c.f.cache_info(), X.f.cache_info())
2083
2084 def test_pickle(self):
2085 cls = self.__class__
2086 for f in cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth:
2087 for proto in range(pickle.HIGHEST_PROTOCOL + 1):
2088 with self.subTest(proto=proto, func=f):
2089 f_copy = pickle.loads(pickle.dumps(f, proto))
2090 self.assertIs(f_copy, f)
2091
2092 def test_copy(self):
2093 cls = self.__class__
2094 def orig(x, y):
2095 return 3 * x + y
2096 part = self.module.partial(orig, 2)
2097 funcs = (cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth,
2098 self.module.lru_cache(2)(part))
2099 for f in funcs:
2100 with self.subTest(func=f):
2101 f_copy = copy.copy(f)
2102 self.assertIs(f_copy, f)
2103
2104 def test_deepcopy(self):
2105 cls = self.__class__
2106 def orig(x, y):
2107 return 3 * x + y
2108 part = self.module.partial(orig, 2)
2109 funcs = (cls.cached_func[0], cls.cached_meth, cls.cached_staticmeth,
2110 self.module.lru_cache(2)(part))
2111 for f in funcs:
2112 with self.subTest(func=f):
2113 f_copy = copy.deepcopy(f)
2114 self.assertIs(f_copy, f)
2115
2116 def test_lru_cache_parameters(self):
2117 @self.module.lru_cache(maxsize=2)
2118 def f():
2119 return 1
2120 self.assertEqual(f.cache_parameters(), {'maxsize': 2, "typed": False})
2121
2122 @self.module.lru_cache(maxsize=1000, typed=True)
2123 def f():
2124 return 1
2125 self.assertEqual(f.cache_parameters(), {'maxsize': 1000, "typed": True})
2126
2127 def test_lru_cache_weakrefable(self):
2128 @self.module.lru_cache
2129 def test_function(x):
2130 return x
2131
2132 class A:
2133 @self.module.lru_cache
2134 def test_method(self, x):
2135 return (self, x)
2136
2137 @staticmethod
2138 @self.module.lru_cache
2139 def test_staticmethod(x):
2140 return (self, x)
2141
2142 refs = [weakref.ref(test_function),
2143 weakref.ref(A.test_method),
2144 weakref.ref(A.test_staticmethod)]
2145
2146 for ref in refs:
2147 self.assertIsNotNone(ref())
2148
2149 del A
2150 del test_function
2151 gc.collect()
2152
2153 for ref in refs:
2154 self.assertIsNone(ref())
2155
2156 def test_common_signatures(self):
2157 def orig(a, /, b, c=True): ...
2158 lru = self.module.lru_cache(1)(orig)
2159
2160 self.assertEqual(str(Signature.from_callable(lru)), '(a, /, b, c=True)')
2161 self.assertEqual(str(Signature.from_callable(lru.cache_info)), '()')
2162 self.assertEqual(str(Signature.from_callable(lru.cache_clear)), '()')
2163
2164 def test_get_annotations(self):
2165 def orig(a: int) -> str: ...
2166 lru = self.module.lru_cache(1)(orig)
2167
2168 self.assertEqual(
2169 get_annotations(orig), {"a": int, "return": str},
2170 )
2171 self.assertEqual(
2172 get_annotations(lru), {"a": int, "return": str},
2173 )
2174
2175 def test_get_annotations_with_forwardref(self):
2176 def orig(a: int) -> nonexistent: ...
2177 lru = self.module.lru_cache(1)(orig)
2178
2179 self.assertEqual(
2180 get_annotations(orig, format=Format.FORWARDREF),
2181 {"a": int, "return": EqualToForwardRef('nonexistent', owner=orig)},
2182 )
2183 self.assertEqual(
2184 get_annotations(lru, format=Format.FORWARDREF),
2185 {"a": int, "return": EqualToForwardRef('nonexistent', owner=lru)},
2186 )
2187 with self.assertRaises(NameError):
2188 get_annotations(orig, format=Format.VALUE)
2189 with self.assertRaises(NameError):
2190 get_annotations(lru, format=Format.VALUE)
2191
2192 @support.skip_on_s390x
2193 @unittest.skipIf(support.is_wasi, "WASI has limited C stack")
2194 @support.skip_if_sanitizer("requires deep stack", ub=True, thread=True)
2195 @support.skip_if_unlimited_stack_size
2196 @support.skip_emscripten_stack_overflow()
2197 def test_lru_recursion(self):
2198
2199 @self.module.lru_cache
2200 def fib(n):
2201 if n <= 1:
2202 return n
2203 return fib(n-1) + fib(n-2)
2204
2205 fib(100)
2206 if self.module == c_functools:
2207 fib.cache_clear()
2208 with support.infinite_recursion():
2209 with self.assertRaises(RecursionError):
2210 fib(support.exceeds_recursion_limit())
2211
2212
2213 @py_functools.lru_cache()
2214 def py_cached_func(x, y):
2215 return 3 * x + y
2216
2217 if c_functools:
2218 @c_functools.lru_cache()
2219 def c_cached_func(x, y):
2220 return 3 * x + y
2221
2222
2223 class TestLRUPy(TestLRU, unittest.TestCase):
2224 module = py_functools
2225 cached_func = py_cached_func,
2226
2227 @module.lru_cache()
2228 def cached_meth(self, x, y):
2229 return 3 * x + y
2230
2231 @staticmethod
2232 @module.lru_cache()
2233 def cached_staticmeth(x, y):
2234 return 3 * x + y
2235
2236
2237 @unittest.skipUnless(c_functools, 'requires the C _functools module')
2238 class TestLRUC(TestLRU, unittest.TestCase):
2239 if c_functools:
2240 module = c_functools
2241 cached_func = c_cached_func,
2242
2243 @module.lru_cache()
2244 def cached_meth(self, x, y):
2245 return 3 * x + y
2246
2247 @staticmethod
2248 @module.lru_cache()
2249 def cached_staticmeth(x, y):
2250 return 3 * x + y
2251
2252
2253 class TestSingleDispatch(unittest.TestCase):
2254 def test_simple_overloads(self):
2255 @functools.singledispatch
2256 def g(obj):
2257 return "base"
2258 def g_int(i):
2259 return "integer"
2260 g.register(int, g_int)
2261 self.assertEqual(g("str"), "base")
2262 self.assertEqual(g(1), "integer")
2263 self.assertEqual(g([1,2,3]), "base")
2264
2265 def test_mro(self):
2266 @functools.singledispatch
2267 def g(obj):
2268 return "base"
2269 class A:
2270 pass
2271 class C(A):
2272 pass
2273 class B(A):
2274 pass
2275 class D(C, B):
2276 pass
2277 def g_A(a):
2278 return "A"
2279 def g_B(b):
2280 return "B"
2281 g.register(A, g_A)
2282 g.register(B, g_B)
2283 self.assertEqual(g(A()), "A")
2284 self.assertEqual(g(B()), "B")
2285 self.assertEqual(g(C()), "A")
2286 self.assertEqual(g(D()), "B")
2287
2288 def test_register_decorator(self):
2289 @functools.singledispatch
2290 def g(obj):
2291 return "base"
2292 @g.register(int)
2293 def g_int(i):
2294 return "int %s" % (i,)
2295 self.assertEqual(g(""), "base")
2296 self.assertEqual(g(12), "int 12")
2297 self.assertIs(g.dispatch(int), g_int)
2298 self.assertIs(g.dispatch(object), g.dispatch(str))
2299 # Note: in the assert above this is not g.
2300 # @singledispatch returns the wrapper.
2301
2302 def test_wrapping_attributes(self):
2303 @functools.singledispatch
2304 def g(obj):
2305 "Simple test"
2306 return "Test"
2307 self.assertEqual(g.__name__, "g")
2308 if sys.flags.optimize < 2:
2309 self.assertEqual(g.__doc__, "Simple test")
2310
2311 @unittest.skipUnless(decimal, 'requires _decimal')
2312 @support.cpython_only
2313 def test_c_classes(self):
2314 @functools.singledispatch
2315 def g(obj):
2316 return "base"
2317 @g.register(decimal.DecimalException)
2318 def _(obj):
2319 return obj.args
2320 subn = decimal.Subnormal("Exponent < Emin")
2321 rnd = decimal.Rounded("Number got rounded")
2322 self.assertEqual(g(subn), ("Exponent < Emin",))
2323 self.assertEqual(g(rnd), ("Number got rounded",))
2324 @g.register(decimal.Subnormal)
2325 def _(obj):
2326 return "Too small to care."
2327 self.assertEqual(g(subn), "Too small to care.")
2328 self.assertEqual(g(rnd), ("Number got rounded",))
2329
2330 def test_compose_mro(self):
2331 # None of the examples in this test depend on haystack ordering.
2332 c = collections.abc
2333 mro = functools._compose_mro
2334 bases = [c.Sequence, c.MutableMapping, c.Mapping, c.Set]
2335 for haystack in permutations(bases):
2336 m = mro(dict, haystack)
2337 self.assertEqual(m, [dict, c.MutableMapping, c.Mapping,
2338 c.Collection, c.Sized, c.Iterable,
2339 c.Container, object])
2340 bases = [c.Container, c.Mapping, c.MutableMapping, collections.OrderedDict]
2341 for haystack in permutations(bases):
2342 m = mro(collections.ChainMap, haystack)
2343 self.assertEqual(m, [collections.ChainMap, c.MutableMapping, c.Mapping,
2344 c.Collection, c.Sized, c.Iterable,
2345 c.Container, object])
2346
2347 # If there's a generic function with implementations registered for
2348 # both Sized and Container, passing a defaultdict to it results in an
2349 # ambiguous dispatch which will cause a RuntimeError (see
2350 # test_mro_conflicts).
2351 bases = [c.Container, c.Sized, str]
2352 for haystack in permutations(bases):
2353 m = mro(collections.defaultdict, [c.Sized, c.Container, str])
2354 self.assertEqual(m, [collections.defaultdict, dict, c.Sized,
2355 c.Container, object])
2356
2357 # MutableSequence below is registered directly on D. In other words, it
2358 # precedes MutableMapping which means single dispatch will always
2359 # choose MutableSequence here.
2360 class D(collections.defaultdict):
2361 pass
2362 c.MutableSequence.register(D)
2363 bases = [c.MutableSequence, c.MutableMapping]
2364 for haystack in permutations(bases):
2365 m = mro(D, haystack)
2366 self.assertEqual(m, [D, c.MutableSequence, c.Sequence, c.Reversible,
2367 collections.defaultdict, dict, c.MutableMapping, c.Mapping,
2368 c.Collection, c.Sized, c.Iterable, c.Container,
2369 object])
2370
2371 # Container and Callable are registered on different base classes and
2372 # a generic function supporting both should always pick the Callable
2373 # implementation if a C instance is passed.
2374 class C(collections.defaultdict):
2375 def __call__(self):
2376 pass
2377 bases = [c.Sized, c.Callable, c.Container, c.Mapping]
2378 for haystack in permutations(bases):
2379 m = mro(C, haystack)
2380 self.assertEqual(m, [C, c.Callable, collections.defaultdict, dict, c.Mapping,
2381 c.Collection, c.Sized, c.Iterable,
2382 c.Container, object])
2383
2384 def test_register_abc(self):
2385 c = collections.abc
2386 d = {"a": "b"}
2387 l = [1, 2, 3]
2388 s = {object(), None}
2389 f = frozenset(s)
2390 t = (1, 2, 3)
2391 @functools.singledispatch
2392 def g(obj):
2393 return "base"
2394 self.assertEqual(g(d), "base")
2395 self.assertEqual(g(l), "base")
2396 self.assertEqual(g(s), "base")
2397 self.assertEqual(g(f), "base")
2398 self.assertEqual(g(t), "base")
2399 g.register(c.Sized, lambda obj: "sized")
2400 self.assertEqual(g(d), "sized")
2401 self.assertEqual(g(l), "sized")
2402 self.assertEqual(g(s), "sized")
2403 self.assertEqual(g(f), "sized")
2404 self.assertEqual(g(t), "sized")
2405 g.register(c.MutableMapping, lambda obj: "mutablemapping")
2406 self.assertEqual(g(d), "mutablemapping")
2407 self.assertEqual(g(l), "sized")
2408 self.assertEqual(g(s), "sized")
2409 self.assertEqual(g(f), "sized")
2410 self.assertEqual(g(t), "sized")
2411 g.register(collections.ChainMap, lambda obj: "chainmap")
2412 self.assertEqual(g(d), "mutablemapping") # irrelevant ABCs registered
2413 self.assertEqual(g(l), "sized")
2414 self.assertEqual(g(s), "sized")
2415 self.assertEqual(g(f), "sized")
2416 self.assertEqual(g(t), "sized")
2417 g.register(c.MutableSequence, lambda obj: "mutablesequence")
2418 self.assertEqual(g(d), "mutablemapping")
2419 self.assertEqual(g(l), "mutablesequence")
2420 self.assertEqual(g(s), "sized")
2421 self.assertEqual(g(f), "sized")
2422 self.assertEqual(g(t), "sized")
2423 g.register(c.MutableSet, lambda obj: "mutableset")
2424 self.assertEqual(g(d), "mutablemapping")
2425 self.assertEqual(g(l), "mutablesequence")
2426 self.assertEqual(g(s), "mutableset")
2427 self.assertEqual(g(f), "sized")
2428 self.assertEqual(g(t), "sized")
2429 g.register(c.Mapping, lambda obj: "mapping")
2430 self.assertEqual(g(d), "mutablemapping") # not specific enough
2431 self.assertEqual(g(l), "mutablesequence")
2432 self.assertEqual(g(s), "mutableset")
2433 self.assertEqual(g(f), "sized")
2434 self.assertEqual(g(t), "sized")
2435 g.register(c.Sequence, lambda obj: "sequence")
2436 self.assertEqual(g(d), "mutablemapping")
2437 self.assertEqual(g(l), "mutablesequence")
2438 self.assertEqual(g(s), "mutableset")
2439 self.assertEqual(g(f), "sized")
2440 self.assertEqual(g(t), "sequence")
2441 g.register(c.Set, lambda obj: "set")
2442 self.assertEqual(g(d), "mutablemapping")
2443 self.assertEqual(g(l), "mutablesequence")
2444 self.assertEqual(g(s), "mutableset")
2445 self.assertEqual(g(f), "set")
2446 self.assertEqual(g(t), "sequence")
2447 g.register(dict, lambda obj: "dict")
2448 self.assertEqual(g(d), "dict")
2449 self.assertEqual(g(l), "mutablesequence")
2450 self.assertEqual(g(s), "mutableset")
2451 self.assertEqual(g(f), "set")
2452 self.assertEqual(g(t), "sequence")
2453 g.register(list, lambda obj: "list")
2454 self.assertEqual(g(d), "dict")
2455 self.assertEqual(g(l), "list")
2456 self.assertEqual(g(s), "mutableset")
2457 self.assertEqual(g(f), "set")
2458 self.assertEqual(g(t), "sequence")
2459 g.register(set, lambda obj: "concrete-set")
2460 self.assertEqual(g(d), "dict")
2461 self.assertEqual(g(l), "list")
2462 self.assertEqual(g(s), "concrete-set")
2463 self.assertEqual(g(f), "set")
2464 self.assertEqual(g(t), "sequence")
2465 g.register(frozenset, lambda obj: "frozen-set")
2466 self.assertEqual(g(d), "dict")
2467 self.assertEqual(g(l), "list")
2468 self.assertEqual(g(s), "concrete-set")
2469 self.assertEqual(g(f), "frozen-set")
2470 self.assertEqual(g(t), "sequence")
2471 g.register(tuple, lambda obj: "tuple")
2472 self.assertEqual(g(d), "dict")
2473 self.assertEqual(g(l), "list")
2474 self.assertEqual(g(s), "concrete-set")
2475 self.assertEqual(g(f), "frozen-set")
2476 self.assertEqual(g(t), "tuple")
2477
2478 def test_c3_abc(self):
2479 c = collections.abc
2480 mro = functools._c3_mro
2481 class A(object):
2482 pass
2483 class B(A):
2484 def __len__(self):
2485 return 0 # implies Sized
2486 @c.Container.register
2487 class C(object):
2488 pass
2489 class D(object):
2490 pass # unrelated
2491 class X(D, C, B):
2492 def __call__(self):
2493 pass # implies Callable
2494 expected = [X, c.Callable, D, C, c.Container, B, c.Sized, A, object]
2495 for abcs in permutations([c.Sized, c.Callable, c.Container]):
2496 self.assertEqual(mro(X, abcs=abcs), expected)
2497 # unrelated ABCs don't appear in the resulting MRO
2498 many_abcs = [c.Mapping, c.Sized, c.Callable, c.Container, c.Iterable]
2499 self.assertEqual(mro(X, abcs=many_abcs), expected)
2500
2501 def test_false_meta(self):
2502 # see issue23572
2503 class MetaA(type):
2504 def __len__(self):
2505 return 0
2506 class A(metaclass=MetaA):
2507 pass
2508 class AA(A):
2509 pass
2510 @functools.singledispatch
2511 def fun(a):
2512 return 'base A'
2513 @fun.register(A)
2514 def _(a):
2515 return 'fun A'
2516 aa = AA()
2517 self.assertEqual(fun(aa), 'fun A')
2518
2519 def test_mro_conflicts(self):
2520 c = collections.abc
2521 @functools.singledispatch
2522 def g(arg):
2523 return "base"
2524 class O(c.Sized):
2525 def __len__(self):
2526 return 0
2527 o = O()
2528 self.assertEqual(g(o), "base")
2529 g.register(c.Iterable, lambda arg: "iterable")
2530 g.register(c.Container, lambda arg: "container")
2531 g.register(c.Sized, lambda arg: "sized")
2532 g.register(c.Set, lambda arg: "set")
2533 self.assertEqual(g(o), "sized")
2534 c.Iterable.register(O)
2535 self.assertEqual(g(o), "sized") # because it's explicitly in __mro__
2536 c.Container.register(O)
2537 self.assertEqual(g(o), "sized") # see above: Sized is in __mro__
2538 c.Set.register(O)
2539 self.assertEqual(g(o), "set") # because c.Set is a subclass of
2540 # c.Sized and c.Container
2541 class P:
2542 pass
2543 p = P()
2544 self.assertEqual(g(p), "base")
2545 c.Iterable.register(P)
2546 self.assertEqual(g(p), "iterable")
2547 c.Container.register(P)
2548 with self.assertRaises(RuntimeError) as re_one:
2549 g(p)
2550 self.assertIn(
2551 str(re_one.exception),
2552 (("Ambiguous dispatch: <class 'collections.abc.Container'> "
2553 "or <class 'collections.abc.Iterable'>"),
2554 ("Ambiguous dispatch: <class 'collections.abc.Iterable'> "
2555 "or <class 'collections.abc.Container'>")),
2556 )
2557 class Q(c.Sized):
2558 def __len__(self):
2559 return 0
2560 q = Q()
2561 self.assertEqual(g(q), "sized")
2562 c.Iterable.register(Q)
2563 self.assertEqual(g(q), "sized") # because it's explicitly in __mro__
2564 c.Set.register(Q)
2565 self.assertEqual(g(q), "set") # because c.Set is a subclass of
2566 # c.Sized and c.Iterable
2567 @functools.singledispatch
2568 def h(arg):
2569 return "base"
2570 @h.register(c.Sized)
2571 def _(arg):
2572 return "sized"
2573 @h.register(c.Container)
2574 def _(arg):
2575 return "container"
2576 # Even though Sized and Container are explicit bases of MutableMapping,
2577 # this ABC is implicitly registered on defaultdict which makes all of
2578 # MutableMapping's bases implicit as well from defaultdict's
2579 # perspective.
2580 with self.assertRaises(RuntimeError) as re_two:
2581 h(collections.defaultdict(lambda: 0))
2582 self.assertIn(
2583 str(re_two.exception),
2584 (("Ambiguous dispatch: <class 'collections.abc.Container'> "
2585 "or <class 'collections.abc.Sized'>"),
2586 ("Ambiguous dispatch: <class 'collections.abc.Sized'> "
2587 "or <class 'collections.abc.Container'>")),
2588 )
2589 class R(collections.defaultdict):
2590 pass
2591 c.MutableSequence.register(R)
2592 @functools.singledispatch
2593 def i(arg):
2594 return "base"
2595 @i.register(c.MutableMapping)
2596 def _(arg):
2597 return "mapping"
2598 @i.register(c.MutableSequence)
2599 def _(arg):
2600 return "sequence"
2601 r = R()
2602 self.assertEqual(i(r), "sequence")
2603 class S:
2604 pass
2605 class T(S, c.Sized):
2606 def __len__(self):
2607 return 0
2608 t = T()
2609 self.assertEqual(h(t), "sized")
2610 c.Container.register(T)
2611 self.assertEqual(h(t), "sized") # because it's explicitly in the MRO
2612 class U:
2613 def __len__(self):
2614 return 0
2615 u = U()
2616 self.assertEqual(h(u), "sized") # implicit Sized subclass inferred
2617 # from the existence of __len__()
2618 c.Container.register(U)
2619 # There is no preference for registered versus inferred ABCs.
2620 with self.assertRaises(RuntimeError) as re_three:
2621 h(u)
2622 self.assertIn(
2623 str(re_three.exception),
2624 (("Ambiguous dispatch: <class 'collections.abc.Container'> "
2625 "or <class 'collections.abc.Sized'>"),
2626 ("Ambiguous dispatch: <class 'collections.abc.Sized'> "
2627 "or <class 'collections.abc.Container'>")),
2628 )
2629 class V(c.Sized, S):
2630 def __len__(self):
2631 return 0
2632 @functools.singledispatch
2633 def j(arg):
2634 return "base"
2635 @j.register(S)
2636 def _(arg):
2637 return "s"
2638 @j.register(c.Container)
2639 def _(arg):
2640 return "container"
2641 v = V()
2642 self.assertEqual(j(v), "s")
2643 c.Container.register(V)
2644 self.assertEqual(j(v), "container") # because it ends up right after
2645 # Sized in the MRO
2646
2647 def test_cache_invalidation(self):
2648 from collections import UserDict
2649 import weakref
2650
2651 class TracingDict(UserDict):
2652 def __init__(self, *args, **kwargs):
2653 super(TracingDict, self).__init__(*args, **kwargs)
2654 self.set_ops = []
2655 self.get_ops = []
2656 def __getitem__(self, key):
2657 result = self.data[key]
2658 self.get_ops.append(key)
2659 return result
2660 def __setitem__(self, key, value):
2661 self.set_ops.append(key)
2662 self.data[key] = value
2663 def clear(self):
2664 self.data.clear()
2665
2666 td = TracingDict()
2667 with support.swap_attr(weakref, "WeakKeyDictionary", lambda: td):
2668 c = collections.abc
2669 @functools.singledispatch
2670 def g(arg):
2671 return "base"
2672 d = {}
2673 l = []
2674 self.assertEqual(len(td), 0)
2675 self.assertEqual(g(d), "base")
2676 self.assertEqual(len(td), 1)
2677 self.assertEqual(td.get_ops, [])
2678 self.assertEqual(td.set_ops, [dict])
2679 self.assertEqual(td.data[dict], g.registry[object])
2680 self.assertEqual(g(l), "base")
2681 self.assertEqual(len(td), 2)
2682 self.assertEqual(td.get_ops, [])
2683 self.assertEqual(td.set_ops, [dict, list])
2684 self.assertEqual(td.data[dict], g.registry[object])
2685 self.assertEqual(td.data[list], g.registry[object])
2686 self.assertEqual(td.data[dict], td.data[list])
2687 self.assertEqual(g(l), "base")
2688 self.assertEqual(g(d), "base")
2689 self.assertEqual(td.get_ops, [list, dict])
2690 self.assertEqual(td.set_ops, [dict, list])
2691 g.register(list, lambda arg: "list")
2692 self.assertEqual(td.get_ops, [list, dict])
2693 self.assertEqual(len(td), 0)
2694 self.assertEqual(g(d), "base")
2695 self.assertEqual(len(td), 1)
2696 self.assertEqual(td.get_ops, [list, dict])
2697 self.assertEqual(td.set_ops, [dict, list, dict])
2698 self.assertEqual(td.data[dict],
2699 functools._find_impl(dict, g.registry))
2700 self.assertEqual(g(l), "list")
2701 self.assertEqual(len(td), 2)
2702 self.assertEqual(td.get_ops, [list, dict])
2703 self.assertEqual(td.set_ops, [dict, list, dict, list])
2704 self.assertEqual(td.data[list],
2705 functools._find_impl(list, g.registry))
2706 class X:
2707 pass
2708 c.MutableMapping.register(X) # Will not invalidate the cache,
2709 # not using ABCs yet.
2710 self.assertEqual(g(d), "base")
2711 self.assertEqual(g(l), "list")
2712 self.assertEqual(td.get_ops, [list, dict, dict, list])
2713 self.assertEqual(td.set_ops, [dict, list, dict, list])
2714 g.register(c.Sized, lambda arg: "sized")
2715 self.assertEqual(len(td), 0)
2716 self.assertEqual(g(d), "sized")
2717 self.assertEqual(len(td), 1)
2718 self.assertEqual(td.get_ops, [list, dict, dict, list])
2719 self.assertEqual(td.set_ops, [dict, list, dict, list, dict])
2720 self.assertEqual(g(l), "list")
2721 self.assertEqual(len(td), 2)
2722 self.assertEqual(td.get_ops, [list, dict, dict, list])
2723 self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list])
2724 self.assertEqual(g(l), "list")
2725 self.assertEqual(g(d), "sized")
2726 self.assertEqual(td.get_ops, [list, dict, dict, list, list, dict])
2727 self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list])
2728 g.dispatch(list)
2729 g.dispatch(dict)
2730 self.assertEqual(td.get_ops, [list, dict, dict, list, list, dict,
2731 list, dict])
2732 self.assertEqual(td.set_ops, [dict, list, dict, list, dict, list])
2733 c.MutableSet.register(X) # Will invalidate the cache.
2734 self.assertEqual(len(td), 2) # Stale cache.
2735 self.assertEqual(g(l), "list")
2736 self.assertEqual(len(td), 1)
2737 g.register(c.MutableMapping, lambda arg: "mutablemapping")
2738 self.assertEqual(len(td), 0)
2739 self.assertEqual(g(d), "mutablemapping")
2740 self.assertEqual(len(td), 1)
2741 self.assertEqual(g(l), "list")
2742 self.assertEqual(len(td), 2)
2743 g.register(dict, lambda arg: "dict")
2744 self.assertEqual(g(d), "dict")
2745 self.assertEqual(g(l), "list")
2746 g._clear_cache()
2747 self.assertEqual(len(td), 0)
2748
2749 def test_annotations(self):
2750 @functools.singledispatch
2751 def i(arg):
2752 return "base"
2753 @i.register
2754 def _(arg: collections.abc.Mapping):
2755 return "mapping"
2756 @i.register
2757 def _(arg: "collections.abc.Sequence"):
2758 return "sequence"
2759 self.assertEqual(i(None), "base")
2760 self.assertEqual(i({"a": 1}), "mapping")
2761 self.assertEqual(i([1, 2, 3]), "sequence")
2762 self.assertEqual(i((1, 2, 3)), "sequence")
2763 self.assertEqual(i("str"), "sequence")
2764
2765 # Registering classes as callables doesn't work with annotations,
2766 # you need to pass the type explicitly.
2767 @i.register(str)
2768 class _:
2769 def __init__(self, arg):
2770 self.arg = arg
2771
2772 def __eq__(self, other):
2773 return self.arg == other
2774 self.assertEqual(i("str"), "str")
2775
2776 def test_method_register(self):
2777 class A:
2778 @functools.singledispatchmethod
2779 def t(self, arg):
2780 self.arg = "base"
2781 @t.register(int)
2782 def _(self, arg):
2783 self.arg = "int"
2784 @t.register(str)
2785 def _(self, arg):
2786 self.arg = "str"
2787 a = A()
2788
2789 a.t(0)
2790 self.assertEqual(a.arg, "int")
2791 aa = A()
2792 self.assertNotHasAttr(aa, 'arg')
2793 a.t('')
2794 self.assertEqual(a.arg, "str")
2795 aa = A()
2796 self.assertNotHasAttr(aa, 'arg')
2797 a.t(0.0)
2798 self.assertEqual(a.arg, "base")
2799 aa = A()
2800 self.assertNotHasAttr(aa, 'arg')
2801
2802 def test_staticmethod_register(self):
2803 class A:
2804 @functools.singledispatchmethod
2805 @staticmethod
2806 def t(arg):
2807 return arg
2808 @t.register(int)
2809 @staticmethod
2810 def _(arg):
2811 return isinstance(arg, int)
2812 @t.register(str)
2813 @staticmethod
2814 def _(arg):
2815 return isinstance(arg, str)
2816 a = A()
2817
2818 self.assertTrue(A.t(0))
2819 self.assertTrue(A.t(''))
2820 self.assertEqual(A.t(0.0), 0.0)
2821
2822 def test_slotted_class(self):
2823 class Slot:
2824 __slots__ = ('a', 'b')
2825 @functools.singledispatchmethod
2826 def go(self, item, arg):
2827 pass
2828
2829 @go.register
2830 def _(self, item: int, arg):
2831 return item + arg
2832
2833 s = Slot()
2834 self.assertEqual(s.go(1, 1), 2)
2835
2836 def test_classmethod_slotted_class(self):
2837 class Slot:
2838 __slots__ = ('a', 'b')
2839 @functools.singledispatchmethod
2840 @classmethod
2841 def go(cls, item, arg):
2842 pass
2843
2844 @go.register
2845 @classmethod
2846 def _(cls, item: int, arg):
2847 return item + arg
2848
2849 s = Slot()
2850 self.assertEqual(s.go(1, 1), 2)
2851 self.assertEqual(Slot.go(1, 1), 2)
2852
2853 def test_staticmethod_slotted_class(self):
2854 class A:
2855 __slots__ = ['a']
2856 @functools.singledispatchmethod
2857 @staticmethod
2858 def t(arg):
2859 return arg
2860 @t.register(int)
2861 @staticmethod
2862 def _(arg):
2863 return isinstance(arg, int)
2864 @t.register(str)
2865 @staticmethod
2866 def _(arg):
2867 return isinstance(arg, str)
2868 a = A()
2869
2870 self.assertTrue(A.t(0))
2871 self.assertTrue(A.t(''))
2872 self.assertEqual(A.t(0.0), 0.0)
2873 self.assertTrue(a.t(0))
2874 self.assertTrue(a.t(''))
2875 self.assertEqual(a.t(0.0), 0.0)
2876
2877 def test_assignment_behavior(self):
2878 # see gh-106448
2879 class A:
2880 @functools.singledispatchmethod
2881 def t(arg):
2882 return arg
2883
2884 a = A()
2885 a.t.foo = 'bar'
2886 a2 = A()
2887 with self.assertRaises(AttributeError):
2888 a2.t.foo
2889
2890 def test_classmethod_register(self):
2891 class A:
2892 def __init__(self, arg):
2893 self.arg = arg
2894
2895 @functools.singledispatchmethod
2896 @classmethod
2897 def t(cls, arg):
2898 return cls("base")
2899 @t.register(int)
2900 @classmethod
2901 def _(cls, arg):
2902 return cls("int")
2903 @t.register(str)
2904 @classmethod
2905 def _(cls, arg):
2906 return cls("str")
2907
2908 self.assertEqual(A.t(0).arg, "int")
2909 self.assertEqual(A.t('').arg, "str")
2910 self.assertEqual(A.t(0.0).arg, "base")
2911
2912 def test_callable_register(self):
2913 class A:
2914 def __init__(self, arg):
2915 self.arg = arg
2916
2917 @functools.singledispatchmethod
2918 @classmethod
2919 def t(cls, arg):
2920 return cls("base")
2921
2922 @A.t.register(int)
2923 @classmethod
2924 def _(cls, arg):
2925 return cls("int")
2926 @A.t.register(str)
2927 @classmethod
2928 def _(cls, arg):
2929 return cls("str")
2930
2931 self.assertEqual(A.t(0).arg, "int")
2932 self.assertEqual(A.t('').arg, "str")
2933 self.assertEqual(A.t(0.0).arg, "base")
2934
2935 def test_abstractmethod_register(self):
2936 class Abstract(metaclass=abc.ABCMeta):
2937
2938 @functools.singledispatchmethod
2939 @abc.abstractmethod
2940 def add(self, x, y):
2941 pass
2942
2943 self.assertTrue(Abstract.add.__isabstractmethod__)
2944 self.assertTrue(Abstract.__dict__['add'].__isabstractmethod__)
2945
2946 with self.assertRaises(TypeError):
2947 Abstract()
2948
2949 def test_type_ann_register(self):
2950 class A:
2951 @functools.singledispatchmethod
2952 def t(self, arg):
2953 return "base"
2954 @t.register
2955 def _(self, arg: int):
2956 return "int"
2957 @t.register
2958 def _(self, arg: str):
2959 return "str"
2960 a = A()
2961
2962 self.assertEqual(a.t(0), "int")
2963 self.assertEqual(a.t(''), "str")
2964 self.assertEqual(a.t(0.0), "base")
2965
2966 def test_staticmethod_type_ann_register(self):
2967 class A:
2968 @functools.singledispatchmethod
2969 @staticmethod
2970 def t(arg):
2971 return arg
2972 @t.register
2973 @staticmethod
2974 def _(arg: int):
2975 return isinstance(arg, int)
2976 @t.register
2977 @staticmethod
2978 def _(arg: str):
2979 return isinstance(arg, str)
2980 a = A()
2981
2982 self.assertTrue(A.t(0))
2983 self.assertTrue(A.t(''))
2984 self.assertEqual(A.t(0.0), 0.0)
2985
2986 def test_classmethod_type_ann_register(self):
2987 class A:
2988 def __init__(self, arg):
2989 self.arg = arg
2990
2991 @functools.singledispatchmethod
2992 @classmethod
2993 def t(cls, arg):
2994 return cls("base")
2995 @t.register
2996 @classmethod
2997 def _(cls, arg: int):
2998 return cls("int")
2999 @t.register
3000 @classmethod
3001 def _(cls, arg: str):
3002 return cls("str")
3003
3004 self.assertEqual(A.t(0).arg, "int")
3005 self.assertEqual(A.t('').arg, "str")
3006 self.assertEqual(A.t(0.0).arg, "base")
3007
3008 def test_method_wrapping_attributes(self):
3009 class A:
3010 @functools.singledispatchmethod
3011 def func(self, arg: int) -> str:
3012 """My function docstring"""
3013 return str(arg)
3014 @functools.singledispatchmethod
3015 @classmethod
3016 def cls_func(cls, arg: int) -> str:
3017 """My function docstring"""
3018 return str(arg)
3019 @functools.singledispatchmethod
3020 @staticmethod
3021 def static_func(arg: int) -> str:
3022 """My function docstring"""
3023 return str(arg)
3024
3025 prefix = A.__qualname__ + '.'
3026 for meth in (
3027 A.func,
3028 A().func,
3029 A.cls_func,
3030 A().cls_func,
3031 A.static_func,
3032 A().static_func
3033 ):
3034 with self.subTest(meth=meth):
3035 self.assertEqual(meth.__module__, __name__)
3036 self.assertEqual(type(meth).__module__, 'functools')
3037 self.assertEqual(meth.__qualname__, prefix + meth.__name__)
3038 self.assertEqual(meth.__doc__,
3039 ('My function docstring'
3040 if support.HAVE_PY_DOCSTRINGS
3041 else None))
3042 self.assertEqual(meth.__annotations__['arg'], int)
3043
3044 self.assertEqual(A.func.__name__, 'func')
3045 self.assertEqual(A().func.__name__, 'func')
3046 self.assertEqual(A.cls_func.__name__, 'cls_func')
3047 self.assertEqual(A().cls_func.__name__, 'cls_func')
3048 self.assertEqual(A.static_func.__name__, 'static_func')
3049 self.assertEqual(A().static_func.__name__, 'static_func')
3050
3051 def test_method_repr(self):
3052 class Callable:
3053 def __call__(self, *args):
3054 pass
3055
3056 class CallableWithName:
3057 __name__ = 'NOQUALNAME'
3058 def __call__(self, *args):
3059 pass
3060
3061 class A:
3062 @functools.singledispatchmethod
3063 def func(self, arg):
3064 pass
3065 @functools.singledispatchmethod
3066 @classmethod
3067 def cls_func(cls, arg):
3068 pass
3069 @functools.singledispatchmethod
3070 @staticmethod
3071 def static_func(arg):
3072 pass
3073 # No __qualname__, only __name__
3074 no_qualname = functools.singledispatchmethod(CallableWithName())
3075 # No __qualname__, no __name__
3076 no_name = functools.singledispatchmethod(Callable())
3077
3078 self.assertEqual(repr(A.__dict__['func']),
3079 f'<single dispatch method descriptor {A.__qualname__}.func>')
3080 self.assertEqual(repr(A.__dict__['cls_func']),
3081 f'<single dispatch method descriptor {A.__qualname__}.cls_func>')
3082 self.assertEqual(repr(A.__dict__['static_func']),
3083 f'<single dispatch method descriptor {A.__qualname__}.static_func>')
3084 self.assertEqual(repr(A.__dict__['no_qualname']),
3085 f'<single dispatch method descriptor NOQUALNAME>')
3086 self.assertEqual(repr(A.__dict__['no_name']),
3087 f'<single dispatch method descriptor ?>')
3088
3089 self.assertEqual(repr(A.func),
3090 f'<single dispatch method {A.__qualname__}.func>')
3091 self.assertEqual(repr(A.cls_func),
3092 f'<single dispatch method {A.__qualname__}.cls_func>')
3093 self.assertEqual(repr(A.static_func),
3094 f'<single dispatch method {A.__qualname__}.static_func>')
3095 self.assertEqual(repr(A.no_qualname),
3096 f'<single dispatch method NOQUALNAME>')
3097 self.assertEqual(repr(A.no_name),
3098 f'<single dispatch method ?>')
3099
3100 a = A()
3101 self.assertEqual(repr(a.func),
3102 f'<bound single dispatch method {A.__qualname__}.func of {a!r}>')
3103 self.assertEqual(repr(a.cls_func),
3104 f'<bound single dispatch method {A.__qualname__}.cls_func of {a!r}>')
3105 self.assertEqual(repr(a.static_func),
3106 f'<bound single dispatch method {A.__qualname__}.static_func of {a!r}>')
3107 self.assertEqual(repr(a.no_qualname),
3108 f'<bound single dispatch method NOQUALNAME of {a!r}>')
3109 self.assertEqual(repr(a.no_name),
3110 f'<bound single dispatch method ? of {a!r}>')
3111
3112 def test_double_wrapped_methods(self):
3113 def classmethod_friendly_decorator(func):
3114 wrapped = func.__func__
3115 @classmethod
3116 @functools.wraps(wrapped)
3117 def wrapper(*args, **kwargs):
3118 return wrapped(*args, **kwargs)
3119 return wrapper
3120
3121 class WithoutSingleDispatch:
3122 @classmethod
3123 @contextlib.contextmanager
3124 def cls_context_manager(cls, arg: int) -> str:
3125 try:
3126 yield str(arg)
3127 finally:
3128 pass
3129 return 'Done'
3130
3131 @classmethod_friendly_decorator
3132 @classmethod
3133 def decorated_classmethod(cls, arg: int) -> str:
3134 return str(arg)
3135
3136 class WithSingleDispatch:
3137 @functools.singledispatchmethod
3138 @classmethod
3139 @contextlib.contextmanager
3140 def cls_context_manager(cls, arg: int) -> str:
3141 """My function docstring"""
3142 try:
3143 yield str(arg)
3144 finally:
3145 pass
3146 return 'Done'
3147
3148 @functools.singledispatchmethod
3149 @classmethod_friendly_decorator
3150 @classmethod
3151 def decorated_classmethod(cls, arg: int) -> str:
3152 """My function docstring"""
3153 return str(arg)
3154
3155 # These are sanity checks
3156 # to test the test itself is working as expected
3157 with WithoutSingleDispatch.cls_context_manager(5) as foo:
3158 without_single_dispatch_foo = foo
3159
3160 with WithSingleDispatch.cls_context_manager(5) as foo:
3161 single_dispatch_foo = foo
3162
3163 self.assertEqual(without_single_dispatch_foo, single_dispatch_foo)
3164 self.assertEqual(single_dispatch_foo, '5')
3165
3166 self.assertEqual(
3167 WithoutSingleDispatch.decorated_classmethod(5),
3168 WithSingleDispatch.decorated_classmethod(5)
3169 )
3170
3171 self.assertEqual(WithSingleDispatch.decorated_classmethod(5), '5')
3172
3173 # Behavioural checks now follow
3174 for method_name in ('cls_context_manager', 'decorated_classmethod'):
3175 with self.subTest(method=method_name):
3176 self.assertEqual(
3177 getattr(WithSingleDispatch, method_name).__name__,
3178 getattr(WithoutSingleDispatch, method_name).__name__
3179 )
3180
3181 self.assertEqual(
3182 getattr(WithSingleDispatch(), method_name).__name__,
3183 getattr(WithoutSingleDispatch(), method_name).__name__
3184 )
3185
3186 for meth in (
3187 WithSingleDispatch.cls_context_manager,
3188 WithSingleDispatch().cls_context_manager,
3189 WithSingleDispatch.decorated_classmethod,
3190 WithSingleDispatch().decorated_classmethod
3191 ):
3192 with self.subTest(meth=meth):
3193 self.assertEqual(meth.__doc__,
3194 ('My function docstring'
3195 if support.HAVE_PY_DOCSTRINGS
3196 else None))
3197 self.assertEqual(meth.__annotations__['arg'], int)
3198
3199 self.assertEqual(
3200 WithSingleDispatch.cls_context_manager.__name__,
3201 'cls_context_manager'
3202 )
3203 self.assertEqual(
3204 WithSingleDispatch().cls_context_manager.__name__,
3205 'cls_context_manager'
3206 )
3207 self.assertEqual(
3208 WithSingleDispatch.decorated_classmethod.__name__,
3209 'decorated_classmethod'
3210 )
3211 self.assertEqual(
3212 WithSingleDispatch().decorated_classmethod.__name__,
3213 'decorated_classmethod'
3214 )
3215
3216 def test_invalid_registrations(self):
3217 msg_prefix = "Invalid first argument to `register()`: "
3218 msg_suffix = (
3219 ". Use either `@register(some_class)` or plain `@register` on an "
3220 "annotated function."
3221 )
3222 @functools.singledispatch
3223 def i(arg):
3224 return "base"
3225 with self.assertRaises(TypeError) as exc:
3226 @i.register(42)
3227 def _(arg):
3228 return "I annotated with a non-type"
3229 self.assertStartsWith(str(exc.exception), msg_prefix + "42")
3230 self.assertEndsWith(str(exc.exception), msg_suffix)
3231 with self.assertRaises(TypeError) as exc:
3232 @i.register
3233 def _(arg):
3234 return "I forgot to annotate"
3235 self.assertStartsWith(str(exc.exception), msg_prefix +
3236 "<function TestSingleDispatch.test_invalid_registrations.<locals>._"
3237 )
3238 self.assertEndsWith(str(exc.exception), msg_suffix)
3239
3240 with self.assertRaises(TypeError) as exc:
3241 @i.register
3242 def _(arg: typing.Iterable[str]):
3243 # At runtime, dispatching on generics is impossible.
3244 # When registering implementations with singledispatch, avoid
3245 # types from `typing`. Instead, annotate with regular types
3246 # or ABCs.
3247 return "I annotated with a generic collection"
3248 self.assertStartsWith(str(exc.exception),
3249 "Invalid annotation for 'arg'."
3250 )
3251 self.assertEndsWith(str(exc.exception),
3252 'typing.Iterable[str] is not a class.'
3253 )
3254
3255 with self.assertRaises(TypeError) as exc:
3256 @i.register
3257 def _(arg: typing.Union[int, typing.Iterable[str]]):
3258 return "Invalid Union"
3259 self.assertStartsWith(str(exc.exception),
3260 "Invalid annotation for 'arg'."
3261 )
3262 self.assertEndsWith(str(exc.exception),
3263 'int | typing.Iterable[str] not all arguments are classes.'
3264 )
3265
3266 def test_invalid_positional_argument(self):
3267 @functools.singledispatch
3268 def f(*args, **kwargs):
3269 pass
3270 msg = 'f requires at least 1 positional argument'
3271 with self.assertRaisesRegex(TypeError, msg):
3272 f()
3273 msg = 'f requires at least 1 positional argument'
3274 with self.assertRaisesRegex(TypeError, msg):
3275 f(a=1)
3276
3277 def test_invalid_positional_argument_singledispatchmethod(self):
3278 class A:
3279 @functools.singledispatchmethod
3280 def t(self, *args, **kwargs):
3281 pass
3282 msg = 't requires at least 1 positional argument'
3283 with self.assertRaisesRegex(TypeError, msg):
3284 A().t()
3285 msg = 't requires at least 1 positional argument'
3286 with self.assertRaisesRegex(TypeError, msg):
3287 A().t(a=1)
3288
3289 def test_union(self):
3290 @functools.singledispatch
3291 def f(arg):
3292 return "default"
3293
3294 @f.register
3295 def _(arg: typing.Union[str, bytes]):
3296 return "typing.Union"
3297
3298 @f.register
3299 def _(arg: int | float):
3300 return "types.UnionType"
3301
3302 self.assertEqual(f([]), "default")
3303 self.assertEqual(f(""), "typing.Union")
3304 self.assertEqual(f(b""), "typing.Union")
3305 self.assertEqual(f(1), "types.UnionType")
3306 self.assertEqual(f(1.0), "types.UnionType")
3307
3308 def test_union_conflict(self):
3309 @functools.singledispatch
3310 def f(arg):
3311 return "default"
3312
3313 @f.register
3314 def _(arg: typing.Union[str, bytes]):
3315 return "typing.Union"
3316
3317 @f.register
3318 def _(arg: int | str):
3319 return "types.UnionType"
3320
3321 self.assertEqual(f([]), "default")
3322 self.assertEqual(f(""), "types.UnionType") # last one wins
3323 self.assertEqual(f(b""), "typing.Union")
3324 self.assertEqual(f(1), "types.UnionType")
3325
3326 def test_union_None(self):
3327 @functools.singledispatch
3328 def typing_union(arg):
3329 return "default"
3330
3331 @typing_union.register
3332 def _(arg: typing.Union[str, None]):
3333 return "typing.Union"
3334
3335 self.assertEqual(typing_union(1), "default")
3336 self.assertEqual(typing_union(""), "typing.Union")
3337 self.assertEqual(typing_union(None), "typing.Union")
3338
3339 @functools.singledispatch
3340 def types_union(arg):
3341 return "default"
3342
3343 @types_union.register
3344 def _(arg: int | None):
3345 return "types.UnionType"
3346
3347 self.assertEqual(types_union(""), "default")
3348 self.assertEqual(types_union(1), "types.UnionType")
3349 self.assertEqual(types_union(None), "types.UnionType")
3350
3351 def test_register_genericalias(self):
3352 @functools.singledispatch
3353 def f(arg):
3354 return "default"
3355
3356 with self.assertRaisesRegex(TypeError, "Invalid first argument to "):
3357 f.register(list[int], lambda arg: "types.GenericAlias")
3358 with self.assertRaisesRegex(TypeError, "Invalid first argument to "):
3359 f.register(typing.List[int], lambda arg: "typing.GenericAlias")
3360 with self.assertRaisesRegex(TypeError, "Invalid first argument to "):
3361 f.register(list[int] | str, lambda arg: "types.UnionTypes(types.GenericAlias)")
3362 with self.assertRaisesRegex(TypeError, "Invalid first argument to "):
3363 f.register(typing.List[float] | bytes, lambda arg: "typing.Union[typing.GenericAlias]")
3364
3365 self.assertEqual(f([1]), "default")
3366 self.assertEqual(f([1.0]), "default")
3367 self.assertEqual(f(""), "default")
3368 self.assertEqual(f(b""), "default")
3369
3370 def test_register_genericalias_decorator(self):
3371 @functools.singledispatch
3372 def f(arg):
3373 return "default"
3374
3375 with self.assertRaisesRegex(TypeError, "Invalid first argument to "):
3376 f.register(list[int])
3377 with self.assertRaisesRegex(TypeError, "Invalid first argument to "):
3378 f.register(typing.List[int])
3379 with self.assertRaisesRegex(TypeError, "Invalid first argument to "):
3380 f.register(list[int] | str)
3381 with self.assertRaisesRegex(TypeError, "Invalid first argument to "):
3382 f.register(typing.List[int] | str)
3383
3384 def test_register_genericalias_annotation(self):
3385 @functools.singledispatch
3386 def f(arg):
3387 return "default"
3388
3389 with self.assertRaisesRegex(TypeError, "Invalid annotation for 'arg'"):
3390 @f.register
3391 def _(arg: list[int]):
3392 return "types.GenericAlias"
3393 with self.assertRaisesRegex(TypeError, "Invalid annotation for 'arg'"):
3394 @f.register
3395 def _(arg: typing.List[float]):
3396 return "typing.GenericAlias"
3397 with self.assertRaisesRegex(TypeError, "Invalid annotation for 'arg'"):
3398 @f.register
3399 def _(arg: list[int] | str):
3400 return "types.UnionType(types.GenericAlias)"
3401 with self.assertRaisesRegex(TypeError, "Invalid annotation for 'arg'"):
3402 @f.register
3403 def _(arg: typing.List[float] | bytes):
3404 return "typing.Union[typing.GenericAlias]"
3405
3406 self.assertEqual(f([1]), "default")
3407 self.assertEqual(f([1.0]), "default")
3408 self.assertEqual(f(""), "default")
3409 self.assertEqual(f(b""), "default")
3410
3411 def test_forward_reference(self):
3412 @functools.singledispatch
3413 def f(arg, arg2=None):
3414 return "default"
3415
3416 @f.register
3417 def _(arg: str, arg2: undefined = None):
3418 return "forward reference"
3419
3420 self.assertEqual(f(1), "default")
3421 self.assertEqual(f(""), "forward reference")
3422
3423 def test_unresolved_forward_reference(self):
3424 @functools.singledispatch
3425 def f(arg):
3426 return "default"
3427
3428 with self.assertRaisesRegex(TypeError, "is an unresolved forward reference"):
3429 @f.register
3430 def _(arg: undefined):
3431 return "forward reference"
3432
3433 def test_method_equal_instances(self):
3434 # gh-127750: Reference to self was cached
3435 class A:
3436 def __eq__(self, other):
3437 return True
3438 def __hash__(self):
3439 return 1
3440 @functools.singledispatchmethod
3441 def t(self, arg):
3442 return self
3443
3444 a = A()
3445 b = A()
3446 self.assertIs(a.t(1), a)
3447 self.assertIs(b.t(2), b)
3448
3449 def test_method_bad_hash(self):
3450 class A:
3451 def __eq__(self, other):
3452 raise AssertionError
3453 def __hash__(self):
3454 raise AssertionError
3455 @functools.singledispatchmethod
3456 def t(self, arg):
3457 pass
3458
3459 # Should not raise
3460 A().t(1)
3461 hash(A().t)
3462 A().t == A().t
3463
3464 def test_method_no_reference_loops(self):
3465 # gh-127750: Created a strong reference to self
3466 class A:
3467 @functools.singledispatchmethod
3468 def t(self, arg):
3469 return weakref.ref(self)
3470
3471 a = A()
3472 r = a.t(1)
3473 self.assertIsNotNone(r())
3474 del a # delete a after a.t
3475 if not support.check_impl_detail(cpython=True):
3476 support.gc_collect()
3477 self.assertIsNone(r())
3478
3479 a = A()
3480 t = a.t
3481 del a # delete a before a.t
3482 support.gc_collect()
3483 r = t(1)
3484 self.assertIsNotNone(r())
3485 del t
3486 if not support.check_impl_detail(cpython=True):
3487 support.gc_collect()
3488 self.assertIsNone(r())
3489
3490 def test_signatures(self):
3491 @functools.singledispatch
3492 def func(item, arg: int) -> str:
3493 return str(item)
3494 @func.register
3495 def _(item: int, arg: bytes) -> str:
3496 return str(item)
3497
3498 self.assertEqual(str(Signature.from_callable(func)),
3499 '(item, arg: int) -> str')
3500
3501 def test_method_signatures(self):
3502 class A:
3503 @functools.singledispatchmethod
3504 def func(self, item, arg: int) -> str:
3505 return str(item)
3506 @func.register
3507 def _(self, item: int, arg: bytes) -> str:
3508 return str(item)
3509
3510 @functools.singledispatchmethod
3511 @classmethod
3512 def cls_func(cls, item, arg: int) -> str:
3513 return str(arg)
3514 @func.register
3515 @classmethod
3516 def _(cls, item: int, arg: bytes) -> str:
3517 return str(item)
3518
3519 @functools.singledispatchmethod
3520 @staticmethod
3521 def static_func(item, arg: int) -> str:
3522 return str(arg)
3523 @func.register
3524 @staticmethod
3525 def _(item: int, arg: bytes) -> str:
3526 return str(item)
3527
3528 self.assertEqual(str(Signature.from_callable(A.func)),
3529 '(self, item, arg: int) -> str')
3530 self.assertEqual(str(Signature.from_callable(A().func)),
3531 '(self, item, arg: int) -> str')
3532 self.assertEqual(str(Signature.from_callable(A.cls_func)),
3533 '(cls, item, arg: int) -> str')
3534 self.assertEqual(str(Signature.from_callable(A.static_func)),
3535 '(item, arg: int) -> str')
3536
3537
3538 class CachedCostItem:
3539 _cost = 1
3540
3541 def __init__(self):
3542 self.lock = py_functools.RLock()
3543
3544 @py_functools.cached_property
3545 def cost(self):
3546 """The cost of the item."""
3547 with self.lock:
3548 self._cost += 1
3549 return self._cost
3550
3551
3552 class OptionallyCachedCostItem:
3553 _cost = 1
3554
3555 def get_cost(self):
3556 """The cost of the item."""
3557 self._cost += 1
3558 return self._cost
3559
3560 cached_cost = py_functools.cached_property(get_cost)
3561
3562
3563 class CachedCostItemWithSlots:
3564 __slots__ = ('_cost')
3565
3566 def __init__(self):
3567 self._cost = 1
3568
3569 @py_functools.cached_property
3570 def cost(self):
3571 raise RuntimeError('never called, slots not supported')
3572
3573
3574 class TestCachedProperty(unittest.TestCase):
3575 def test_cached(self):
3576 item = CachedCostItem()
3577 self.assertEqual(item.cost, 2)
3578 self.assertEqual(item.cost, 2) # not 3
3579
3580 def test_cached_attribute_name_differs_from_func_name(self):
3581 item = OptionallyCachedCostItem()
3582 self.assertEqual(item.get_cost(), 2)
3583 self.assertEqual(item.cached_cost, 3)
3584 self.assertEqual(item.get_cost(), 4)
3585 self.assertEqual(item.cached_cost, 3)
3586
3587 def test_object_with_slots(self):
3588 item = CachedCostItemWithSlots()
3589 with self.assertRaisesRegex(
3590 TypeError,
3591 "No '__dict__' attribute on 'CachedCostItemWithSlots' instance to cache 'cost' property.",
3592 ):
3593 item.cost
3594
3595 def test_immutable_dict(self):
3596 class MyMeta(type):
3597 @py_functools.cached_property
3598 def prop(self):
3599 return True
3600
3601 class MyClass(metaclass=MyMeta):
3602 pass
3603
3604 with self.assertRaisesRegex(
3605 TypeError,
3606 "The '__dict__' attribute on 'MyMeta' instance does not support item assignment for caching 'prop' property.",
3607 ):
3608 MyClass.prop
3609
3610 def test_reuse_different_names(self):
3611 """Disallow this case because decorated function a would not be cached."""
3612 with self.assertRaises(TypeError) as ctx:
3613 class ReusedCachedProperty:
3614 @py_functools.cached_property
3615 def a(self):
3616 pass
3617
3618 b = a
3619
3620 self.assertEqual(
3621 str(ctx.exception),
3622 str(TypeError("Cannot assign the same cached_property to two different names ('a' and 'b')."))
3623 )
3624
3625 def test_reuse_same_name(self):
3626 """Reusing a cached_property on different classes under the same name is OK."""
3627 counter = 0
3628
3629 @py_functools.cached_property
3630 def _cp(_self):
3631 nonlocal counter
3632 counter += 1
3633 return counter
3634
3635 class A:
3636 cp = _cp
3637
3638 class B:
3639 cp = _cp
3640
3641 a = A()
3642 b = B()
3643
3644 self.assertEqual(a.cp, 1)
3645 self.assertEqual(b.cp, 2)
3646 self.assertEqual(a.cp, 1)
3647
3648 def test_set_name_not_called(self):
3649 cp = py_functools.cached_property(lambda s: None)
3650 class Foo:
3651 pass
3652
3653 Foo.cp = cp
3654
3655 with self.assertRaisesRegex(
3656 TypeError,
3657 "Cannot use cached_property instance without calling __set_name__ on it.",
3658 ):
3659 Foo().cp
3660
3661 def test_access_from_class(self):
3662 self.assertIsInstance(CachedCostItem.cost, py_functools.cached_property)
3663
3664 def test_doc(self):
3665 self.assertEqual(CachedCostItem.cost.__doc__,
3666 ("The cost of the item."
3667 if support.HAVE_PY_DOCSTRINGS
3668 else None))
3669
3670 def test_module(self):
3671 self.assertEqual(CachedCostItem.cost.__module__, CachedCostItem.__module__)
3672
3673 def test_subclass_with___set__(self):
3674 """Caching still works for a subclass defining __set__."""
3675 class readonly_cached_property(py_functools.cached_property):
3676 def __set__(self, obj, value):
3677 raise AttributeError("read only property")
3678
3679 class Test:
3680 def __init__(self, prop):
3681 self._prop = prop
3682
3683 @readonly_cached_property
3684 def prop(self):
3685 return self._prop
3686
3687 t = Test(1)
3688 self.assertEqual(t.prop, 1)
3689 t._prop = 999
3690 self.assertEqual(t.prop, 1)
3691
3692
3693 if __name__ == '__main__':
3694 unittest.main()