-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-102812: unittest's addError/addFailure/addSubTest accept an exception instance in addition to (typ, val, tb) as paramter #102814
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
Conversation
…exception instance in addition to (typ, val, tb) as paramter
| else: | ||
| result.addError(test, exc_info) | ||
| result.addError(test, exc) |
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.
Maybe I should revert this change, in case there are use-defined subclasses of TestResult that expect the tuple?
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.
Yeah, I think the folks still using unittest are often in interesting and subtle places (Twisted's trial springs to mind...) so I think we really want to make sure 100% backwards compatibility (even in internal APIs sigh) is preserved if at all possible.
I'm curious: where are you finding yourself calling addError and friends in your code?
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.
I'm not calling addError in my code. We're trying to remove the (typ, exc, tb) triplet from the language. The interpreter doesn't need it anymore, it only exists for backwards compatibility.
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.
I made this a draft PR, it need more thought. If we can't make unittest use the new APIs then I'm not sure there is a point committing this. We would need a plan for how to migrate off the triplet.
This is related to the PEP707 discussion, so we could wait to see what comes out of that.
| except _UnexpectedSuccess: | ||
| result.addFailure(self, sys.exc_info()) | ||
| except _UnexpectedSuccess as e: | ||
| result.addFailure(self, e) |
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.
ditto.
| self._createClassOrModuleLevelException( | ||
| result, exc_info[1], 'setUpClass', className, | ||
| info=exc_info) | ||
| err=exc) |
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.
ditto
| :meth:`~unittest.TestResult.addSubTest` now accept either an exception | ||
| instance or a ``(typ, val, tb)`` tuple. (Contributed by Irit Katriel in |
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.
Make clear what is new while being clear that still only 1 is passed.
| :meth:`~unittest.TestResult.addSubTest` now accept either an exception | |
| instance or a ``(typ, val, tb)`` tuple. (Contributed by Irit Katriel in | |
| :meth:`~unittest.TestResult.addSubTest` now accept an exception instead of | |
| a ``(typ, val, tb)`` tuple. (Contributed by Irit Katriel in |
Do you want to say that an exception is in some sense preferred?
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.
I think "instead of" may sound like the old format is no longer accepted. But agree I would emphasise what is new and what is old.
| if isinstance(err, subtest.failureException): | ||
| self._write_status(subtest, "FAIL") | ||
| else: | ||
| self._write_status(subtest, "ERROR") |
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.
Factor out minimal difference in the two branches depending on the condition.
| if isinstance(err, subtest.failureException): | |
| self._write_status(subtest, "FAIL") | |
| else: | |
| self._write_status(subtest, "ERROR") | |
| status = "FAIL" if isinstance(err, subtest.failureException) else "ERROR" | |
| self._write_status(subtest, status) |
There might be a better word than 'status'. Note: if the statement were not already being edited, this would be an unrelated change and I would not suggest it here. But since you are ...
| if isinstance(err, subtest.failureException): | ||
| self.stream.write('F') | ||
| else: | ||
| self.stream.write('E') |
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.
| if isinstance(err, subtest.failureException): | |
| self.stream.write('F') | |
| else: | |
| self.stream.write('E') | |
| dot = 'F' if isinstance(err, subtest.failureException) else 'E' | |
| self.stream.write(dot) |
| error = _ErrorHolder(errorName) | ||
| addSkip = getattr(result, 'addSkip', None) | ||
| if addSkip is not None and isinstance(exception, case.SkipTest): | ||
| addSkip(error, str(exception)) | ||
| else: | ||
| if not info: | ||
| result.addError(error, sys.exc_info()) | ||
| if not err: |
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.
It is not clear why the assert it needed, but with it, a possible refactoring (which cannot be marked as a multi-line suggestion).
if not err:
err = sys.exc_info()[1]
else:
assert not isinstance(err, tuple)
result.addError(error, err)
| :meth:`~unittest.TestResult.addSubTest` now accept either an exception | ||
| instance or a ``(typ, val, tb)`` tuple. |
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.
| :meth:`~unittest.TestResult.addSubTest` now accept either an exception | |
| instance or a ``(typ, val, tb)`` tuple. | |
| :meth:`~unittest.TestResult.addSubTest` now accept an exception instance | |
| instead of a ``(typ, val, tb)`` tuple. |
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.
My turn to ping ;-). Too late for 3.12 I guess, hence new suggestion(s). Please accept the suggestions you are sure of, and resolve when appropriate. I fixed the merge conflict (easy).
|
|
||
| The default implementation appends a tuple ``(test, formatted_err)`` to | ||
| the instance's :attr:`errors` attribute, where *formatted_err* is a | ||
| formatted traceback derived from *err*. | ||
|
|
||
| .. versionchanged:: 3.12 |
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.
| .. versionchanged:: 3.12 | |
| .. versionchanged:: 3.13 |
|
|
||
| The default implementation appends a tuple ``(test, formatted_err)`` to | ||
| the instance's :attr:`failures` attribute, where *formatted_err* is a | ||
| formatted traceback derived from *err*. | ||
|
|
||
| .. versionchanged:: 3.12 |
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.
| .. versionchanged:: 3.12 | |
| .. versionchanged:: 3.13 |
|
|
||
| The default implementation does nothing when the outcome is a | ||
| success, and records subtest failures as normal failures. | ||
|
|
||
| .. versionadded:: 3.4 | ||
|
|
||
| .. method:: addDuration(test, elapsed) | ||
| .. versionchanged:: 3.12 |
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.
| .. versionchanged:: 3.12 | |
| .. versionchanged:: 3.13 |
|
Thanks, @terryjreedy . I have some doubts about this PR because I think there might be user-defined subclasses of unittest that override these functions (essentially callbacks expecting (typ, exc, tb) triplets). I'm not sure how to move us on from that. |
|
Closing because I think this is more complicated than I thought. (See previous comment). |
Uh oh!
There was an error while loading. Please reload this page.