[New-bugs-announce] [issue36580] unittest.mock does not understand dataclasses

John Parejko report at bugs.python.org
Tue Apr 9 17:08:33 EDT 2019


New submission from John Parejko <parejkoj at uw.edu>:

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 <report at bugs.python.org>
<https://bugs.python.org/issue36580>
_______________________________________


More information about the New-bugs-announce mailing list