mirror of
https://github.com/python/cpython.git
synced 2025-08-31 14:07:50 +00:00
bpo-44807: Allow Protocol classes to define __init__ (GH-31628)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
b0b836b20c
commit
5f2abae61e
3 changed files with 29 additions and 1 deletions
|
@ -1598,6 +1598,32 @@ class ProtocolTests(BaseTestCase):
|
|||
with self.assertRaises(TypeError):
|
||||
CG[int](42)
|
||||
|
||||
def test_protocol_defining_init_does_not_get_overridden(self):
|
||||
# check that P.__init__ doesn't get clobbered
|
||||
# see https://bugs.python.org/issue44807
|
||||
|
||||
class P(Protocol):
|
||||
x: int
|
||||
def __init__(self, x: int) -> None:
|
||||
self.x = x
|
||||
class C: pass
|
||||
|
||||
c = C()
|
||||
P.__init__(c, 1)
|
||||
self.assertEqual(c.x, 1)
|
||||
|
||||
def test_concrete_class_inheriting_init_from_protocol(self):
|
||||
class P(Protocol):
|
||||
x: int
|
||||
def __init__(self, x: int) -> None:
|
||||
self.x = x
|
||||
|
||||
class C(P): pass
|
||||
|
||||
c = C(1)
|
||||
self.assertIsInstance(c, C)
|
||||
self.assertEqual(c.x, 1)
|
||||
|
||||
def test_cannot_instantiate_abstract(self):
|
||||
@runtime_checkable
|
||||
class P(Protocol):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue