Add subscript notation to packages (#48467)

This PR allows using the subscript notation directly on packages. The
intent is to reduce the boilerplate needed to retrieve package
properties from nodes other than root.
This commit is contained in:
Massimiliano Culpo
2025-01-10 19:00:51 +01:00
committed by GitHub
parent b670205e54
commit dd69b646ad
18 changed files with 44 additions and 37 deletions

View File

@@ -767,6 +767,9 @@ def __init__(self, spec):
self.win_rpath = fsys.WindowsSimulatedRPath(self)
super().__init__()
def __getitem__(self, key: str) -> "PackageBase":
return self.spec[key].package
@classmethod
def dependency_names(cls):
return _subkeys(cls.dependencies)

View File

@@ -285,3 +285,16 @@ def compilers(compiler, arch_spec):
error = capfd.readouterr()[1]
assert "Skipping tests for package" in error
assert "test requires missing compiler" in error
def test_package_subscript(default_mock_concretization):
"""Tests that we can use the subscript notation on packages, and that it returns a package"""
root = default_mock_concretization("mpileaks")
root_pkg = root.package
# Subscript of a virtual
assert isinstance(root_pkg["mpi"], spack.package_base.PackageBase)
# Subscript on concrete
for d in root.traverse():
assert isinstance(root_pkg[d.name], spack.package_base.PackageBase)