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

Method test_subclass_right_op

Lib/test/test_descr.py:4544–4593  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

4542 C.__name__ = "normal"
4543
4544 def test_subclass_right_op(self):
4545 # Testing correct dispatch of subclass overloading __r<op>__...
4546
4547 # This code tests various cases where right-dispatch of a subclass
4548 # should be preferred over left-dispatch of a base class.
4549
4550 # Case 1: subclass of int; this tests code in abstract.c::binary_op1()
4551
4552 class B(int):
4553 def __floordiv__(self, other):
4554 return "B.__floordiv__"
4555 def __rfloordiv__(self, other):
4556 return "B.__rfloordiv__"
4557
4558 self.assertEqual(B(1) // 1, "B.__floordiv__")
4559 self.assertEqual(1 // B(1), "B.__rfloordiv__")
4560
4561 # Case 2: subclass of object; this is just the baseline for case 3
4562
4563 class C(object):
4564 def __floordiv__(self, other):
4565 return "C.__floordiv__"
4566 def __rfloordiv__(self, other):
4567 return "C.__rfloordiv__"
4568
4569 self.assertEqual(C() // 1, "C.__floordiv__")
4570 self.assertEqual(1 // C(), "C.__rfloordiv__")
4571
4572 # Case 3: subclass of new-style class; here it gets interesting
4573
4574 class D(C):
4575 def __floordiv__(self, other):
4576 return "D.__floordiv__"
4577 def __rfloordiv__(self, other):
4578 return "D.__rfloordiv__"
4579
4580 self.assertEqual(D() // C(), "D.__floordiv__")
4581 self.assertEqual(C() // D(), "D.__rfloordiv__")
4582
4583 # Case 4: this didn't work right in 2.2.2 and 2.3a1
4584
4585 class E(C):
4586 pass
4587
4588 self.assertEqual(E.__rfloordiv__, C.__rfloordiv__)
4589
4590 self.assertEqual(E() // 1, "C.__floordiv__")
4591 self.assertEqual(1 // E(), "C.__rfloordiv__")
4592 self.assertEqual(E() // C(), "C.__floordiv__")
4593 self.assertEqual(C() // E(), "C.__floordiv__") # This one would fail
4594
4595 @support.impl_detail("testing an internal kind of method object")
4596 def test_meth_class_get(self):

Callers

nothing calls this directly

Calls 5

BClass · 0.70
CClass · 0.70
DClass · 0.70
EClass · 0.70
assertEqualMethod · 0.45

Tested by

no test coverage detected