Skip to content

Commit

Permalink
Ensure all relevant allennlp submodules are imported with `import_plu…
Browse files Browse the repository at this point in the history
…gins()` (allenai#5246)

* ensure allennlp is a default plugin

* fix logging issue

* fixes

* actually fix
  • Loading branch information
epwalsh authored and Abhishek-P committed Aug 11, 2021
1 parent b7fd842 commit 38c930b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions allennlp/common/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 6 additions & 2 deletions allennlp/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
TypeVar,
Union,
Sequence,
Set,
)

import numpy
Expand Down Expand Up @@ -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
Expand All @@ -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]:
Expand Down
4 changes: 2 additions & 2 deletions allennlp/tools/archive_surgery.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -79,4 +78,5 @@ def main():


if __name__ == "__main__":
logging.basicConfig(level=logging.ERROR)
main()

0 comments on commit 38c930b

Please sign in to comment.