FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-44806: Fix issue with typing.Protocol __init__ method generation by uriyyo · Pull Request #27541 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
28 changes: 28 additions & 0 deletions Lib/test/test_typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,34 @@ class P(Protocol):
with self.assertRaisesRegex(TypeError, "@runtime_checkable"):
isinstance(1, P)

def test_protocol_init_method(self): # see bpo-44806
class P(Protocol):
x: int

class D1(P):
pass

class D2(P):
def __init__(self, x):
self.x = x

class B:
def __init__(self, x):
self.x = x

class D3(P, B):
pass

with self.subTest(cls=D1):
with self.assertRaisesRegex(TypeError, r"D1\(\) takes no arguments"):
D1(x=1)

for cls in (D2, D3):
with self.subTest(cls=cls):
d = cls(x=1)

self.assertIsInstance(d, cls)
self.assertEqual(d.x, 1)

class GenericTests(BaseTestCase):

Expand Down
6 changes: 6 additions & 0 deletions Lib/typing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1522,6 +1522,12 @@ def _proto_hook(other):

# We have nothing more to do for non-protocols...
if not cls._is_protocol:
if '__init__' not in cls.__dict__:
# bpo-44806: reset __init__ method to correct one
cls.__init__ = next(
(b.__init__ for b in cls.__bases__ if b.__init__ is not _no_init),
object.__init__,
)
return

# ... otherwise check consistency of bases, and prohibit instantiation.
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix issue when ``typing.Protocol`` delivered class get ``__init__`` method
that can accepts unlimited `*args` and `**kwargs`. Patch provided by Yurii
Karabas.

Back | FazBrowse Home | New Git URL