Skip to content

Two tracebacks with assert_wrapper / wrap_assert_has_calls #44

Closed

Description

@blueyed

When using assert_has_calls I am seeing two tracebacks, but the first one should not be there?!

Traceback (most recent call last):
  File "…/pytest-mock/pytest_mock.py", line 155, in assert_wrapper
    __wrapped_mock_method__(*args, **kwargs)
  File "/usr/lib64/python3.5/unittest/mock.py", line 824, in assert_has_calls
    ) from cause
AssertionError: Calls not found.
Expected: […]
Actual: […]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "…/app/tests/test_metrics.py", line 495, in test_handle_missing_values
    expected_call(end, 2, None),
  File "…/pytest-mock/pytest_mock.py", line 181, in wrap_assert_has_calls
    *args, **kwargs)
  File "…/pytest-mock/pytest_mock.py", line 157, in assert_wrapper
    raise AssertionError(*e.args)
AssertionError: Calls not found.
Expected: […]
Actual: […]

This is with pytest-2.9.2.dev1 and Python 3.5.1.

It looks like the __tracebackhide__ = True trick is not working?!

With mock_traceback_monkeypatch=false I only get the original traceback as expected.

Activity

  1. asfaltboy commented on May 20, 2016

    @asfaltboy
    Contributor

    Although I have not tried using the plugin with python 3 yet, this looks like the "raise .. from .." exception chaining feature which was added in 3.5, to quote the PEP:

    The default exception handler will be modified to report chained
    exceptions.  The chain of exceptions is traversed by following the
    '__cause__' and '__context__' attributes, with '__cause__' taking
    priority.  In keeping with the chronological order of tracebacks,
    the most recently raised exception is displayed last; that is, the
    display begins with the description of the innermost exception and
    backs up the chain to the outermost exception.  The tracebacks are
    formatted as usual, with one of the lines:
    

    So if we want to "hide" the fact that we caught and raised the AssertionErroe exception in python 3, we could probably set e.__context__ = None, or maybe e.__suppress_context__ = True is better and more explicit.

  2. The-Compiler commented on May 20, 2016

    @The-Compiler
    Member

    @asfaltboy you mean 2.5, not 3.5 😉

  3. asfaltboy commented on May 20, 2016

    @asfaltboy
    Contributor

    Nah, you'll not find the changes in 2; though version 2.5 was the original target for PEP 344, it was superseded by PEP 3134 since it was targeted for Python 3. You can see 3134 mentioned in Python 3 release notes.

  4. nicoddemus commented on May 20, 2016

    @nicoddemus
    Member

    @blueyed this happens only with --tb=native right?

    @asfaltboy tried your suggestion real quick now but it did not work: inside the except clause in assert_wrapper, e.__context__ is already None. I will investigate this more carefully later, but meanwhile if somebody got another suggestion I'm all ears.

  5. blueyed commented on May 21, 2016

    @blueyed
    ContributorAuthor

    @nicoddemus
    Yeah, I am using --tb=native.

  6. added a commit that references this issue on May 25, 2016
    dfd2603
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions