summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorAdrian Garcia Badaracco <1755071+adriangb@users.noreply.github.com>2022-04-11 14:51:25 (GMT)
committerGitHub <noreply@github.com>2022-04-11 14:51:25 (GMT)
commit5f2abae61ec69264b835dcabe2cdabe57b9a990e (patch)
tree4c58c52c703a1f52e97ec402d8274620ee2a2352 /Lib/test
parentb0b836b20cb56c225874a4a39ef895f89ab2970f (diff)
downloadcpython-5f2abae61ec69264b835dcabe2cdabe57b9a990e.zip
cpython-5f2abae61ec69264b835dcabe2cdabe57b9a990e.tar.gz
cpython-5f2abae61ec69264b835dcabe2cdabe57b9a990e.tar.bz2
bpo-44807: Allow Protocol classes to define __init__ (GH-31628)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_typing.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index e09f8aa..b884f7b 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -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):