Skip to content

Commit

Permalink
refactor: Better handling of import errors and system exits while ins…
Browse files Browse the repository at this point in the history
…pecting modules
  • Loading branch information
pawamoy committed Jan 2, 2022
1 parent 879d91b commit 7ba1589
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/griffe/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,15 +117,18 @@ def _visit_module(self, code: str, module_name: str, module_path: Path, parent:
)

def _inspect_module(self, module_name: str, filepath: Path | None = None, parent: Module | None = None) -> Module:
return inspect(
module_name,
filepath=filepath,
extensions=self.extensions,
parent=parent,
docstring_parser=self.docstring_parser,
docstring_options=self.docstring_options,
lines_collection=self.lines_collection,
)
try:
return inspect(
module_name,
filepath=filepath,
extensions=self.extensions,
parent=parent,
docstring_parser=self.docstring_parser,
docstring_options=self.docstring_options,
lines_collection=self.lines_collection,
)
except SystemExit as error:
raise ImportError(f"Importing '{module_name}' raised a system exit") from error

def _member_parent(self, module: Module, subparts: NamePartsType, subpath: Path) -> Module:
parent_parts = subparts[:-1]
Expand Down Expand Up @@ -289,6 +292,8 @@ def _load_submodule(self, module: Module, subparts: NamePartsType, subpath: Path
except SyntaxError:
message = traceback.format_exc(limit=0).replace("SyntaxError: invalid syntax", "").strip()
logger.error(f"Syntax error: {message}")
except ImportError as error: # noqa: WPS440
logger.error(f"Import error: {error}")


class AsyncGriffeLoader(_BaseGriffeLoader):
Expand Down Expand Up @@ -422,11 +427,17 @@ async def _load_submodule(self, module: Module, subparts: NamePartsType, subpath
try:
member_parent = self._member_parent(module, subparts, subpath)
except UnimportableModuleError as error:
logger.debug(str(error))
else:
logger.warning(f"{error}. Missing __init__ module?")
return
try:
member_parent[subparts[-1]] = await self._load_module_path(
subparts[-1], subpath, submodules=False, parent=member_parent
)
except SyntaxError:
message = traceback.format_exc(limit=0).replace("SyntaxError: invalid syntax", "").strip()
logger.error(f"Syntax error: {message}")
except ImportError as error: # noqa: WPS440
logger.error(f"Import error: {error}")


def _top_name_and_path(
Expand Down

0 comments on commit 7ba1589

Please sign in to comment.