I'm trying to port our namedtuple classes into dataclass in Python 3.6 using the backport package. However, I noticed when mocking dataclass classes, you cannot use the "spec" keyword anymore. I assume it's because the dataclass code is auto generated.
from dataclasses import dataclass
import mock
@dataclass
class A:
aaa: str
bbb: int
m = mock.Mock(spec=A)
m.aaa
And this is the error I get:
AttributeError: Mock object has no attribute 'aaa'
Any idea if there's any way to automatically set all the attributes from original object to the mock object? I have lots of data classes with a lot of data. It's going to be really tedious if I try to manually set the values one by one.
dataclass
. – Anlagedataclass
. You just don't have any class variables here. (aaa: str
is an annotation, not a class variable, and the dataclass processing doesn't create anaaa
class variable.) – Anlage