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
Uncaught AttributeError in unittest.mock._get_target #85575
Comments
When calling >>> 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 |
Given that 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. |
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". |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: