Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Lib/test/test_unittest/testmock/testhelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,7 @@ def __post_init__(self):
create_autospec(WithPostInit()),
]:
with self.subTest(mock=mock):
self.assertIsInstance(mock, WithPostInit)
self.assertIsInstance(mock.a, int)
self.assertIsInstance(mock.b, int)

Expand All @@ -1072,6 +1073,7 @@ class WithDefault:
create_autospec(WithDefault(1)),
]:
with self.subTest(mock=mock):
self.assertIsInstance(mock, WithDefault)
self.assertIsInstance(mock.a, int)
self.assertIsInstance(mock.b, int)

Expand All @@ -1087,6 +1089,7 @@ def b(self) -> int:
create_autospec(WithMethod(1)),
]:
with self.subTest(mock=mock):
self.assertIsInstance(mock, WithMethod)
self.assertIsInstance(mock.a, int)
mock.b.assert_not_called()

Expand All @@ -1102,11 +1105,27 @@ class WithNonFields:
create_autospec(WithNonFields(1)),
]:
with self.subTest(mock=mock):
self.assertIsInstance(mock, WithNonFields)
with self.assertRaisesRegex(AttributeError, msg):
mock.a
with self.assertRaisesRegex(AttributeError, msg):
mock.b

def test_dataclass_special_attrs(self):
@dataclass
class Description:
name: str

for mock in [
create_autospec(Description, instance=True),
create_autospec(Description(1)),
]:
with self.subTest(mock=mock):
self.assertIsInstance(mock, Description)
self.assertIs(mock.__class__, Description)
self.assertIsInstance(mock.__dataclass_fields__, MagicMock)
self.assertIsInstance(mock.__dataclass_params__, MagicMock)

class TestCallList(unittest.TestCase):

def test_args_list_contains_call_list(self):
Expand Down
8 changes: 7 additions & 1 deletion Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -2771,9 +2771,13 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,
if is_type and instance and is_dataclass(spec):
dataclass_fields = fields(spec)
entries.extend((f.name, f.type) for f in dataclass_fields)
_kwargs = {'spec': [f.name for f in dataclass_fields]}
spec_list = [f.name for f in dataclass_fields]
spec_list.extend(['__dataclass_fields__', '__dataclass_params__'])
_kwargs = {'spec': spec_list} # we set `__class__` further
is_dataclass_spec = True
else:
_kwargs = {'spec': spec}
is_dataclass_spec = False

if spec_set:
_kwargs = {'spec_set': spec}
Expand Down Expand Up @@ -2810,6 +2814,8 @@ def create_autospec(spec, spec_set=False, instance=False, _parent=None,

mock = Klass(parent=_parent, _new_parent=_parent, _new_name=_new_name,
name=_name, **_kwargs)
if is_dataclass_spec:
mock.__class__ = spec # we need this for `isinstance` to work

if isinstance(spec, FunctionTypes):
# should only happen at the top level because we don't
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :class:`unittest.mock.Mock` generation on :func:`dataclasses.dataclass`
objects. Now all special attributes are set as it was before :gh:`124429`.
Loading