[TIP] failing tests after upgrading Mock 0.7 to 0.8

Michael Foord michael at voidspace.org.uk
Tue Nov 20 15:31:25 PST 2012

On 20 Nov 2012, at 21:47, Michael <mock at webhippo.net> wrote:

> I would like to respectfully answer this:
> This is a super un-useful answer.
> 1. Yes it worked in 0.7.2
> 2. No the docs did not mention the __init__/super was call was required. For all we knew it was not required.
> 3. The docs also do not mention like all kinds of things, like that one should step on one foot while using mock ... that does not mean we should do them.

Sorry for the late reply, I've only just seen this thread. The short answer is that overriding __init__ in a Mock subclass did *not* work (except perhaps for a very limited set of use-cases) in 0.7.2:

>>> import mock as m
>>> m.__version__
>>> class M(m.Mock):
...      def __init__(self): pass
>>> a = M()
>>> a.foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/compile/virtualenvs/old-mock/lib/python2.7/site-packages/mock.py", line 355, in __getattr__
    elif self._methods is not None:
  File "/compile/virtualenvs/old-mock/lib/python2.7/site-packages/mock.py", line 354, in __getattr__
    raise AttributeError(name)
AttributeError: _methods

An uninitialised Mock instance doesn't work. In general subclassing and overriding an initialiser, without calling up to the base class(es), is a bug in your code.

All the best,

Michael Foord

> Also this does not actually answer the question as to which change introduced this? (specifically in Changelog)
>> Did Mock's documentation ever tell you not to call the super init?
>> Generally speaking this is something subclasses need to do in Python, and
>> not doing it is a latent bug.
>> -m
>> On 20 November 2012 11:53, Eric Rasmussen <ericrasmussen at gmail.com> wrote:
>>> Hello,
>>> I am upgrading from Mock 0.7 and it looks like there was a backwards
>>> incompatible change in 0.8: it's apparently no longer possible to create a
>>> class inheriting from Mock unless you call the super method. Can someone
>>> explain why the below works in 0.7 but not in 0.8 and above?
>>> class MyMock(Mock):
>>>    def __init__(self, name):
>>>        self.name = name
>>> Now I have to write:
>>> class MyMock(Mock):
>>>    def __init__(self, name):
>>>        Mock.__init__(self)
>>>        self.name = name
>>> I did find an issue here:
>>> http://code.google.com/p/mock/issues/detail?id=161
>>> But it does not mention the reason for the change. I was unable to find
>>> any mention of a backwards incompatible change of this nature in the
>>> changelog, which is concerning. I just want to make sure I fully understand
>>> the scope of the change. If you can point me to any documentation or pull
>>> requests that show when and why this change was made that would be an
>>> enormous help.
>>> It's really important for a project I'm working on that we document this
>>> new requirement for creating a subclass of Mock, so I appreciate any
>>> insight you can offer.
>>> Thank you,
>>> Eric
>>> _______________________________________________
>>> testing-in-python mailing list
>>> testing-in-python at lists.idyll.org
>>> http://lists.idyll.org/listinfo/testing-in-python
>> --
>> Martin
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20121121/0dc24a9d/attachment.html>
> _______________________________________________
> testing-in-python mailing list
> testing-in-python at lists.idyll.org
> http://lists.idyll.org/listinfo/testing-in-python


May you do good and not evil
May you find forgiveness for yourself and forgive others
May you share freely, never taking more than you give.
-- the sqlite blessing 

More information about the testing-in-python mailing list