summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_typing.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2021-08-02 16:23:22 (GMT)
committerGitHub <noreply@github.com>2021-08-02 16:23:22 (GMT)
commit043cd60abed09edddc7185bcf7d039771acc734d (patch)
treee37e68fdb15b6546d8d37e6bded2e775d57684f1 /Lib/test/test_typing.py
parent36d952d228582b0ffc7a86c520d4ddbe8943d803 (diff)
downloadcpython-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/test/test_typing.py')
-rw-r--r--Lib/test/test_typing.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/Lib/test/test_typing.py b/Lib/test/test_typing.py
index 6cd83a1..1a172a0 100644
--- a/Lib/test/test_typing.py
+++ b/Lib/test/test_typing.py
@@ -881,6 +881,9 @@ class ProtocolTests(BaseTestCase):
class C(P): pass
self.assertIsInstance(C(), C)
+ with self.assertRaises(TypeError):
+ C(42)
+
T = TypeVar('T')
class PG(Protocol[T]): pass
@@ -895,6 +898,8 @@ class ProtocolTests(BaseTestCase):
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
@@ -1322,6 +1327,37 @@ class ProtocolTests(BaseTestCase):
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')