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

Unexpected module import when using inspect #24

Open
jeremander opened this issue May 27, 2022 · 0 comments
Open

Unexpected module import when using inspect #24

jeremander opened this issue May 27, 2022 · 0 comments

Comments

@jeremander
Copy link

Ordinarily, accessing attributes on a LazyModule triggers the import, but sometimes this can happen unexpectedly. One such case happens with the __file__ attribute. Here is a minimal example.

fail.py

raise ValueError('Boom!')

script.py

import inspect
from lazy_import import lazy_module

fail = lazy_module('fail')
frame = inspect.currentframe()
mod = inspect.getmodule(frame)

Running the above script triggers the import (and hence, the error). This results from inspect checking the __file__ attribute of every module in sys.modules, including fail. The behavior is undesirable since the user did not intentionally access any attribute of fail.

To fix this, one could simply add '__file__' to the list of exemptions in LazyModule.__getattribute__.

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

No branches or pull requests

1 participant