Skip to content

__lazy_import__ with a string fromlist doesn't resolve to the module being imported #151672

@brandtbucher

Description

@brandtbucher

Bug report

fromlist can be either a None, a string, or a tuple of strings. In all cases, __import__ returns the module being imported:

>>> __import__("json", fromlist=None)
<module 'json' from '.../json/__init__.py'>
>>> __import__("json", fromlist="load")
<module 'json' from '.../json/__init__.py'>
>>> __import__("json", fromlist=("load", "dump"))
<module 'json' from '.../json/__init__.py'>

__lazy_import__ behaves exactly the same way, except when the fromlist is a string. In this case, it resolves to the member being imported instead:

>>> __lazy_import__("json", fromlist=None).resolve()
<module 'json' from '.../json/__init__.py'>
>>> __lazy_import__("json", fromlist="load").resolve()  # Different!
<function load at 0x106635430>
>>> __lazy_import__("json", fromlist=("load", "dump")).resolve()
<module 'json' from '.../json/__init__.py'>

This feels like a bug. The PR to fix this is pretty simple (wrap fromlist in a tuple in _PyImport_LazyImportModuleLevelObject when it's a string), just wanted to double-check that this is indeed the expected behavior before opening it.

Metadata

Metadata

Assignees

Labels

3.15pre-release feature fixes, bugs and security fixes3.16new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-lazy-importstype-bugAn unexpected behavior, bug, or error
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions