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

Two tracebacks with assert_wrapper / wrap_assert_has_calls #44

Closed
blueyed opened this issue May 20, 2016 · 5 comments
Closed

Two tracebacks with assert_wrapper / wrap_assert_has_calls #44

blueyed opened this issue May 20, 2016 · 5 comments

Comments

@blueyed
Copy link
Contributor

blueyed commented May 20, 2016

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.

@asfaltboy
Copy link
Contributor

asfaltboy commented May 20, 2016

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.

@The-Compiler
Copy link
Member

@asfaltboy you mean 2.5, not 3.5 😉

@asfaltboy
Copy link
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.

@nicoddemus
Copy link
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.

@blueyed
Copy link
Contributor Author

blueyed commented May 21, 2016

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants