[issue41403] Uncaught AttributeError in unittest.mock._get_target

webisteme report at bugs.python.org
Sun Jul 26 15:15:44 EDT 2020


New submission from webisteme <egothill at gmail.com>:

When calling `mock.patch` incorrectly, as in the following example, an uncaught error is thrown:

```shell
>>> from unittest import mock
>>> class Foo:
...     pass
... 
>>> mock.patch(Foo())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1624, in patch
    getter, attribute = _get_target(target)
  File "/usr/local/Cellar/python/3.7.6_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/unittest/mock.py", line 1469, in _get_target
    target, attribute = target.rsplit('.', 1)
AttributeError: 'Foo' object has no attribute 'rsplit'
```
This can happen when confusing `mock.patch` with `mock.patch.object`. However, the uncaught error is not informative, as it does not indicate that the wrong type of object was passed to `mock.patch`.

----------
components: Library (Lib)
messages: 374339
nosy: webisteme
priority: normal
severity: normal
status: open
title: Uncaught AttributeError in unittest.mock._get_target
type: behavior
versions: Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue41403>
_______________________________________


More information about the Python-bugs-list mailing list