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
mock side_effect should be checked for iterable not callable #80779
Comments
In mock.py, in method: There is a following piece of code: if not _callable(effect):
result = next(effect)
if _is_exception(result):
raise result
if result is DEFAULT:
result = self.return_value
return result
ret_val = effect(*args, **kwargs) This works correctly for iterables (such as lists) that are not defined as generators. It seems like the check should be not for callable, but for iterable:
|
I am not sure if the snippets you are referring to are from testing-cabal/mock repo which could be different from master branch. Current code is at [0] if effect is not None:
if _is_exception(effect):
raise effect
elif not _callable(effect):
result = next(effect)
if _is_exception(result):
raise result
else:
result = effect(*args, **kwargs)
if result is not DEFAULT:
return result
This does seem to work for generator function as below. Sorry, maybe I am getting it wrong with respect to terminologies and understanding the issue. Can you add a short script around what you are expecting? $ cat ../backups/bpo36598.py
from unittest.mock import patch def gen(i):
while i < 5:
yield i
i += 1
def foo():
return 1
with patch('__main__.foo', side_effect=gen(0)):
for _ in range(2):
print(foo())
for _ in range(2):
print(foo())
$ ./python.exe ../backups/bpo36598.py
0
1
2
3 [0] Line 1043 in a9bd892
|
You are right. I was not calling generator the right way in mock. After I tried your suggestion it works. |
Thanks, I am closing this as not a bug. Feel free to reopen this if I have missed any. |
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: