Skip to content

Commit

Permalink
feat: Add CLI option to disallow inspection
Browse files Browse the repository at this point in the history
  • Loading branch information
pawamoy committed Apr 19, 2022
1 parent b217eab commit 8f71a07
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
9 changes: 9 additions & 0 deletions src/griffe/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,14 @@ def _load_packages(
resolve_aliases: bool = True,
only_exported: bool = True,
only_known_modules: bool = True,
allow_inspection: bool = True,
):
loader = GriffeLoader(
extensions=extensions,
search_paths=search_paths,
docstring_parser=docstring_parser,
docstring_options=docstring_options,
allow_inspection=allow_inspection,
)
for package in packages:
if not package:
Expand Down Expand Up @@ -244,6 +246,12 @@ def get_parser() -> argparse.ArgumentParser:
action="store_true",
help="Show statistics at the end.",
)
parser.add_argument(
"-X",
"--no-inspection",
action="store_true",
help="Disallow inspection of builtin/compiled/not found modules.",
)

parser.add_argument("packages", metavar="PACKAGE", nargs="+", help="Packages to find and parse.")
return parser
Expand Down Expand Up @@ -300,6 +308,7 @@ def main(args: list[str] | None = None) -> int: # noqa: WPS231
opts.resolve_aliases,
not opts.resolve_implicit,
not opts.resolve_external,
not opts.no_inspection,
)
packages = loader.modules_collection.members

Expand Down
28 changes: 19 additions & 9 deletions src/griffe/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ def __init__(
docstring_options: dict[str, Any] | None = None,
lines_collection: LinesCollection | None = None,
modules_collection: ModulesCollection | None = None,
allow_inspection: bool = True,
) -> None:
"""Initialize the loader.
Expand All @@ -93,6 +94,7 @@ def __init__(
self.docstring_options: dict[str, Any] = docstring_options or {}
self.lines_collection: LinesCollection = lines_collection or LinesCollection()
self.modules_collection: ModulesCollection = modules_collection or ModulesCollection()
self.allow_inspection: bool = allow_inspection
self.finder: ModuleFinder = ModuleFinder(search_paths)
self._time_stats: dict = {
"time_spent_visiting": 0,
Expand Down Expand Up @@ -121,17 +123,23 @@ def load_module(
"""
module_name: str
if module in _builtin_modules:
logger.debug(f"{module} is a builtin module: inspecting")
module_name = module # type: ignore[assignment]
top_module = self._inspect_module(module) # type: ignore[arg-type]
return self._store_and_return(module_name, top_module)

logger.debug(f"{module} is a builtin module")
if self.allow_inspection:
logger.debug(f"Inspecting {module}")
module_name = module # type: ignore[assignment]
top_module = self._inspect_module(module) # type: ignore[arg-type]
return self._store_and_return(module_name, top_module)
raise LoadingError("Cannot load builtin module without inspection")
try:
module_name, package = self.finder.find_spec(module, try_relative_path)
except ModuleNotFoundError:
logger.debug(f"Could not find {module}: trying inspection")
module_name = module # type: ignore[assignment]
top_module = self._inspect_module(module) # type: ignore[arg-type]
logger.debug(f"Could not find {module}")
if self.allow_inspection:
logger.debug(f"Trying inspection on {module}")
module_name = module # type: ignore[assignment]
top_module = self._inspect_module(module) # type: ignore[arg-type]
else:
raise
else:
logger.debug(f"Found {module}: loading")
try: # noqa: WPS505
Expand Down Expand Up @@ -356,8 +364,10 @@ def _load_module_path(
elif module_path.suffix == ".py":
code = module_path.read_text(encoding="utf8")
module = self._visit_module(code, module_name, module_path, parent)
else:
elif self.allow_inspection:
module = self._inspect_module(module_name, module_path, parent)
else:
raise LoadingError("Cannot load compiled module without inspection")
if submodules:
self._load_submodules(module)
return module
Expand Down

0 comments on commit 8f71a07

Please sign in to comment.