Skip to content

Resolve pkg:// search paths without importing packages - #78

Merged
python-templates-automerge[bot] merged 2 commits into
mainfrom
pkg-paths-without-import
Oct 11, 2026
Merged

python-templates-automerge[bot] merged 2 commits into
mainfrom
pkg-paths-without-import

Conversation

@timkpaine

Copy link
Copy Markdown
Member

Every pkg:// search path registered through hydra.lernaplugins was resolved with importlib.util.find_spec (plugin discovery) and importlib.resources.files (config source checks and listings). Both import the package and run every parent __init__.py, so an app paid for importing every installed plugin on startup, even ones it never used. On my machine, with 14 plugins installed, composing a one-line config took 2.45s and imported ccflow along with most of the plugin packages.

pkg_helper.find_package_dir now locates a regular package's directory by walking the import path in the same order the path finder uses: a regular package, then a module, then namespace portions. Already-imported parents use their real __path__. It returns None whenever a walk can't match what an import would do (zip imports, namespace leaf packages, already-imported packages, modules shadowing packages, missing packages), and callers then use importlib as before.

  • _is_pkg_path_available and ImportlibResourcesConfigSource.available/is_group/is_config/list use the walk, as do the pkg_config_exists, pkg_group_exists and pkg_list_options callbacks used by the Rust repository.
  • ImportlibResourcesConfigSource.load_config and load_pkg_config still call importlib.resources.files. A package whose configs are actually read is imported as before, so import-time side effects such as ConfigStore registrations still run before its configs are used.

The same compose now takes 0.24s and imports none of the plugin packages. test_pkg_paths.py covers parents with an __init__ that raises, namespace parents, an extended __path__, the fallback cases including a zip import, and that reading a config still imports the package. The full suite has the same 147 failures as main locally (shell-completion expect scripts, completion tests that see installed plugins' groups, and fixture pip install in an externally managed Python), with no new ones.

Plugin discovery and config lookups called importlib.util.find_spec and importlib.resources.files, which run every parent __init__ of each registered pkg:// path. Any app with a few plugins installed paid for importing all of them on startup. Existence checks and listings now locate the package directory by walking the import path, and fall back to importlib for zip imports, namespace packages, already-imported packages and modules shadowing packages. Reading a config still uses importlib.resources, so a package whose configs are used is imported as before.

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
ruff 0.17 no longer enables DTZ005 by default, so the suppression trips RUF100 and fails lint-py.

Signed-off-by: Tim Paine <3105306+timkpaine@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown
Contributor

Test Results

3 311 tests  +8   3 099 ✅ +8   5m 29s ⏱️ - 2m 23s
    2 suites ±0     212 💤 ±0 
    2 files   ±0       0 ❌ ±0 

Results for commit f886836. ± Comparison against base commit 06c5658.

@codecov

codecov Bot commented Oct 11, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.41379% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
lerna/_internal/core_plugins/pkg_helper.py 95.55% 2 Missing ⚠️
lerna/core/plugins.py 66.66% 1 Missing ⚠️
Files with missing lines Coverage Δ
.../core_plugins/importlib_resources_config_source.py 85.41% <100.00%> (+0.15%) ⬆️
lerna/core/utils.py 58.85% <ø> (ø)
lerna/tests/test_pkg_paths.py 100.00% <100.00%> (ø)
lerna/core/plugins.py 56.33% <66.66%> (+0.13%) ⬆️
lerna/_internal/core_plugins/pkg_helper.py 81.52% <95.55%> (+14.85%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@timkpaine timkpaine added the tag: automerge Automatically merge this PR when status checks pass label Oct 11, 2026
@python-templates-automerge
python-templates-automerge Bot merged commit 70d2d4c into main Oct 11, 2026
6 checks passed
@python-templates-automerge
python-templates-automerge Bot deleted the pkg-paths-without-import branch October 11, 2026 19:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

tag: automerge Automatically merge this PR when status checks pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant