diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2021-08-02 16:23:22 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-02 16:23:22 (GMT) |
commit | 043cd60abed09edddc7185bcf7d039771acc734d (patch) | |
tree | e37e68fdb15b6546d8d37e6bded2e775d57684f1 /Lib/typing.py | |
parent | 36d952d228582b0ffc7a86c520d4ddbe8943d803 (diff) | |
download | cpython-043cd60abed09edddc7185bcf7d039771acc734d.zip cpython-043cd60abed09edddc7185bcf7d039771acc734d.tar.gz cpython-043cd60abed09edddc7185bcf7d039771acc734d.tar.bz2 |
bpo-44806: Fix __init__ in subclasses of protocols (GH-27545)
Non-protocol subclasses of protocol ignore now the __init__ method
inherited from protocol base classes.
Diffstat (limited to 'Lib/typing.py')
-rw-r--r-- | Lib/typing.py | 12 |
1 files changed, 10 insertions, 2 deletions
diff --git a/Lib/typing.py b/Lib/typing.py index 16ad5ce..7a12d31 100644 --- a/Lib/typing.py +++ b/Lib/typing.py @@ -1381,8 +1381,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 _caller(depth=1, default='__main__'): try: @@ -1522,6 +1521,15 @@ class Protocol(Generic, metaclass=_ProtocolMeta): # 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. |