diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2021-09-03 06:26:53 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-03 06:26:53 (GMT) |
commit | 79e9f5a58427c73dc546cb571819d50defe2e14f (patch) | |
tree | 5e78d616aa27df0beedc8beb6386400c8a1151c4 /Lib/test | |
parent | 7aa58f5425189c95927d1620df8d6b0ba57d393a (diff) | |
download | cpython-79e9f5a58427c73dc546cb571819d50defe2e14f.zip cpython-79e9f5a58427c73dc546cb571819d50defe2e14f.tar.gz cpython-79e9f5a58427c73dc546cb571819d50defe2e14f.tar.bz2 |
bpo-45081: Fix __init__ method generation when inheriting from Protocol (GH-28121)
Co-authored-by: Ken Jin <28750310+Fidget-Spinner@users.noreply.github.com>
(cherry picked from commit 0635e201beaf52373f776ff32702795e38f43ae3)
Co-authored-by: Yurii Karabas <1998uriyyo@gmail.com>
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_dataclasses.py | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/Lib/test/test_dataclasses.py b/Lib/test/test_dataclasses.py index 8e645ae..33c9fcd 100644 --- a/Lib/test/test_dataclasses.py +++ b/Lib/test/test_dataclasses.py @@ -10,7 +10,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 @@ -2150,6 +2150,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): |