New submission from John Parejko <[email protected]>:
The new dataclasses.dataclass is very useful for describing the properties of a
class, but it appears that Mocks of such decorated classes do not catch the
members that are defined in the dataclass. I believe the root cause of this is
the fact that unittest.mock.Mock generates the attributes of its spec object
via `dir`, and the non-defaulted dataclass attributes do not appear in dir.
Given the utility in building classes with dataclass, it would be very useful
if Mocks could see the class attributes of the dataclass.
Example code:
import dataclasses
import unittest.mock
@dataclasses.dataclass
class Foo:
name: str
baz: float
bar: int = 12
FooMock = unittest.mock.Mock(Foo)
fooMock = FooMock() # should fail: Foo.__init__ takes two arguments
# I would expect these to be True, but they are False
'name' in dir(fooMock)
'baz' in dir(fooMock)
'bar' in dir(fooMock)
----------
components: Library (Lib), Tests
messages: 339808
nosy: John Parejko2
priority: normal
severity: normal
status: open
title: unittest.mock does not understand dataclasses
type: behavior
versions: Python 3.7
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue36580>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com