Skip to content

test_lazy_import: add coverage for reload() and sys.modules deletion after reification #149013

@XiaoWan-zi

Description

@XiaoWan-zi

Bug report / test coverage gap

Lib/test/test_lazy_import/__init__.py has broad lazy import coverage, but it misses two practical state-transition scenarios after reification:

  1. importlib.reload() on a module that was first imported lazily and then reified.
  2. Deleting sys.modules[name] after reification and then importing again.

Why this matters

These are common import-state operations and can regress subtly when lazy import behavior changes.
Adding tests helps lock in expected semantics and prevents regressions in future import machinery refactors.

Proposed change

Add two tests in test_lazy_import:

  • test_reload_after_lazy_reification_keeps_single_module_object

    • Reify a lazily imported module.
    • Call importlib.reload().
    • Assert identity consistency between the binding and sys.modules[name].
  • test_reimport_after_deleting_sys_modules_entry_creates_new_module

    • Reify a lazily imported module.
    • Delete sys.modules[name].
    • Verify old binding still works.
    • Re-import module and assert a new module object is cached.

Affected files

  • Lib/test/test_lazy_import/__init__.py

Version

  • main (3.15 development branch)

Additional notes

This is test-only; no runtime behavior change is introduced.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15new features, bugs and security fixestestsTests in the Lib/test dirtopic-lazy-importstype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions