Skip to content

Reloading a lazy module that has not yet been materailized should be no-op #139686

Open

Description

@QEDady

Bug report

Bug description:

Reloading a not-yet materialized lazy module is not supported well. It makes sense for the reloading in this case to be no-op since when the actual loading happens e.g. by accessing an attribute, the new code will be loaded anyway. Currently, the reload tries to reload the module while its loaded is lazy in the first place.

CPython versions tested on:

3.13, 3.12, 3.14, CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Oct 7, 2025
  2. ericvsmith commented on Oct 8, 2025

    @ericvsmith
    Member

    Please show some example code. Show how it works, and what you expect the behavior to be.

  3. QEDady commented on Oct 9, 2025

    @QEDady
    ContributorAuthor

    There is no way to surface the behaviour with the public API. Nevertheless, the private API for _LazyModule should be compatible with importlib.reload. I sent a fix in this PR

  4. added 2 commits that reference this issue on Dec 12, 2025
  5. encukou commented on Jan 8, 2026

    @encukou
    Member

    GH-139857 broke* refleak buildbots -- tests fail when executed twice:

    $ ./python -m test -v test_importlib test_importlib -m 'LazyLoaderTests'
    [...]
    ======================================================================
    ERROR: test_attr_unchanged (test.test_importlib.test_lazy.LazyLoaderTests.test_attr_unchanged)
    ----------------------------------------------------------------------
    Traceback (most recent call last):
      File "/home/encukou/dev/cpython/Lib/test/test_importlib/test_lazy.py", line 105, in test_attr_unchanged
        self.assertEqual(TestingImporter.mutated_name, module.__name__)
                                                       ^^^^^^^^^^^^^^^
      File "/home/encukou/dev/cpython/Lib/importlib/util.py", line 213, in __getattribute__
        raise ValueError(f"module object for {original_name!r} "
                          "substituted in sys.modules during a lazy "
                          "load")
    ValueError: module object for 'lazy_loader_test' substituted in sys.modules during a lazy load
    
    
    • similar errors for test_delete_eventual_attr and test_delete_preexisting_attr.

    * [edit] The tests pass again when re-run, so buildbot status is “warn” rather that “fail”.

  6. self-assigned this
    on Jan 8, 2026
  7. brettcannon commented on Jan 8, 2026

    @brettcannon
    Member

    I'll revert the change today to fix the buildbots.

  8. added a commit that references this issue on Jan 8, 2026
  9. brettcannon commented on Jan 8, 2026

    @brettcannon
    Member

    I have a revert PR out and set to auto-merge.

    And now that PEP 810 has been accepted, I don't know how important it is to try again to get the functionality in or if it would be better on PyPI, maybe with #127036 .

  10. added a commit that references this issue on Jan 8, 2026
  11. removed their assignment
    on Jan 8, 2026
  12. added a commit that references this issue on Jan 12, 2026
  13. added a commit that references this issue on Feb 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard 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