FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-44806: Fix __init__ in subclasses of protocols by serhiy-storchaka · Pull Request #27545 · python/cpython · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
36 changes: 36 additions & 0 deletions Lib/test/test_typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,9 @@ class P(Protocol): pass
class C(P): pass

self.assertIsInstance(C(), C)
with self.assertRaises(TypeError):
C(42)

T = TypeVar('T')

class PG(Protocol[T]): pass
Expand All @@ -895,6 +898,8 @@ class PG(Protocol[T]): pass
class CG(PG[T]): pass

self.assertIsInstance(CG[int](), CG)
with self.assertRaises(TypeError):
CG[int](42)

def test_cannot_instantiate_abstract(self):
@runtime_checkable
Expand Down Expand Up @@ -1322,6 +1327,37 @@ def __init__(self):

self.assertEqual(C[int]().test, 'OK')

class B:
def __init__(self):
self.test = 'OK'

class D1(B, P[T]):
pass

self.assertEqual(D1[int]().test, 'OK')

class D2(P[T], B):
pass

self.assertEqual(D2[int]().test, 'OK')

def test_new_called(self):
T = TypeVar('T')

class P(Protocol[T]): pass

class C(P[T]):
def __new__(cls, *args):
self = super().__new__(cls, *args)
self.test = 'OK'
return self

self.assertEqual(C[int]().test, 'OK')
with self.assertRaises(TypeError):
C[int](42)
with self.assertRaises(TypeError):
C[int](a=42)

def test_protocols_bad_subscripts(self):
T = TypeVar('T')
S = TypeVar('S')
Expand Down
12 changes: 10 additions & 2 deletions Lib/typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -1522,6 +1521,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.
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Non-protocol subclasses of :class:`typing.Protocol` ignore now the
``__init__`` method inherited from protocol base classes.

Back | FazBrowse Home | New Git URL