gh-125519: Improve traceback if importlib.reload() is called with a non-module object#125520
Conversation
…with a non-module object
| def test_reload_traceback_with_non_str(self): | ||
| # gh-125519 | ||
| with support.captured_stdout() as stdout: | ||
| try: | ||
| self.init.reload("typing") | ||
| except TypeError as exc: | ||
| traceback.print_exception(exc, file=stdout) | ||
| else: | ||
| self.fail("Expected TypeError to be raised") | ||
| printed_traceback = stdout.getvalue() | ||
| self.assertIn("TypeError", printed_traceback) | ||
| self.assertNotIn("AttributeError", printed_traceback) | ||
| self.assertNotIn("module.__spec__.name", printed_traceback) |
There was a problem hiding this comment.
We could also do this with assertions on the __cause__ or __context__ attribute of the raised exception, but that feels like asserting an implementation detail to me. I prefer for the test to assert what the user will be seeing when the exception is raised (which feels like the important thing).
|
Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13. |
…with a non-module object (pythonGH-125520) (cherry picked from commit c5c21fe) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
…with a non-module object (pythonGH-125520) (cherry picked from commit c5c21fe) Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
|
GH-125768 is a backport of this pull request to the 3.13 branch. |
|
GH-125769 is a backport of this pull request to the 3.12 branch. |
|
Thanks for the review @hauntsaninja! |
…with a non-module object (python#125520)
importlib.reload()is called with astr#125519