mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 05:31:20 +00:00 
			
		
		
		
	bpo-44806: Fix __init__ in subclasses of protocols (GH-27545) (GH-27559)
Non-protocol subclasses of protocol ignore now the __init__ method
inherited from protocol base classes.
(cherry picked from commit 043cd60abe)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
			
			
This commit is contained in:
		
							parent
							
								
									4817c14395
								
							
						
					
					
						commit
						0f6a7739df
					
				
					 3 changed files with 48 additions and 2 deletions
				
			
		|  | @ -1080,8 +1080,7 @@ def _is_callable_members_only(cls): | |||
| 
 | ||||
| 
 | ||||
| def _no_init(self, *args, **kwargs): | ||||
|     if type(self)._is_protocol: | ||||
|         raise TypeError('Protocols cannot be instantiated') | ||||
|     raise TypeError('Protocols cannot be instantiated') | ||||
| 
 | ||||
| 
 | ||||
| def _allow_reckless_class_cheks(): | ||||
|  | @ -1210,6 +1209,15 @@ def _proto_hook(other): | |||
| 
 | ||||
|         # We have nothing more to do for non-protocols... | ||||
|         if not cls._is_protocol: | ||||
|             if cls.__init__ == _no_init: | ||||
|                 for base in cls.__mro__: | ||||
|                     init = base.__dict__.get('__init__', _no_init) | ||||
|                     if init != _no_init: | ||||
|                         cls.__init__ = init | ||||
|                         break | ||||
|                 else: | ||||
|                     # should not happen | ||||
|                     cls.__init__ = object.__init__ | ||||
|             return | ||||
| 
 | ||||
|         # ... otherwise check consistency of bases, and prohibit instantiation. | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Miss Islington (bot)
						Miss Islington (bot)