-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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. | ||
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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): | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -186,28 +186,33 @@ def __exit__(self, *exc_info): | |
__tracebackhide__ = True | ||
|
||
# only check if we're not currently handling an exception | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a test for the |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These attributes should be initialized with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, we should add a quick example to the docs to |
||
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] | ||
) | ||
) |
There was a problem hiding this comment.
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 ofpytest.warns
There was a problem hiding this comment.
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.