Export to GitHub

mock - issue #221

Fix __delattr__ of mock to allow multiple creation and deletion of attributes.


Posted on Jan 13, 2014 by Swift Rhino

What steps will reproduce the problem?

>>> a = Mock() >>> a.this = ‘that’ >>> del a.this >>> a.this = ‘that’

>>> del a.this Traceback (most recent call last): ... AttributeError: this

What is the expected output? What do you see instead? I would expect it to happily delete the attribute this. Instead it raises and attribute error.

What version of the product are you using? On what operating system? mock-1.0.1-py2.7 Red Hat Enterprise Linux Server release 5.5

Please provide any additional information below.

suggested change:

Previous:

def __delattr__(self, name):
    if name in _all_magics and name in type(self).__dict__:
        delattr(type(self), name)
        if name not in self.__dict__:
            # for magic methods that are still MagicProxy objects and
            # not set on the instance itself
            return

    if name in self.__dict__:
        object.__delattr__(self, name)

    obj = self._mock_children.get(name, _missing)
    if obj is _deleted:
        raise AttributeError(name)
    if obj is not _missing:
        del self._mock_children[name]
    self._mock_children[name] = _deleted

Change:

def __delattr__(self, name):
    if name in _all_magics and name in type(self).__dict__:
        delattr(type(self), name)
        if name not in self.__dict__:
            # for magic methods that are still MagicProxy objects and
            # not set on the instance itself
            return

    obj = self._mock_children.get(name, _missing)
    if name in self.__dict__:
        object.__delattr__(self, name)
    elif obj is _deleted:
        raise AttributeError(name)
    if obj is not _missing:
        del self._mock_children[name]
    self._mock_children[name] = _deleted

Incidentally the if ‘obj is not _missing’ line seems superfluous.

Comment #1

Posted on Jan 13, 2014 by Helpful Monkey

Thanks for the report. I've posted it on the Python bug tracker (as mock is in the Python standard library as well) as http://bugs.python.org/issue20239

Status: New

Labels:
Type-Defect Priority-High