diff options
author | Guido van Rossum <guido@dropbox.com> | 2016-08-18 09:22:23 -0700 |
---|---|---|
committer | Guido van Rossum <guido@dropbox.com> | 2016-08-18 09:22:23 -0700 |
commit | 97c1adf3935234da716d3289b85f72dcd67e90c2 (patch) | |
tree | 0af6f9f258cf26ee9e59db463cc89d04c45bc0b8 /Lib/test/test_binop.py | |
parent | 0a6996d87d19a524c2a11dd315d96d12083c47d4 (diff) | |
download | cpython-97c1adf3935234da716d3289b85f72dcd67e90c2.tar.gz cpython-97c1adf3935234da716d3289b85f72dcd67e90c2.zip |
Anti-registration of various ABC methods.
- Issue #25958: Support "anti-registration" of special methods from
various ABCs, like __hash__, __iter__ or __len__. All these (and
several more) can be set to None in an implementation class and the
behavior will be as if the method is not defined at all.
(Previously, this mechanism existed only for __hash__, to make
mutable classes unhashable.) Code contributed by Andrew Barnert and
Ivan Levkivskyi.
Diffstat (limited to 'Lib/test/test_binop.py')
-rw-r--r-- | Lib/test/test_binop.py | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/Lib/test/test_binop.py b/Lib/test/test_binop.py index fc8d30fac1f..3ed018e0895 100644 --- a/Lib/test/test_binop.py +++ b/Lib/test/test_binop.py @@ -2,7 +2,7 @@ import unittest from test import support -from operator import eq, le +from operator import eq, le, ne from abc import ABCMeta def gcd(a, b): @@ -388,6 +388,54 @@ class OperationOrderTests(unittest.TestCase): self.assertEqual(op_sequence(eq, B, V), ['B.__eq__', 'V.__eq__']) self.assertEqual(op_sequence(le, B, V), ['B.__le__', 'V.__ge__']) +class SupEq(object): + """Class that can test equality""" + def __eq__(self, other): + return True + +class S(SupEq): + """Subclass of SupEq that should fail""" + __eq__ = None + +class F(object): + """Independent class that should fall back""" + +class X(object): + """Independent class that should fail""" + __eq__ = None + +class SN(SupEq): + """Subclass of SupEq that can test equality, but not non-equality""" + __ne__ = None + +class XN: + """Independent class that can test equality, but not non-equality""" + def __eq__(self, other): + return True + __ne__ = None + +class FallbackBlockingTests(unittest.TestCase): + """Unit tests for None method blocking""" + + def test_fallback_rmethod_blocking(self): + e, f, s, x = SupEq(), F(), S(), X() + self.assertEqual(e, e) + self.assertEqual(e, f) + self.assertEqual(f, e) + # left operand is checked first + self.assertEqual(e, x) + self.assertRaises(TypeError, eq, x, e) + # S is a subclass, so it's always checked first + self.assertRaises(TypeError, eq, e, s) + self.assertRaises(TypeError, eq, s, e) + + def test_fallback_ne_blocking(self): + e, sn, xn = SupEq(), SN(), XN() + self.assertFalse(e != e) + self.assertRaises(TypeError, ne, e, sn) + self.assertRaises(TypeError, ne, sn, e) + self.assertFalse(e != xn) + self.assertRaises(TypeError, ne, xn, e) if __name__ == "__main__": unittest.main() |