Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow repeated deletion of unittest.mock.Mock attributes #64438

Closed
voidspace opened this issue Jan 13, 2014 · 4 comments
Closed

Allow repeated deletion of unittest.mock.Mock attributes #64438

voidspace opened this issue Jan 13, 2014 · 4 comments
Assignees
Labels
3.7 (EOL) end of life 3.8 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@voidspace
Copy link
Contributor

BPO 20239
Nosy @cjw296, @voidspace, @mariocj89, @tirkarthi
PRs
  • bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes #11057
  • [3.7] bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes (GH-11057) #11629
  • [3.7] bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes (GH-11057) #11629
  • [3.7] bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes (GH-11057) #11629
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/voidspace'
    closed_at = <Date 2019-01-21.11:10:03.538>
    created_at = <Date 2014-01-13.11:15:01.944>
    labels = ['3.8', 'type-bug', '3.7']
    title = 'Allow repeated deletion of unittest.mock.Mock attributes'
    updated_at = <Date 2019-01-21.11:10:03.538>
    user = 'https://github.com/voidspace'

    bugs.python.org fields:

    activity = <Date 2019-01-21.11:10:03.538>
    actor = 'pablogsal'
    assignee = 'michael.foord'
    closed = True
    closed_date = <Date 2019-01-21.11:10:03.538>
    closer = 'pablogsal'
    components = []
    creation = <Date 2014-01-13.11:15:01.944>
    creator = 'michael.foord'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 20239
    keywords = ['patch']
    message_count = 4.0
    messages = ['208019', '331434', '334114', '334117']
    nosy_count = 4.0
    nosy_names = ['cjw296', 'michael.foord', 'mariocj89', 'xtreak']
    pr_nums = ['11057', '11629', '11629', '11629']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue20239'
    versions = ['Python 3.7', 'Python 3.8']

    @voidspace
    Copy link
    Contributor Author

    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.

    @voidspace voidspace self-assigned this Jan 13, 2014
    @voidspace voidspace added the type-bug An unexpected behavior, bug, or error label Jan 13, 2014
    @tirkarthi
    Copy link
    Member

    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.

    @tirkarthi tirkarthi added 3.7 (EOL) end of life 3.8 only security fixes labels Dec 9, 2018
    @cjw296
    Copy link
    Contributor

    cjw296 commented Jan 21, 2019

    New changeset 222d303 by Chris Withers (Pablo Galindo) in branch 'master':
    bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes (bpo-11057)
    222d303

    @cjw296
    Copy link
    Contributor

    cjw296 commented Jan 21, 2019

    New changeset d358a8c by Chris Withers (Miss Islington (bot)) in branch '3.7':
    bpo-20239: Allow repeated deletion of unittest.mock.Mock attributes (GH-11629)
    d358a8c

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life 3.8 only security fixes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    4 participants