-
-
Notifications
You must be signed in to change notification settings - Fork 34.4k
lazy imports filter sees the same thing for from x import y and from .x import y #148110
Copy link
Copy link
Open
Open
Copy link
Labels
3.15new features, bugs and security fixesnew features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-lazy-importstype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
3.15new features, bugs and security fixesnew features, bugs and security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-lazy-importstype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error