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
Importing call from unittest.mock directly causes ValueError #79934
Comments
Ok so that's a pretty odd bug. I already had from unittest.mock import patch, mock_open so I simply modified that to from instead of doing mock.call in my test. Changing this to from unittest import mock and then mock.call fixed the error. from unittest.mock import patch, mock_open, call mocked_print.assert_has_calls([ I get: C:\Program Files (x86)\Python37-64\lib\doctest.py:932: in find But when I don't import call directly my test runs as expected: from unittest.mock import patch, mock_open
import unittest.mock mocked_print.assert_has_calls([ I have the same issue when using: assert mocked_print.call_args_list == [call("first print"), call("second print")] <- ValueError |
I'm having a problem with mock.call when I import it directly from unittest.mock. When I do: from unittest.mock import patch, mock_open, call mocked_print.assert_has_calls([ I get: C:\Program Files (x86)\Python37-64\lib\doctest.py:932: in find But when I don't import call directly my test runs as expected: from unittest.mock import patch, mock_open
import unittest.mock mocked_print.assert_has_calls([ I have the same issue when using: assert mocked_print.call_args_list == [call("first print"), call("second print")] <- ValueError |
Apologies for initial malformed message, I hit submit prematurely. |
Hi David, Can you provide a full reproducer? In your example, mocked_print is undefined. Please, provide a self-contained script that can be executed to test the behaviour. |
According to the error message, this is a duplicate of https://bugs.python.org/issue25532 |
Indeed, closed as duplicate of https://bugs.python.org/issue25532 |
Sorry, looks like I was wrong about it being a duplicate. The actual bug appears to be "doctest can't run on a module that contains un-unwrappable objects", which there probably is an issue for but I don't know what it is. |
I think this tickt needs to be reopened, because
@PPPerry Or do you already know where this issue is tracked? |
Indeed, you are right that this should be reopened. |
Is it the below example scenario being discussed in the issue? Since call is imported it's also present in obj.__dict__ and hence trying to unwrap it [0] could cause error. This is also mentioned in https://bugs.python.org/issue25532#msg253885 against which this issue was closed.
# bpo35753.py
from unittest.mock import call ./python.exe -m doctest bpo35753.py
Traceback (most recent call last):
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/runpy.py", line 192, in _run_module_as_main
return _run_code(code, main_globals, None,
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/runpy.py", line 85, in _run_code
exec(code, run_globals)
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/doctest.py", line 2785, in <module>
sys.exit(_test())
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/doctest.py", line 2775, in _test
failures, _ = testmod(m, verbose=verbose, optionflags=options)
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/doctest.py", line 1947, in testmod
for test in finder.find(m, name, globs=globs, extraglobs=extraglobs):
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/doctest.py", line 932, in find
self._find(tests, obj, name, module, source_lines, globs, {})
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/doctest.py", line 991, in _find
if ((inspect.isroutine(inspect.unwrap(val))
File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/inspect.py", line 524, in unwrap
raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
ValueError: wrapper loop when unwrapping call [0] Line 991 in aaf47ca
|
Somehow I missed the follow up here until now. I don't remember the original code, but I'm fairly confident that mocked_print is the patched print function eg Probably solved in this commit dated the same day as the bug report: |
I have a patch here that fixes this: #22981 It simply swallows the unwrap exception making doctest immune to such buggy objects. Can someone help it get reviewed please? |
Fixed in 3.10 and 3.11, thanks Albert for your patch. |
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: