MCPcopy Index your code
hub / github.com/python/cpython / test_post_init

Method test_post_init

Lib/test/test_dataclasses/__init__.py:5142–5166  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

5140
5141
5142 def test_post_init(self):
5143 @dataclass
5144 class A:
5145 a: int
5146 _: KW_ONLY
5147 b: InitVar[int]
5148 c: int
5149 d: InitVar[int]
5150 def __post_init__(self, b, d):
5151 raise CustomError(f'{b=} {d=}')
5152 with self.assertRaisesRegex(CustomError, 'b=3 d=4'):
5153 A(1, c=2, b=3, d=4)
5154
5155 @dataclass
5156 class B:
5157 a: int
5158 _: KW_ONLY
5159 b: InitVar[int]
5160 c: int
5161 d: InitVar[int]
5162 def __post_init__(self, b, d):
5163 self.a = b
5164 self.c = d
5165 b = B(1, c=2, b=3, d=4)
5166 self.assertEqual(asdict(b), {'a': 3, 'c': 4})
5167
5168 def test_defaults(self):
5169 # For kwargs, make sure we can have defaults after non-defaults.

Callers

nothing calls this directly

Calls 5

asdictFunction · 0.85
assertRaisesRegexMethod · 0.80
AClass · 0.70
BClass · 0.70
assertEqualMethod · 0.45

Tested by

no test coverage detected