MCPcopy Index your code
hub / github.com/python/cpython / test__format__lookup

Method test__format__lookup

Lib/test/test_fstring.py:39–64  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

37 eval(str)
38
39 def test__format__lookup(self):
40 # Make sure __format__ is looked up on the type, not the instance.
41 class X:
42 def __format__(self, spec):
43 return 'class'
44
45 x = X()
46
47 # Add a bound __format__ method to the 'y' instance, but not
48 # the 'x' instance.
49 y = X()
50 y.__format__ = types.MethodType(lambda self, spec: 'instance', y)
51
52 self.assertEqual(f'{y}', format(y))
53 self.assertEqual(f'{y}', 'class')
54 self.assertEqual(format(x), format(y))
55
56 # __format__ is not called this way, but still make sure it
57 # returns what we expect (so we can make sure we're bypassing
58 # it).
59 self.assertEqual(x.__format__(''), 'class')
60 self.assertEqual(y.__format__(''), 'instance')
61
62 # This is how __format__ is actually called.
63 self.assertEqual(type(x).__format__(x, ''), 'class')
64 self.assertEqual(type(y).__format__(y, ''), 'class')
65
66 def test_ast(self):
67 # Inspired by http://bugs.python.org/issue24975

Callers

nothing calls this directly

Calls 4

assertEqualMethod · 0.95
__format__Method · 0.95
formatFunction · 0.85
XClass · 0.70

Tested by

no test coverage detected