Message 339808 - Python tracker

Message339808

Author John Parejko2
Recipients John Parejko2
Date 2019-04-09.21:08:33
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1554844113.82.0.464270307904.issue36580@roundup.psfhosted.org>
In-reply-to
Content
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)
History
Date User Action Args
2019-04-09 21:08:33John Parejko2setrecipients: + John Parejko2
2019-04-09 21:08:33John Parejko2setmessageid: <1554844113.82.0.464270307904.issue36580@roundup.psfhosted.org>
2019-04-09 21:08:33John Parejko2linkissue36580 messages
2019-04-09 21:08:33John Parejko2create