Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import capturer fails to error cleanly on submodule imports without 'asname' values. #28

Closed
DavidCEllis opened this issue Nov 14, 2024 · 0 comments · Fixed by #29
Closed

Comments

@DavidCEllis
Copy link
Owner

Currently in the tracing back, a submodule import without an assigned name will trigger a keyerror as it fails to be traced backwards.

from ducktools.lazyimporter import LazyImporter
from ducktools.lazyimporter.capture import capture_imports

laz = LazyImporter()

with capture_imports(laz, auto_export=False):
    import unittest.mock

Errors as:

Traceback (most recent call last):
  File "C:\Users\ducks\Source\ducktools-lazyimporter\scratch\example.py", line 6, in <module>
    with capture_imports(laz, auto_export=False):
         ~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\ducks\Source\ducktools-lazyimporter\src\ducktools\lazyimporter\capture.py", line 236, in __exit__
    capture = importer_map[attrib_name]
              ~~~~~~~~~~~~^^^^^^^^^^^^^
KeyError: 'unittest'

This is because the name in the global namespace isn't the submodule, it's the initial parent import.
The importer map is constructed based on the final element, so an "as" import can work backwards to the original import.

If the imports are reversed, there is currently no error - but the submodule doesn't work as the module import overwrote the submodule import in the globals namespace.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant