-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
Expand file tree
/
Copy pathtest_dataclass.py
More file actions
26 lines (21 loc) · 758 Bytes
/
test_dataclass.py
File metadata and controls
26 lines (21 loc) · 758 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import unittest
from dataclasses import dataclass
class TestDataclassInheritanceFieldOrder(unittest.TestCase):
def test_required_after_optional_in_subclass(self):
@dataclass
class Base:
x: int = 10 # optional field with default
@dataclass
class Sub(Base):
y: int # required field in subclass
# Should require y but allow optional x to be omitted
obj = Sub(y=5)
self.assertEqual(obj.x, 10)
self.assertEqual(obj.y, 5)
# Should allow overriding x
obj2 = Sub(x=42, y=9)
self.assertEqual(obj2.x, 42)
self.assertEqual(obj2.y, 9)
# Missing y should raise TypeError
with self.assertRaises(TypeError):
Sub()