classification
Title: Uncaught AttributeError in unittest.mock._get_target
Type: behavior Stage:
Components: Library (Lib) Versions: Python 3.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: cjw296, mariocj89, michael.foord, webisteme, xtreak
Priority: normal Keywords:

Created on 2020-07-26 19:15 by webisteme, last changed 2020-07-27 06:40 by cjw296.

Messages (2)
msg374339 - (view) Author: (webisteme) * Date: 2020-07-26 19:15
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`.
msg374362 - (view) Author: Chris Withers (cjw296) * (Python committer) Date: 2020-07-27 06:40
Given that `mock.patch` is being used incorrectly here, the error message seems clear enough: It's saying there's a `Foo` object in place, and `rsplit` gives a strong indication that a string was expected.

Would adding type hints in mock.py be a resolution for this?

I'd be fine to see this bug closed with "wontfix", but will leave it open for now in case others feel strongly differently.
History
Date User Action Args
2020-07-27 06:40:04cjw296setmessages: + msg374362
2020-07-27 04:07:36xtreaksetnosy: + cjw296, michael.foord, mariocj89, xtreak
2020-07-26 19:15:44webistemecreate