This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Allow repeated deletion of unittest.mock.Mock attributes
Type: behavior Stage: resolved
Components: Versions: Python 3.8, Python 3.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: michael.foord Nosy List: cjw296, mariocj89, michael.foord, xtreak
Priority: normal Keywords: patch

Created on 2014-01-13 11:15 by michael.foord, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 11057 merged pablogsal, 2018-12-09 21:36
PR 11629 merged miss-islington, 2019-01-21 08:58
PR 11629 merged miss-islington, 2019-01-21 08:58
PR 11629 merged miss-islington, 2019-01-21 08:58
Messages (4)
msg208019 - (view) Author: Michael Foord (michael.foord) * (Python committer) Date: 2014-01-13 11:15
Reported as mock issue 221: http://code.google.com/p/mock/issues/detail?id=221

>>> from unittest.mock import Mock
>>> m = Mock()
>>> m.foo = 3
>>> del m.foo
>>> m.foo = 4
>>> del m.foo
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/compile/py3k-cpython/Lib/unittest/mock.py", line 687, in __delattr__
    raise AttributeError(name)
AttributeError: foo

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.
msg331434 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2018-12-09 14:17
I find this to be a reasonable behavior as with normal objects that support setting the attribute after deletion. It also seems to be an easy issue since @michael.foord has already attached the patch for this from the original report. The patch also also causes no test failure on master and a unit test can be added for the behavior once confirmed.
msg334114 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2019-01-21 08:57
New changeset 222d303ade8aadf0adcae5190fac603bdcafe3f0 by Chris Withers (Pablo Galindo) in branch 'master':
bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes (#11057)
https://github.com/python/cpython/commit/222d303ade8aadf0adcae5190fac603bdcafe3f0
msg334117 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2019-01-21 09:37
New changeset d358a8cda75446a8e0b5d99149f709395d5eae19 by Chris Withers (Miss Islington (bot)) in branch '3.7':
bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes (GH-11629)
https://github.com/python/cpython/commit/d358a8cda75446a8e0b5d99149f709395d5eae19
History
Date User Action Args
2022-04-11 14:57:56adminsetgithub: 64438
2019-01-21 11:10:03pablogsalsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-01-21 09:37:58cjw296setmessages: + msg334117
2019-01-21 08:58:32miss-islingtonsetpull_requests: + pull_request11397
2019-01-21 08:58:27miss-islingtonsetpull_requests: + pull_request11396
2019-01-21 08:58:19miss-islingtonsetpull_requests: + pull_request11395
2019-01-21 08:57:58cjw296setmessages: + msg334114
2018-12-09 21:36:55pablogsalsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request10291
2018-12-09 14:17:58xtreaksetnosy: + cjw296, mariocj89, xtreak

messages: + msg331434
versions: + Python 3.7, Python 3.8, - Python 3.3, Python 3.4
2014-01-13 11:15:01michael.foordcreate