pablogsal · GitHub

The lazy imports filter gets the unresolved module name for relative imports, so lazy from target import VALUE and lazy from .target import VALUE look identical to the callback even though they resolve to different modules. In this repro both filter calls print ('pkg.runner', 'target', ('VALUE',)), so the filter cannot tell the absolute and relative cases apart.

$ tmpdir=$(mktemp -d)
$ mkdir -p "$tmpdir/pkg"
$ cat > "$tmpdir/target.py" <<'PY'
VALUE = 'absolute'
PY
$ cat > "$tmpdir/pkg/__init__.py" <<'PY'
PY
$ cat > "$tmpdir/pkg/target.py" <<'PY'
VALUE = 'relative'
PY
$ cat > "$tmpdir/pkg/runner.py" <<'PY'
import sys
def my_filter(importer, name, fromlist):
    print((importer, name, fromlist))
    return True
sys.set_lazy_imports_filter(my_filter)
lazy from target import VALUE as absolute_value
lazy from .target import VALUE as relative_value
PY
$ PYTHONPATH="$tmpdir" ./python -c 'import pkg.runner'
('pkg.runner', 'target', ('VALUE',))
('pkg.runner', 'target', ('VALUE',))

Linked PRs

Read the original on github.com ↗