Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
split up and improve test
  • Loading branch information
adriangb committed Mar 2, 2022
commit 1fe020aa4636ddfb466d072b21041407af0341e5
23 changes: 15 additions & 8 deletions Lib/test/test_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1074,24 +1074,31 @@ class CG(PG[T]): pass
with self.assertRaises(TypeError):
CG[int](42)

def test_protocol_defining_init(self):
def test_protocol_defining_init_does_not_get_overriden(self):
Comment thread
adriangb marked this conversation as resolved.
Outdated
# 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

Comment thread
JelleZijlstra marked this conversation as resolved.
Outdated
class C: pass

# the protocol itself cannot be instantiated
with self.assertRaises(TypeError):
P()
c = C()
P.__init__(c, 1)
self.assertEqual(c.x, 1)

def test_concrete_class_inherting_init_from_protocol(self):
Comment thread
adriangb marked this conversation as resolved.
Outdated
class P(Protocol):
x: int
def __init__(self, x: int) -> None:
self.x = x

# but a concrete subclass can
class C(P): pass

c = C(1)
self.assertIsInstance(c, C)

# and this concrete subclass can inherit the __init__
# implementation from Protocol
self.assertEqual(c.x, 1)

def test_cannot_instantiate_abstract(self):
Expand Down