Skip to content

Fully single-module lazy imports are not recorded in sys.lazy_modules #145059

@johnslavik

Description

@johnslavik

Bug report

Bug description:

Current:

>>> lazy import foo
>>> lazy import spam.eggs
>>> lazy from fourty import two
>>>
>>> import sys
>>> sys.lazy_modules
{'spam': {'eggs'}, 'fourty': {'two'}}

Expected either:

>>> sys.lazy_modules
{'foo': set(), 'spam': {'eggs'}, 'fourty': {'two'}}

or (I like this less because it makes the value type a union set[str] | None, and unions often necessitate type narrowing):

>>> sys.lazy_modules
{'foo': None, 'spam': {'eggs'}, 'fourty': {'two'}}

The PEP (though not 100% up-to-date on sys.lazy_modules, see GH-145057) describes sys.lazy_modules as "fully-qualified module names which have been lazily imported at some point (primarily for diagnostics and introspection)".

It believe that the simplest form of a lazy import, i.e. one that has no submodules/members, should as well be recorded to reflect that intent.

cc @pablogsal @DinoV @brittanyrey @Yhg1s
Please lmk if and which suggestion is desired behavior or if we would rather keep the current behavior as is.

Happy to work on this one too! (Re-assign if you'd like to take over)

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Metadata

Metadata

Assignees

Labels

3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions