testing-cabal / mock

The Python mock library

Home Page:https://docs.python.org/dev/library/unittest.mock.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

spec_set/autospec/spec seems to not be reading attributes defined in class body

efagerberg opened this issue · comments

Hello, I really like that this library allows for really strict mocking however one thing I have noticed is that it seems like using spec on a mock does not properly read the class body for attributes like some of the documentation claims. For example this is a snippet of the Logger class in python 3.6's logging module

class Logger(Filterer):
    name: str
    level: int
    parent: Union[Logger, PlaceHolder]
    propagate: bool
    handlers: List[Handler]
    disabled: int

Now I want to mock that class ensuring that propagate gets set to False for example

from unittest import mock
from logging import Logger

logger = mock.Mock(spec_set=Logger)
setFalse(logger)
assert logger.propagate is False
*** AttributeError: Mock object has no attribute 'propagate'

I have noticed this does work when the value is initialized in the class body so for example

class Logger(Filterer):
    name: str
    level: int
    parent: Union[Logger, PlaceHolder]
    propagate: bool = False
    handlers: List[Handler]
    disabled: int

This would not fail with the test in question.

Wondering if this is intended behavior or not or if I am misunderstanding something.

This package is a rolling backport of unittest.mock.
As such, any problems you encounter most likely need to be fixed upstream.

Please can you try and reproduce the problem on the latest release of Python 3, including alphas, and replace any import from mock with ones from unittest.mock.

If the issue still occurs, then please report upstream through https://bugs.python.org/ as it will need to be fixed there so that it can be backported here and released to you.

If not, reply with what you find out and we can re-open this issue.

Thanks will do