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: Uncaught AttributeError in unittest.mock._get_target
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: andrei.avk, cjw296, iritkatriel, mariocj89, michael.foord, webisteme, xtreak
Priority: normal Keywords: patch

Created on 2020-07-26 19:15 by webisteme, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 30833 merged iritkatriel, 2022-01-23 17:40
PR 30834 merged iritkatriel, 2022-01-23 18:48
PR 30835 merged iritkatriel, 2022-01-23 18:52
Messages (6)
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.
msg409472 - (view) Author: Andrei Kulakov (andrei.avk) * (Python triager) Date: 2022-01-01 19:05
More generally, split()/rsplit() are probably the most common operations done on expected string at the start of a function, i.e. most likely to be triggered on the wrong type passed in. At the same time to many new users split()/rsplit() does not imply anything related to strings, and 'split' sounds too generic to expect explanatory results from google search. (although the first few results do point to the right explanation, and rsplit does seem like something that would return helpful search results).

I wonder if it would be helpful, especially for .split() (and rsplit for consistency), to add to the error msg: "... it's likely that a string object was expected instead of <type> object".
msg411400 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-23 18:42
New changeset f7955a82e36d4c32ebdd7b7707cdf0e6ffa7a418 by Irit Katriel in branch 'main':
bpo-41403: Improve error message for invalid mock target (GH-30833)
https://github.com/python/cpython/commit/f7955a82e36d4c32ebdd7b7707cdf0e6ffa7a418
msg411410 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-23 19:34
New changeset e3ade66ec575e0cb4882cfdff155ef962e67c837 by Irit Katriel in branch '3.10':
bpo-41403: Improve error message for invalid mock target (GH-30833) (GH-30834)
https://github.com/python/cpython/commit/e3ade66ec575e0cb4882cfdff155ef962e67c837
msg411411 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-01-23 19:35
New changeset 1398dca838529e682c06b496cc1911d91334ff3a by Irit Katriel in branch '3.9':
bpo-41403: Improve error message for invalid mock target (GH-30833) (GH-30835)
https://github.com/python/cpython/commit/1398dca838529e682c06b496cc1911d91334ff3a
History
Date User Action Args
2022-04-11 14:59:34adminsetgithub: 85575
2022-01-23 19:35:34iritkatrielsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2022-01-23 19:35:18iritkatrielsetmessages: + msg411411
2022-01-23 19:34:50iritkatrielsetmessages: + msg411410
2022-01-23 18:52:24iritkatrielsetpull_requests: + pull_request29021
2022-01-23 18:48:39iritkatrielsetpull_requests: + pull_request29020
2022-01-23 18:42:52iritkatrielsetmessages: + msg411400
2022-01-23 17:40:24iritkatrielsetkeywords: + patch
nosy: + iritkatriel

pull_requests: + pull_request29019
stage: patch review
2022-01-14 18:27:44iritkatrielsetversions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.7
2022-01-01 19:05:29andrei.avksetnosy: + andrei.avk
messages: + msg409472
2020-07-27 06:40:04cjw296setmessages: + msg374362
2020-07-27 04:07:36xtreaksetnosy: + cjw296, michael.foord, mariocj89, xtreak
2020-07-26 19:15:44webistemecreate