Skip to content

Add exception to value alias on ExceptionInfo #5541

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions changelog/5541.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add ``.exception`` attribute as an alias for ``.value`` to facilitate porting tests written using unittest.
Copy link
Member

Choose a reason for hiding this comment

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

This should be updated to mention the warning attribute of pytest.warns

Copy link
Member

Choose a reason for hiding this comment

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

Oh this should also be 5541.improvement.rst now.

14 changes: 14 additions & 0 deletions src/_pytest/_code/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,20 @@ def type(self):
def value(self):
"""the exception value"""
return self._excinfo[1]

@property
def exception(self):
"""
an alias to '.value' to facilitate porting porting tests written using
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
an alias to '.value' to facilitate porting porting tests written using
an alias to '.value' to facilitate porting tests written using

unittest. Prefer '.value' in new code.
"""
msg = (
"The '.exception' attribute is an alias to facilitate porting "
"tests written using unittest.\n"
"Prefer '.value' in new code."
)
warnings.warn(PytestWarning(msg), stacklevel=2)
return self.value

@property
def tb(self):
Expand Down
55 changes: 30 additions & 25 deletions src/_pytest/recwarn.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,28 +186,33 @@ def __exit__(self, *exc_info):
__tracebackhide__ = True

# only check if we're not currently handling an exception
Copy link
Member

Choose a reason for hiding this comment

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

We need a test for the warning attribute to avoid regressions.

if all(a is None for a in exc_info):
if self.expected_warning is not None:
if not any(issubclass(r.category, self.expected_warning) for r in self):
__tracebackhide__ = True
fail(
"DID NOT WARN. No warnings of type {} was emitted. "
"The list of emitted warnings is: {}.".format(
self.expected_warning, [each.message for each in self]
)
)
elif self.match_expr is not None:
for r in self:
if issubclass(r.category, self.expected_warning):
if re.compile(self.match_expr).search(str(r.message)):
break
else:
fail(
"DID NOT WARN. No warnings of type {} matching"
" ('{}') was emitted. The list of emitted warnings"
" is: {}.".format(
self.expected_warning,
self.match_expr,
[each.message for each in self],
)
)
if not all(a is None for a in exc_info):
return

if self.expected_warning is None:
return

for r in self:
if issubclass(r.category, self.expected_warning) and (
self.match_expr is None or re.search(self.match_expr, str(r.message))
):
self.warning = r.message
Copy link
Member

Choose a reason for hiding this comment

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

These attributes should be initialized with None in __init__

Copy link
Member

Choose a reason for hiding this comment

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

Also, we should add a quick example to the docs to warnings.rst. 👍

self.filename = r.filename
self.lineno = r.lineno
return

if self.match_expr is None:
fail(
"DID NOT WARN. No warnings of type {} was emitted. "
"The list of emitted warnings is: {}.".format(
self.expected_warning, [each.message for each in self]
)
)

fail(
"DID NOT WARN. No warnings of type {} matching"
" ('{}') was emitted. The list of emitted warnings"
" is: {}.".format(
self.expected_warning, self.match_expr, [each.message for each in self]
)
)