Skip to content
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

Setting a side_effect on mock from create_autospec doesn't work #62026

Closed
voidspace opened this issue Apr 24, 2013 · 9 comments
Closed

Setting a side_effect on mock from create_autospec doesn't work #62026

voidspace opened this issue Apr 24, 2013 · 9 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@voidspace
Copy link
Contributor

BPO 17826
Nosy @gpshead, @ezio-melotti, @merwok, @voidspace, @kushaldas
Files
  • issue17826_v1.patch: Code and test case update
  • issue17826_v2.patch: Code and test case update revision 2
  • issue17826_v3.patch: New patch with fix in proper place for side_effect for functions (includes test case).
  • 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:

    assignee = 'https://github.com/voidspace'
    closed_at = <Date 2014-04-14.20:10:47.946>
    created_at = <Date 2013-04-24.10:00:07.992>
    labels = ['type-bug', 'library']
    title = "Setting a side_effect on mock from create_autospec doesn't work"
    updated_at = <Date 2014-04-14.20:10:47.945>
    user = 'https://github.com/voidspace'

    bugs.python.org fields:

    activity = <Date 2014-04-14.20:10:47.945>
    actor = 'michael.foord'
    assignee = 'michael.foord'
    closed = True
    closed_date = <Date 2014-04-14.20:10:47.946>
    closer = 'michael.foord'
    components = ['Library (Lib)']
    creation = <Date 2013-04-24.10:00:07.992>
    creator = 'michael.foord'
    dependencies = []
    files = ['30025', '30055', '34838']
    hgrepos = []
    issue_num = 17826
    keywords = ['patch']
    message_count = 9.0
    messages = ['187692', '187840', '187844', '187845', '188036', '215916', '216098', '216164', '216196']
    nosy_count = 6.0
    nosy_names = ['gregory.p.smith', 'ezio.melotti', 'eric.araujo', 'michael.foord', 'python-dev', 'kushal.das']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue17826'
    versions = ['Python 3.3', 'Python 3.4']

    @voidspace
    Copy link
    Contributor Author

    >>> from unittest.mock import create_autospec
    >>> def f(): pass
    ... 
    >>> m = create_autospec(f)
    >>> m.side_effect = [1, 2]
    >>> m()
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
      File "<string>", line 3, in f
      File "/compile/py3k-cpython/Lib/unittest/mock.py", line 872, in __call__
        return _mock_self._mock_call(*args, **kwargs)
      File "/compile/py3k-cpython/Lib/unittest/mock.py", line 931, in _mock_call
        result = next(effect)
    TypeError: 'list' object is not an iterator

    @voidspace voidspace self-assigned this Apr 24, 2013
    @voidspace voidspace added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Apr 24, 2013
    @voidspace
    Copy link
    Contributor Author

    This illustrates the difference:

    >>> from mock import Mock, create_autospec
    >>> some_list = [1, 2, 3]
    >>> m = Mock()
    >>> m.side_effect = some_list
    >>> m.side_effect
    <listiterator object at 0x1004ab7d0>
    >>> m2 = create_autospec(lambda: None)
    >>> m2.side_effect = some_list
    >>> m2.side_effect
    [1, 2, 3]

    When setting a side_effect on a function (created by create_autospec) the side_effect setter is not used - so turning the list into an iterator needs to be done on first use instead.

    @kushaldas
    Copy link
    Member

    Working on this.

    @kushaldas
    Copy link
    Member

    Patch along with a test for the same.

    @kushaldas
    Copy link
    Member

    Version 2 of the patch, with typo fixed also one more addition test to check callable side effect in create_autospec.

    @merwok
    Copy link
    Member

    merwok commented Apr 11, 2014

    Michael, a patch including tests is ready for this issue.

    @voidspace
    Copy link
    Contributor Author

    Can you explain why we need to check for the call_count here? I don't understand why this is needed.

    @kushaldas
    Copy link
    Member

    New patch with fix in proper place for side_effect for functions (includes test case).

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Apr 14, 2014

    New changeset 1e3c64470629 by Michael Foord in branch '3.4':
    bpo-17826. Setting an iterable side_effect on a mock created by create_autospec now works
    http://hg.python.org/cpython/rev/1e3c64470629

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants