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

Messy traceback if importlib.reload() is called with a str #125519

Closed
AlexWaygood opened this issue Oct 15, 2024 · 0 comments
Closed

Messy traceback if importlib.reload() is called with a str #125519

AlexWaygood opened this issue Oct 15, 2024 · 0 comments
Assignees
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir topic-importlib type-bug An unexpected behavior, bug, or error

Comments

@AlexWaygood
Copy link
Member

AlexWaygood commented Oct 15, 2024

Bug report

Bug description:

If you call importlib.reload() with a str, there's a huge amount of unnecessary detail in the traceback that makes it look like there's an internal error in the function itself:

>>> import importlib
>>> import typing
>>> importlib.reload("typing")
Traceback (most recent call last):
  File "/Users/alexw/.pyenv/versions/3.13.0/lib/python3.13/importlib/__init__.py", line 101, in reload
    name = module.__spec__.name
           ^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '__spec__'. Did you mean: '__doc__'?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/alexw/.pyenv/versions/3.13.0/lib/python3.13/importlib/__init__.py", line 104, in reload
    name = module.__name__
           ^^^^^^^^^^^^^^^
AttributeError: 'str' object has no attribute '__name__'. Did you mean: '__ne__'?

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    importlib.reload("typing")
    ~~~~~~~~~~~~~~~~^^^^^^^^^^
  File "/Users/alexw/.pyenv/versions/3.13.0/lib/python3.13/importlib/__init__.py", line 106, in reload
    raise TypeError("reload() argument must be a module")
TypeError: reload() argument must be a module

We should probably suppress the exception context here.

CPython versions tested on:

3.12, 3.13, CPython main branch

Operating systems tested on:

macOS

Linked PRs

@AlexWaygood AlexWaygood added type-bug An unexpected behavior, bug, or error stdlib Python modules in the Lib dir topic-importlib labels Oct 15, 2024
@AlexWaygood AlexWaygood self-assigned this Oct 15, 2024
@AlexWaygood AlexWaygood added 3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Oct 15, 2024
AlexWaygood added a commit to AlexWaygood/cpython that referenced this issue Oct 15, 2024
@AlexWaygood AlexWaygood changed the title Terrible traceback if importlib.reload() is called with a str Messy traceback if importlib.reload() is called with a str Oct 15, 2024
AlexWaygood added a commit that referenced this issue Oct 21, 2024
miss-islington pushed a commit to miss-islington/cpython that referenced this issue 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 issue Oct 21, 2024
…with a non-module object (pythonGH-125520)

(cherry picked from commit c5c21fe)

Co-authored-by: Alex Waygood <[email protected]>
AlexWaygood added a commit that referenced this issue 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 issue 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
3.12 bugs and security fixes 3.13 bugs and security fixes 3.14 new features, bugs and security fixes stdlib Python modules in the Lib dir topic-importlib type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant