diff --git a/lib/spack/spack/repo.py b/lib/spack/spack/repo.py index a2ebf9f53a..b735866321 100644 --- a/lib/spack/spack/repo.py +++ b/lib/spack/spack/repo.py @@ -118,7 +118,11 @@ def __init__(self, namespace): def __getattr__(self, name): """Getattr lazily loads modules if they're not already loaded.""" submodule = self.__package__ + '.' + name - setattr(self, name, __import__(submodule)) + try: + setattr(self, name, __import__(submodule)) + except ImportError: + msg = "'{0}' object has no attribute {1}" + raise AttributeError(msg.format(type(self), name)) return getattr(self, name) diff --git a/lib/spack/spack/test/repo.py b/lib/spack/spack/test/repo.py index 1a4331af63..df3015500d 100644 --- a/lib/spack/spack/test/repo.py +++ b/lib/spack/spack/test/repo.py @@ -67,3 +67,15 @@ def test_repo_invisibles(mutable_mock_repo, extra_repo): with open(os.path.join(extra_repo.root, 'packages', '.invisible'), 'w'): pass extra_repo.all_package_names() + + +@pytest.mark.parametrize('attr_name,exists', [ + ('cmake', True), + ('__sphinx_mock__', False) +]) +@pytest.mark.regression('20661') +def test_namespace_hasattr(attr_name, exists, mutable_mock_repo): + # Check that we don't fail on 'hasattr' checks because + # of a custom __getattr__ implementation + nms = spack.repo.SpackNamespace('spack.pkg.builtin.mock') + assert hasattr(nms, attr_name) == exists