Skip to content

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

Closed

Description

@AlexWaygood

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

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    stdlibStandard Library Python modules in the Lib/ directory
    on Oct 15, 2024
  2. self-assigned this
    on Oct 15, 2024
  3. added
    3.12only security fixes
    3.13only security fixes
    3.14bugs and security fixes
    on Oct 15, 2024
  4. added a commit that references this issue on Oct 15, 2024
  5. changed the title [-]Terrible traceback if `importlib.reload()` is called with a `str`[/-] [+]Messy traceback if `importlib.reload()` is called with a `str`[/+] on Oct 15, 2024
  6. added a commit that references this issue on Oct 21, 2024
  7. added 2 commits that reference this issue on Oct 21, 2024
  8. added 2 commits that reference this issue on Oct 21, 2024
  9. added a commit that references this issue on Jan 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

3.12only security fixes3.13only security fixes3.14bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-importlibtype-bugAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions