Skip to content

Commit 97c1adf

Browse files
author
Guido van Rossum
committed
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.
1 parent 0a6996d commit 97c1adf

15 files changed

Lines changed: 300 additions & 62 deletions

β€ŽDoc/library/collections.abc.rstβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ ABC Inherits from Abstract Methods Mixin
114114

115115
.. class:: Reversible
116116

117-
ABC for classes that provide the :meth:`__reversed__` method.
117+
ABC for iterable classes that also provide the :meth:`__reversed__`
118+
method.
118119

119120
.. versionadded:: 3.6
120121

β€ŽDoc/reference/datamodel.rstβ€Ž

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,12 @@ to ``type(x).__getitem__(x, i)``. Except where mentioned, attempts to execute a
10631063
operation raise an exception when no appropriate method is defined (typically
10641064
:exc:`AttributeError` or :exc:`TypeError`).
10651065

1066+
Setting a special method to ``None`` indicates that the corresponding
1067+
operation is not available. For example, if a class sets
1068+
:meth:`__iter__` to ``None``, the class is not iterable, so calling
1069+
:func:`iter` on its instances will raise a :exc:`TypeError` (without
1070+
falling back to :meth:`__getitem__`). [#]_
1071+
10661072
When implementing a class that emulates any built-in type, it is important that
10671073
the emulation only be implemented to the degree that it makes sense for the
10681074
object being modelled. For example, some sequences may work well with retrieval
@@ -2113,7 +2119,7 @@ left undefined.
21132119
(``+``, ``-``, ``*``, ``@``, ``/``, ``//``, ``%``, :func:`divmod`,
21142120
:func:`pow`, ``**``, ``<<``, ``>>``, ``&``, ``^``, ``|``) with reflected
21152121
(swapped) operands. These functions are only called if the left operand does
2116-
not support the corresponding operation and the operands are of different
2122+
not support the corresponding operation [#]_ and the operands are of different
21172123
types. [#]_ For instance, to evaluate the expression ``x - y``, where *y* is
21182124
an instance of a class that has an :meth:`__rsub__` method, ``y.__rsub__(x)``
21192125
is called if ``x.__sub__(y)`` returns *NotImplemented*.
@@ -2529,6 +2535,17 @@ An example of an asynchronous context manager class::
25292535
controlled conditions. It generally isn't a good idea though, since it can
25302536
lead to some very strange behaviour if it is handled incorrectly.
25312537
2538+
.. [#] The :meth:`__hash__`, :meth:`__iter__`, :meth:`__reversed__`, and
2539+
:meth:`__contains__` methods have special handling for this; others
2540+
will still raise a :exc:`TypeError`, but may do so by relying on
2541+
the behavior that ``None`` is not callable.
2542+
2543+
.. [#] "Does not support" here means that the class has no such method, or
2544+
the method returns ``NotImplemented``. Do not set the method to
2545+
``None`` if you want to force fallback to the right operand's reflected
2546+
method--that will instead have the opposite effect of explicitly
2547+
*blocking* such fallback.
2548+
25322549
.. [#] For operands of the same type, it is assumed that if the non-reflected method
25332550
(such as :meth:`__add__`) fails the operation is not supported, which is why the
25342551
reflected method is not called.

β€ŽLib/_collections_abc.pyβ€Ž

Lines changed: 29 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@ async def _coro(): pass
6262

6363
### ONE-TRICK PONIES ###
6464

65+
def _check_methods(C, *methods):
66+
mro = C.__mro__
67+
for method in methods:
68+
for B in mro:
69+
if method in B.__dict__:
70+
if B.__dict__[method] is None:
71+
return NotImplemented
72+
break
73+
else:
74+
return NotImplemented
75+
return True
76+
6577
class Hashable(metaclass=ABCMeta):
6678

6779
__slots__ = ()
@@ -73,11 +85,7 @@ def __hash__(self):
7385
@classmethod
7486
def __subclasshook__(cls, C):
7587
if cls is Hashable:
76-
for B in C.__mro__:
77-
if "__hash__" in B.__dict__:
78-
if B.__dict__["__hash__"]:
79-
return True
80-
break
88+
return _check_methods(C, "__hash__")
8189
return NotImplemented
8290

8391

@@ -92,11 +100,7 @@ def __await__(self):
92100
@classmethod
93101
def __subclasshook__(cls, C):
94102
if cls is Awaitable:
95-
for B in C.__mro__:
96-
if "__await__" in B.__dict__:
97-
if B.__dict__["__await__"]:
98-
return True
99-
break
103+
return _check_methods(C, "__await__")
100104
return NotImplemented
101105

102106

@@ -137,14 +141,7 @@ def close(self):
137141
@classmethod
138142
def __subclasshook__(cls, C):
139143
if cls is Coroutine:
140-
mro = C.__mro__
141-
for method in ('__await__', 'send', 'throw', 'close'):
142-
for base in mro:
143-
if method in base.__dict__:
144-
break
145-
else:
146-
return NotImplemented
147-
return True
144+
return _check_methods(C, '__await__', 'send', 'throw', 'close')
148145
return NotImplemented
149146

150147

@@ -162,8 +159,7 @@ def __aiter__(self):
162159
@classmethod
163160
def __subclasshook__(cls, C):
164161
if cls is AsyncIterable:
165-
if any("__aiter__" in B.__dict__ for B in C.__mro__):
166-
return True
162+
return _check_methods(C, "__aiter__")
167163
return NotImplemented
168164

169165

@@ -182,9 +178,7 @@ def __aiter__(self):
182178
@classmethod
183179
def __subclasshook__(cls, C):
184180
if cls is AsyncIterator:
185-
if (any("__anext__" in B.__dict__ for B in C.__mro__) and
186-
any("__aiter__" in B.__dict__ for B in C.__mro__)):
187-
return True
181+
return _check_methods(C, "__anext__", "__aiter__")
188182
return NotImplemented
189183

190184

@@ -200,8 +194,7 @@ def __iter__(self):
200194
@classmethod
201195
def __subclasshook__(cls, C):
202196
if cls is Iterable:
203-
if any("__iter__" in B.__dict__ for B in C.__mro__):
204-
return True
197+
return _check_methods(C, "__iter__")
205198
return NotImplemented
206199

207200

@@ -220,9 +213,7 @@ def __iter__(self):
220213
@classmethod
221214
def __subclasshook__(cls, C):
222215
if cls is Iterator:
223-
if (any("__next__" in B.__dict__ for B in C.__mro__) and
224-
any("__iter__" in B.__dict__ for B in C.__mro__)):
225-
return True
216+
return _check_methods(C, '__iter__', '__next__')
226217
return NotImplemented
227218

228219
Iterator.register(bytes_iterator)
@@ -246,16 +237,13 @@ class Reversible(Iterable):
246237

247238
@abstractmethod
248239
def __reversed__(self):
249-
return NotImplemented
240+
while False:
241+
yield None
250242

251243
@classmethod
252244
def __subclasshook__(cls, C):
253245
if cls is Reversible:
254-
for B in C.__mro__:
255-
if "__reversed__" in B.__dict__:
256-
if B.__dict__["__reversed__"] is not None:
257-
return True
258-
break
246+
return _check_methods(C, "__reversed__", "__iter__")
259247
return NotImplemented
260248

261249

@@ -302,17 +290,10 @@ def close(self):
302290
@classmethod
303291
def __subclasshook__(cls, C):
304292
if cls is Generator:
305-
mro = C.__mro__
306-
for method in ('__iter__', '__next__', 'send', 'throw', 'close'):
307-
for base in mro:
308-
if method in base.__dict__:
309-
break
310-
else:
311-
return NotImplemented
312-
return True
293+
return _check_methods(C, '__iter__', '__next__',
294+
'send', 'throw', 'close')
313295
return NotImplemented
314296

315-
316297
Generator.register(generator)
317298

318299

@@ -327,8 +308,7 @@ def __len__(self):
327308
@classmethod
328309
def __subclasshook__(cls, C):
329310
if cls is Sized:
330-
if any("__len__" in B.__dict__ for B in C.__mro__):
331-
return True
311+
return _check_methods(C, "__len__")
332312
return NotImplemented
333313

334314

@@ -343,8 +323,7 @@ def __contains__(self, x):
343323
@classmethod
344324
def __subclasshook__(cls, C):
345325
if cls is Container:
346-
if any("__contains__" in B.__dict__ for B in C.__mro__):
347-
return True
326+
return _check_methods(C, "__contains__")
348327
return NotImplemented
349328

350329

@@ -359,8 +338,7 @@ def __call__(self, *args, **kwds):
359338
@classmethod
360339
def __subclasshook__(cls, C):
361340
if cls is Callable:
362-
if any("__call__" in B.__dict__ for B in C.__mro__):
363-
return True
341+
return _check_methods(C, "__call__")
364342
return NotImplemented
365343

366344

@@ -640,6 +618,8 @@ def __eq__(self, other):
640618
return NotImplemented
641619
return dict(self.items()) == dict(other.items())
642620

621+
__reversed__ = None
622+
643623
Mapping.register(mappingproxy)
644624

645625

β€ŽLib/test/test_augassign.pyβ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ class aug_test3(aug_test):
8383
def __iadd__(self, val):
8484
return aug_test3(self.val + val)
8585

86+
class aug_test4(aug_test3):
87+
"""Blocks inheritance, and fallback to __add__"""
88+
__iadd__ = None
89+
8690
x = aug_test(1)
8791
y = x
8892
x += 10
@@ -106,6 +110,10 @@ def __iadd__(self, val):
106110
self.assertTrue(y is not x)
107111
self.assertEqual(x.val, 13)
108112

113+
x = aug_test4(4)
114+
with self.assertRaises(TypeError):
115+
x += 10
116+
109117

110118
def testCustomMethods2(test_self):
111119
output = []

β€ŽLib/test/test_binop.pyβ€Ž

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import unittest
44
from test import support
5-
from operator import eq, le
5+
from operator import eq, le, ne
66
from abc import ABCMeta
77

88
def gcd(a, b):
@@ -388,6 +388,54 @@ def test_comparison_orders(self):
388388
self.assertEqual(op_sequence(eq, B, V), ['B.__eq__', 'V.__eq__'])
389389
self.assertEqual(op_sequence(le, B, V), ['B.__le__', 'V.__ge__'])
390390

391+
class SupEq(object):
392+
"""Class that can test equality"""
393+
def __eq__(self, other):
394+
return True
395+
396+
class S(SupEq):
397+
"""Subclass of SupEq that should fail"""
398+
__eq__ = None
399+
400+
class F(object):
401+
"""Independent class that should fall back"""
402+
403+
class X(object):
404+
"""Independent class that should fail"""
405+
__eq__ = None
406+
407+
class SN(SupEq):
408+
"""Subclass of SupEq that can test equality, but not non-equality"""
409+
__ne__ = None
410+
411+
class XN:
412+
"""Independent class that can test equality, but not non-equality"""
413+
def __eq__(self, other):
414+
return True
415+
__ne__ = None
416+
417+
class FallbackBlockingTests(unittest.TestCase):
418+
"""Unit tests for None method blocking"""
419+
420+
def test_fallback_rmethod_blocking(self):
421+
e, f, s, x = SupEq(), F(), S(), X()
422+
self.assertEqual(e, e)
423+
self.assertEqual(e, f)
424+
self.assertEqual(f, e)
425+
# left operand is checked first
426+
self.assertEqual(e, x)
427+
self.assertRaises(TypeError, eq, x, e)
428+
# S is a subclass, so it's always checked first
429+
self.assertRaises(TypeError, eq, e, s)
430+
self.assertRaises(TypeError, eq, s, e)
431+
432+
def test_fallback_ne_blocking(self):
433+
e, sn, xn = SupEq(), SN(), XN()
434+
self.assertFalse(e != e)
435+
self.assertRaises(TypeError, ne, e, sn)
436+
self.assertRaises(TypeError, ne, sn, e)
437+
self.assertFalse(e != xn)
438+
self.assertRaises(TypeError, ne, xn, e)
391439

392440
if __name__ == "__main__":
393441
unittest.main()

β€ŽLib/test/test_bool.pyβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,17 @@ def __len__(self):
333333
except (Exception) as e_len:
334334
self.assertEqual(str(e_bool), str(e_len))
335335

336+
def test_blocked(self):
337+
class A:
338+
__bool__ = None
339+
self.assertRaises(TypeError, bool, A())
340+
341+
class B:
342+
def __len__(self):
343+
return 10
344+
__bool__ = None
345+
self.assertRaises(TypeError, bool, B())
346+
336347
def test_real_and_imag(self):
337348
self.assertEqual(True.real, 1)
338349
self.assertEqual(True.imag, 0)

β€ŽLib/test/test_bytes.pyβ€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,36 @@ def ptr_formatter(ptr):
843843
self.assertRaises(OverflowError,
844844
PyBytes_FromFormat, b'%c', c_int(256))
845845

846+
def test_bytes_blocking(self):
847+
class IterationBlocked(list):
848+
__bytes__ = None
849+
i = [0, 1, 2, 3]
850+
self.assertEqual(bytes(i), b'\x00\x01\x02\x03')
851+
self.assertRaises(TypeError, bytes, IterationBlocked(i))
852+
853+
# At least in CPython, because bytes.__new__ and the C API
854+
# PyBytes_FromObject have different fallback rules, integer
855+
# fallback is handled specially, so test separately.
856+
class IntBlocked(int):
857+
__bytes__ = None
858+
self.assertEqual(bytes(3), b'\0\0\0')
859+
self.assertRaises(TypeError, bytes, IntBlocked(3))
860+
861+
# While there is no separately-defined rule for handling bytes
862+
# subclasses differently from other buffer-interface classes,
863+
# an implementation may well special-case them (as CPython 2.x
864+
# str did), so test them separately.
865+
class BytesSubclassBlocked(bytes):
866+
__bytes__ = None
867+
self.assertEqual(bytes(b'ab'), b'ab')
868+
self.assertRaises(TypeError, bytes, BytesSubclassBlocked(b'ab'))
869+
870+
class BufferBlocked(bytearray):
871+
__bytes__ = None
872+
ba, bb = bytearray(b'ab'), BufferBlocked(b'ab')
873+
self.assertEqual(bytes(ba), b'ab')
874+
self.assertRaises(TypeError, bytes, bb)
875+
846876

847877
class ByteArrayTest(BaseBytesTest, unittest.TestCase):
848878
type2test = bytearray

0 commit comments

Comments
Β (0)