New submission from Florian Brucker <python-b...@florianbrucker.de>:

The "parent" attribute of unittest.mock.Mock is either broken or undocumented.

For example, on Python 3.7.4:

>>> from unittest.mock import Mock
>>> m = Mock(x=1, parent=2)
>>> m.x
1
>>> m.parent
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/unittest/mock.py", line 659, in __repr__
    name = self._extract_mock_name()
  File "/usr/local/lib/python3.7/unittest/mock.py", line 638, in 
_extract_mock_name
    _name_list.append(_parent._mock_new_name + dot)
AttributeError: 'int' object has no attribute '_mock_new_name'
>>> parent = Mock()
>>> child = Mock(parent=parent)
>>> child.parent is parent
False

I stumbled upon this while trying to mock an object that has a "parent" 
attribute.

>From the documentation I understand that mocks have built-in parents. However, 
>the documentation never mentions the "parent" attribute specifically, so I 
>always assumed that the built-in parent-child relationship was handled using 
>private or name-mangled attributes. And since the "parent" attribute is not 
>mentioned in the docs, I assumed I could set it by passing an additional kwarg 
>to Mock.

I would have expected one of the following, in order of personal preference:

a) That a private or name-mangled attribute is used for the built-in 
parent-child relationship, so that I can mock objects which themselves have a 
"parent" attribute

b) That the special meaning of the "parent" attribute is documented, and that 
trying to set it directly (via the constructor or via attribute assignment, and 
without going through attach_mock) triggers a warning.

----------
assignee: docs@python
components: Documentation, Library (Lib)
messages: 359348
nosy: docs@python, florian.brucker
priority: normal
severity: normal
status: open
title: unittest.mock.Mock.parent is broken or undocumented
type: behavior
versions: Python 3.6, Python 3.7

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue39222>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to