Karthikeyan Singaravelan added the comment:
Closing this as not a bug since autospeccing with create_autospec can be used
and spec only does attribute access validation.
Thanks
--
resolution: -> not a bug
stage: -> resolved
status: open -> closed
__
Karthikeyan Singaravelan added the comment:
Below is a even more simpler reproducer without dataclasses. 'name' is not
listed as a class attribute in dir(Person) since it's not defined with a value
but 'age' is with zero. Python seems to not to list something not defined with
a value in decl
Karthikeyan Singaravelan added the comment:
To add to this mock.Mock also copies dir(spec) but creating an instance from
mock doesn't copy it where it's not a problem with create_autospec. Mock with
spec does only attribute validation whereas create_autospec does signature
validation. There
Karthikeyan Singaravelan added the comment:
mock.Mock doesn't do signature validation by default for constructor and
methods. This is expected. create_autospec [0] should be used to make sure the
signature is validated.'
import dataclasses
import unittest.mock
@dataclasses.dataclass
class Fo
Eric V. Smith added the comment:
I'm not sure why dataclasses would be different here:
>>> import dataclasses
>>> import unittest.mock
>>> @dataclasses.dataclass
... class Foo:
... name: str
... baz: float
... bar: int = 12
...
>>> import inspect
>>> inspect.signature(Foo)
None>
>
New submission from John Parejko :
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.