diff --git a/CHANGELOG.md b/CHANGELOG.md index aaf1c2d633a..684ec6e0081 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed Broken link in `allennlp.fairness.fairness_metrics.Separation` docs +- Ensured all `allennlp` submodules are imported with `allennlp.common.plugins.import_plugins()`. ## [v2.5.0](https://github.com/allenai/allennlp/releases/tag/v2.5.0) - 2021-06-03 diff --git a/allennlp/common/plugins.py b/allennlp/common/plugins.py index e114631f3ab..21cc694ba95 100644 --- a/allennlp/common/plugins.py +++ b/allennlp/common/plugins.py @@ -75,6 +75,14 @@ def import_plugins() -> None: """ Imports the plugins found with `discover_plugins()`. """ + # Ensure all relevant submodules of AllenNLP are imported. + import_module_and_submodules( + "allennlp", + exclude={ + "allennlp.sanity_checks", # deprecated + "allennlp.tools", # things in here are usually run as commands themselves + }, + ) # Workaround for a presumed Python issue where spawned processes can't find modules in the current directory. cwd = os.getcwd() diff --git a/allennlp/common/util.py b/allennlp/common/util.py index 4db2ef6b5fe..b4eba865195 100644 --- a/allennlp/common/util.py +++ b/allennlp/common/util.py @@ -28,6 +28,7 @@ TypeVar, Union, Sequence, + Set, ) import numpy @@ -328,13 +329,16 @@ def push_python_path(path: PathType) -> ContextManagerFunctionReturnType[None]: sys.path.remove(path) -def import_module_and_submodules(package_name: str) -> None: +def import_module_and_submodules(package_name: str, exclude: Optional[Set[str]] = None) -> None: """ Import all submodules under the given package. Primarily useful so that people using AllenNLP as a library can specify their own custom packages and have their custom classes get loaded and registered. """ + if exclude and package_name in exclude: + return + importlib.invalidate_caches() # For some reason, python doesn't always add this by default to your path, but you pretty much @@ -353,7 +357,7 @@ def import_module_and_submodules(package_name: str) -> None: if path_string and module_finder.path != path_string: # type: ignore[union-attr] continue subpackage = f"{package_name}.{name}" - import_module_and_submodules(subpackage) + import_module_and_submodules(subpackage, exclude=exclude) def peak_cpu_memory() -> Dict[int, int]: diff --git a/allennlp/tools/archive_surgery.py b/allennlp/tools/archive_surgery.py index fc1014d23fc..3cba3f57169 100644 --- a/allennlp/tools/archive_surgery.py +++ b/allennlp/tools/archive_surgery.py @@ -22,8 +22,7 @@ from allennlp.common.file_utils import cached_path from allennlp.models.archival import CONFIG_NAME -logger = logging.getLogger() -logger.setLevel(logging.ERROR) +logger = logging.getLogger(__name__) def main(): @@ -79,4 +78,5 @@ def main(): if __name__ == "__main__": + logging.basicConfig(level=logging.ERROR) main()