brandtbucher · GitHub

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.

Linked PRs

Read the original on github.com ↗