Skip to content

Eliminate "during the above exception" in py3 #79

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

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pytest_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def mock(mocker):

def assert_wrapper(__wrapped_mock_method__, *args, **kwargs):
__tracebackhide__ = True
err = None
try:
__wrapped_mock_method__(*args, **kwargs)
except AssertionError as e:
Expand All @@ -180,7 +181,9 @@ def assert_wrapper(__wrapped_mock_method__, *args, **kwargs):
actual_args, actual_kwargs = __mock_self.call_args
assert actual_args == args[1:]
assert actual_kwargs == kwargs
raise AssertionError(*e.args)
err = e
if err is not None:
raise AssertionError(*err.args)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use else: with the try to return and just raise AssertionError in the end then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remembered that Python 3 (can't find a link now) will delete the exception value caught by as after leaving the except block; that is done to avoiding leaking the frames in the traceback value.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@blueyed yeah this is constructed this way because if the assertion is raised inside the
except block then you still get the during the above exception message/tb. If the e isn't assigned to some other variable then it is a NameError in py3. If this project didn't support py2 we could theoretically do raise ... from None but that's illegal before py3 and it didn't actually work correctly for me in my testing.



def wrap_assert_not_called(*args, **kwargs):
Expand Down