Skip to content

lazy imports filter sees the same thing for from x import y and from .x import y #148110

@pablogsal

Description

@pablogsal

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.15new features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-lazy-importstype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions