diff --git a/src/pynwb/__init__.py b/src/pynwb/__init__.py index c7f327918..3c4ab0bfa 100644 --- a/src/pynwb/__init__.py +++ b/src/pynwb/__init__.py @@ -158,6 +158,10 @@ def load_namespaces(**kwargs): namespace_path = getargs('namespace_path', kwargs) return __TYPE_MAP.load_namespaces(namespace_path) +def available_namespaces(): + """Returns all namespaces registered in the namespace catalog""" + return __NS_CATALOG.namespaces + def _git_cmd(*args) -> subprocess.CompletedProcess: """ @@ -183,9 +187,7 @@ def _clone_submodules(): 'Exception while initializing submodules, got:\n' 'stdout:\n' + ('-'*20) + res.stdout + "\nstderr:\n" + ('-'*20) + res.stderr) else: # pragma: no cover - raise RuntimeError("'core' is not a registered namespace, and pynwb doesn't seem to be installed" - "from a cloned repository so the submodules can't be initialized. " - "Something has gone terribly wrong. maybe try reinstalling???") + raise RuntimeError("Package is not installed from a git repository, can't clone submodules") def _load_core_namespace(final:bool=False): @@ -234,19 +236,20 @@ def _load_core_namespace(final:bool=False): else: try: _clone_submodules() - except (FileNotFoundError, OSError): # pragma: no cover - raise RuntimeError( - "'core' is not a registered namespace. If you installed PyNWB locally using a git clone, you need to " - "use the --recurse_submodules flag when cloning. See developer installation instructions here: " - "https://pynwb.readthedocs.io/en/stable/install_developers.html#install-from-git-repository" - ) + except (FileNotFoundError, OSError, RuntimeError) as e: # pragma: no cover + if 'core' not in available_namespaces(): + warnings.warn( + "'core' is not a registered namespace. If you installed PyNWB locally using a git clone, " + "you need to use the --recurse_submodules flag when cloning. " + "See developer installation instructions here: " + "https://pynwb.readthedocs.io/en/stable/install_developers.html#install-from-git-repository\n" + f"Got exception: \n{e}" + ) if not final: _load_core_namespace(final=True) _load_core_namespace() -def available_namespaces(): - """Returns all namespaces registered in the namespace catalog""" - return __NS_CATALOG.namespaces + # a function to register a container classes with the global map diff --git a/tests/unit/test_import.py b/tests/unit/test_import.py index 39dd728f6..5e82a9201 100644 --- a/tests/unit/test_import.py +++ b/tests/unit/test_import.py @@ -11,6 +11,7 @@ except ImportError: from importlib_resources import files +import pynwb from pynwb.testing import TestCase class TestPyNWBSubmoduleClone(TestCase): @@ -23,9 +24,7 @@ def setUp(self): # remove cache files if they exist self.typemap_cache = Path(files('pynwb') / 'core_typemap.pkl') - self.nscatalog_cache = Path(files('pynwb') / 'core_nscatalog.pkl') self.typemap_cache.unlink(missing_ok=True) - self.nscatalog_cache.unlink(missing_ok=True) def tearDown(self) -> None: @@ -40,11 +39,9 @@ def test_clone_on_import(self): we should try and import it """ assert not self.repo_dir.exists() - assert not self.nscatalog_cache.exists() assert not self.typemap_cache.exists() with self.assertWarns(Warning) as clone_warning: - import pynwb importlib.reload(pynwb) # there can be other warnings, but we should get at least one @@ -53,7 +50,6 @@ def test_clone_on_import(self): assert any(['initializing submodules' in msg for msg in messages]) assert self.repo_dir.exists() - assert self.nscatalog_cache.exists() assert self.typemap_cache.exists() # we should also get the namespaces correctly too @@ -72,5 +68,4 @@ def test_typemap_ns_match(self): The global __NS_CATALOG and the one contained within the global __TYPE_MAP should be the same object after importing """ - import pynwb - assert id(pynwb.__TYPE_MAP.namespace_catalog) == id(pynwb.__NS_CATALOG) \ No newline at end of file + assert id(getattr(pynwb, '__TYPE_MAP').namespace_catalog) == id(getattr(pynwb, '__NS_CATALOG')) \ No newline at end of file