mirror of
https://github.com/python/cpython.git
synced 2025-07-28 13:44:43 +00:00
bpo-45081: Fix __init__ method generation when inheriting from Protocol (GH-28121) (GH-28132)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
(cherry picked from commit 0635e201be
)
Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
This commit is contained in:
parent
ca27109c17
commit
98eb40828a
3 changed files with 47 additions and 13 deletions
|
@ -9,7 +9,7 @@ import inspect
|
|||
import builtins
|
||||
import unittest
|
||||
from unittest.mock import Mock
|
||||
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional
|
||||
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol
|
||||
from typing import get_type_hints
|
||||
from collections import deque, OrderedDict, namedtuple
|
||||
from functools import total_ordering
|
||||
|
@ -2124,6 +2124,26 @@ class TestInit(unittest.TestCase):
|
|||
self.x = 2 * x
|
||||
self.assertEqual(C(5).x, 10)
|
||||
|
||||
def test_inherit_from_protocol(self):
|
||||
# Dataclasses inheriting from protocol should preserve their own `__init__`.
|
||||
# See bpo-45081.
|
||||
|
||||
class P(Protocol):
|
||||
a: int
|
||||
|
||||
@dataclass
|
||||
class C(P):
|
||||
a: int
|
||||
|
||||
self.assertEqual(C(5).a, 5)
|
||||
|
||||
@dataclass
|
||||
class D(P):
|
||||
def __init__(self, a):
|
||||
self.a = a * 2
|
||||
|
||||
self.assertEqual(D(5).a, 10)
|
||||
|
||||
|
||||
class TestRepr(unittest.TestCase):
|
||||
def test_repr(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue