Skip to content

Unexpected module import when using inspect #24

@jeremander

Description

@jeremander

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__.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions