@@ -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+
6577class 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
228219Iterator .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-
316297Generator .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+
643623Mapping .register (mappingproxy )
644624
645625
0 commit comments