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

Method test_two_fields_one_default

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

Source from the content-addressed store, hash-verified

131 self.assertEqual(o.x, 32)
132
133 def test_two_fields_one_default(self):
134 @dataclass
135 class C:
136 x: int
137 y: int = 0
138
139 o = C(3)
140 self.assertEqual((o.x, o.y), (3, 0))
141
142 # Non-defaults following defaults.
143 with self.assertRaisesRegex(TypeError,
144 "non-default argument 'y' follows "
145 "default argument 'x'"):
146 @dataclass
147 class C:
148 x: int = 0
149 y: int
150
151 # A derived class adds a non-default field after a default one.
152 with self.assertRaisesRegex(TypeError,
153 "non-default argument 'y' follows "
154 "default argument 'x'"):
155 @dataclass
156 class B:
157 x: int = 0
158
159 @dataclass
160 class C(B):
161 y: int
162
163 # Override a base class field and add a default to
164 # a field which didn't use to have a default.
165 with self.assertRaisesRegex(TypeError,
166 "non-default argument 'y' follows "
167 "default argument 'x'"):
168 @dataclass
169 class B:
170 x: int
171 y: int
172
173 @dataclass
174 class C(B):
175 x: int = 0
176
177 def test_overwrite_hash(self):
178 # Test that declaring this class isn't an error. It should

Callers

nothing calls this directly

Calls 3

assertEqualMethod · 0.95
assertRaisesRegexMethod · 0.95
CClass · 0.70

Tested by

no test coverage detected