Skip to content
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

gh-125519: Improve traceback if importlib.reload() is called with a non-module object #125520

Merged
merged 1 commit into from
Oct 21, 2024

Conversation

AlexWaygood
Copy link
Member

@AlexWaygood AlexWaygood commented Oct 15, 2024

Comment on lines +358 to +370
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)
Copy link
Member Author

@AlexWaygood AlexWaygood Oct 15, 2024

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).

Copy link
Contributor

@hauntsaninja hauntsaninja left a 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!

@AlexWaygood AlexWaygood merged commit c5c21fe into python:main Oct 21, 2024
41 checks passed
@AlexWaygood AlexWaygood deleted the importlib-reload-tb branch October 21, 2024 06:53
@miss-islington-app
Copy link

Thanks @AlexWaygood for the PR 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 21, 2024
…with a non-module object (pythonGH-125520)

(cherry picked from commit c5c21fe)

Co-authored-by: Alex Waygood <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Oct 21, 2024
…with a non-module object (pythonGH-125520)

(cherry picked from commit c5c21fe)

Co-authored-by: Alex Waygood <[email protected]>
@bedevere-app
Copy link

bedevere-app bot commented Oct 21, 2024

GH-125768 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Oct 21, 2024
@bedevere-app
Copy link

bedevere-app bot commented Oct 21, 2024

GH-125769 is a backport of this pull request to the 3.12 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.12 bug and security fixes label Oct 21, 2024
@AlexWaygood
Copy link
Member Author

Thanks for the review @hauntsaninja!

AlexWaygood added a commit that referenced this pull request Oct 21, 2024
… with a non-module object (GH-125520) (#125769)

gh-125519: Improve traceback if `importlib.reload()` is called with a non-module object (GH-125520)
(cherry picked from commit c5c21fe)

Co-authored-by: Alex Waygood <[email protected]>
AlexWaygood added a commit that referenced this pull request Oct 21, 2024
… with a non-module object (GH-125520) (#125768)

gh-125519: Improve traceback if `importlib.reload()` is called with a non-module object (GH-125520)
(cherry picked from commit c5c21fe)

Co-authored-by: Alex Waygood <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir topic-importlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants