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

Importing call from unittest.mock directly causes ValueError #79934

Closed
toonarmycaptain mannequin opened this issue Jan 16, 2019 · 14 comments
Closed

Importing call from unittest.mock directly causes ValueError #79934

toonarmycaptain mannequin opened this issue Jan 16, 2019 · 14 comments
Labels
3.10 only security fixes 3.11 only security fixes tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error

Comments

@toonarmycaptain
Copy link
Mannequin

toonarmycaptain mannequin commented Jan 16, 2019

BPO 35753
Nosy @ambv, @pablogsal, @toonarmycaptain, @miss-islington, @splbio, @tirkarthi
PRs
  • [3.8] bpo-35753: Fix crash in doctest with unwrap-able functions #22834
  • bpo-35753: Fix crash in doctest with unwrap-able functions #22981
  • [3.10] bpo-35753: Fix crash in doctest with unwrap-able functions (GH-22981) #25926
  • 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 = None
    closed_at = <Date 2021-05-05.18:02:39.893>
    created_at = <Date 2019-01-16.20:17:24.858>
    labels = ['type-bug', 'tests', '3.10', '3.11']
    title = 'Importing call from unittest.mock directly causes ValueError'
    updated_at = <Date 2021-07-18.15:30:41.973>
    user = 'https://github.com/toonarmycaptain'

    bugs.python.org fields:

    activity = <Date 2021-07-18.15:30:41.973>
    actor = 'pablogsal'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-05-05.18:02:39.893>
    closer = 'lukasz.langa'
    components = ['Tests']
    creation = <Date 2019-01-16.20:17:24.858>
    creator = 'toonarmycaptain'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35753
    keywords = ['patch']
    message_count = 14.0
    messages = ['333786', '333787', '333788', '333789', '333790', '333791', '333797', '343335', '343339', '343573', '379177', '388710', '393027', '393028']
    nosy_count = 7.0
    nosy_names = ['lukasz.langa', 'pablogsal', 'toonarmycaptain', 'miss-islington', 'splbio', 'xtreak', 'Thomas Hisch']
    pr_nums = ['22834', '22981', '25926']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue35753'
    versions = ['Python 3.10', 'Python 3.11']

    @toonarmycaptain
    Copy link
    Mannequin Author

    toonarmycaptain mannequin commented Jan 16, 2019

    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([
    call("first print"),
    call("second print"),
    ])

    I get:

    C:\Program Files (x86)\Python37-64\lib\doctest.py:932: in find
    self._find(tests, obj, name, module, source_lines, globs, {})
    C:\Program Files (x86)\Python37-64\lib\doctest.py:991: in _find
    if ((inspect.isroutine(inspect.unwrap(val))
    C:\Program Files (x86)\Python37-64\lib\inspect.py:515: in unwrap
    raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
    E ValueError: wrapper loop when unwrapping call
    collected 1 item / 1 errors

    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([
    mock.call(),
    mock.call(),
    ])

    I have the same issue when using:

    assert mocked_print.call_args_list == [call("first print"), call("second print")] <- ValueError
    assert mocked_print.call_args_list == [mock.call("first print"), mock.call("second print")] <- Works as expected.

    @toonarmycaptain toonarmycaptain mannequin added 3.7 (EOL) end of life tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error labels Jan 16, 2019
    @toonarmycaptain
    Copy link
    Mannequin Author

    toonarmycaptain mannequin commented Jan 16, 2019

    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([
    call("first print"),
    call("second print"),
    ])

    I get:

    C:\Program Files (x86)\Python37-64\lib\doctest.py:932: in find
    self._find(tests, obj, name, module, source_lines, globs, {})
    C:\Program Files (x86)\Python37-64\lib\doctest.py:991: in _find
    if ((inspect.isroutine(inspect.unwrap(val))
    C:\Program Files (x86)\Python37-64\lib\inspect.py:515: in unwrap
    raise ValueError('wrapper loop when unwrapping {!r}'.format(f))
    E ValueError: wrapper loop when unwrapping call
    collected 1 item / 1 errors

    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([
    mock.call(),
    mock.call(),
    ])

    I have the same issue when using:

    assert mocked_print.call_args_list == [call("first print"), call("second print")] <- ValueError
    assert mocked_print.call_args_list == [mock.call("first print"), mock.call("second print")] <- Works as expected.

    @toonarmycaptain
    Copy link
    Mannequin Author

    toonarmycaptain mannequin commented Jan 16, 2019

    Apologies for initial malformed message, I hit submit prematurely.

    @pablogsal
    Copy link
    Member

    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.

    @pppery
    Copy link
    Mannequin

    pppery mannequin commented Jan 16, 2019

    According to the error message, this is a duplicate of https://bugs.python.org/issue25532

    @pablogsal
    Copy link
    Member

    Indeed, closed as duplicate of https://bugs.python.org/issue25532

    @pppery
    Copy link
    Mannequin

    pppery mannequin commented Jan 16, 2019

    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.

    @ThomasHisch
    Copy link
    Mannequin

    ThomasHisch mannequin commented May 24, 2019

    I think this tickt needs to be reopened, because

    The actual bug appears to be "doctest can't run on a module that contains un-unwrappable objects"

    @PPPerry Or do you already know where this issue is tracked?

    @pppery
    Copy link
    Mannequin

    pppery mannequin commented May 24, 2019

    Indeed, you are right that this should be reopened.

    @pablogsal pablogsal reopened this May 26, 2019
    @tirkarthi
    Copy link
    Member

    The actual bug appears to be "doctest can't run on a module that contains un-unwrappable objects"

    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.

    This can occur when you use doctest.DocTestSuite to load doc tests from a module where unittest.mock.call has been imported.

    # 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]

    if ((inspect.isroutine(inspect.unwrap(val))

    @toonarmycaptain
    Copy link
    Mannequin Author

    toonarmycaptain mannequin commented Oct 21, 2020

    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
    with patch('dionysus_app.class_functions.print') as mocked_print:

    Probably solved in this commit dated the same day as the bug report:
    toonarmycaptain/dionysus@4f0eb7c

    @splbio
    Copy link
    Mannequin

    splbio mannequin commented Mar 15, 2021

    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?

    @ambv
    Copy link
    Contributor

    ambv commented May 5, 2021

    New changeset 10d6f6b by Miss Islington (bot) in branch '3.10':
    bpo-35753: Fix crash in doctest with unwrap-able functions (GH-22981) (bpo-25926)
    10d6f6b

    @ambv
    Copy link
    Contributor

    ambv commented May 5, 2021

    Fixed in 3.10 and 3.11, thanks Albert for your patch.

    @ambv ambv added 3.10 only security fixes 3.11 only security fixes and removed 3.7 (EOL) end of life labels May 5, 2021
    @ambv ambv closed this as completed May 5, 2021
    @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
    3.10 only security fixes 3.11 only security fixes tests Tests in the Lib/test dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants