-
-
Notifications
You must be signed in to change notification settings - Fork 31.8k
gh-125519: Improve traceback if importlib.reload()
is called with a non-module object
#125520
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
…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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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).
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 asserting __context__
is None is fine, a la bugbear/ruff B904. But this is fine too!
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