File tree Expand file tree Collapse file tree 3 files changed +29
-1
lines changed Expand file tree Collapse file tree 3 files changed +29
-1
lines changed Original file line number Diff line number Diff line change @@ -1598,6 +1598,32 @@ class CG(PG[T]): pass
1598
1598
with self .assertRaises (TypeError ):
1599
1599
CG [int ](42 )
1600
1600
1601
+ def test_protocol_defining_init_does_not_get_overridden (self ):
1602
+ # check that P.__init__ doesn't get clobbered
1603
+ # see https://bugs.python.org/issue44807
1604
+
1605
+ class P (Protocol ):
1606
+ x : int
1607
+ def __init__ (self , x : int ) -> None :
1608
+ self .x = x
1609
+ class C : pass
1610
+
1611
+ c = C ()
1612
+ P .__init__ (c , 1 )
1613
+ self .assertEqual (c .x , 1 )
1614
+
1615
+ def test_concrete_class_inheriting_init_from_protocol (self ):
1616
+ class P (Protocol ):
1617
+ x : int
1618
+ def __init__ (self , x : int ) -> None :
1619
+ self .x = x
1620
+
1621
+ class C (P ): pass
1622
+
1623
+ c = C (1 )
1624
+ self .assertIsInstance (c , C )
1625
+ self .assertEqual (c .x , 1 )
1626
+
1601
1627
def test_cannot_instantiate_abstract (self ):
1602
1628
@runtime_checkable
1603
1629
class P (Protocol ):
Original file line number Diff line number Diff line change @@ -1997,7 +1997,8 @@ def _proto_hook(other):
1997
1997
issubclass (base , Generic ) and base ._is_protocol ):
1998
1998
raise TypeError ('Protocols can only inherit from other'
1999
1999
' protocols, got %r' % base )
2000
- cls .__init__ = _no_init_or_replace_init
2000
+ if cls .__init__ is Protocol .__init__ :
2001
+ cls .__init__ = _no_init_or_replace_init
2001
2002
2002
2003
2003
2004
class _AnnotatedAlias (_GenericAlias , _root = True ):
Original file line number Diff line number Diff line change
1
+ :class: `typing.Protocol ` no longer silently replaces :meth: `__init__ ` methods defined on subclasses. Patch by Adrian Garcia Badaracco.
You can’t perform that action at this time.
0 commit comments