Skip to content

Commit

Permalink
terminate on most import errors, fix #407
Browse files Browse the repository at this point in the history
  • Loading branch information
mhils committed Aug 11, 2022
1 parent ad6a7b4 commit cdce38f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
- Fix display of `@classmethod @property` instances without docstrings. ([@mhils](https://github.com/mhils))
- Add support for `@functools.singledispatchmethod`.
([#428](https://github.com/mitmproxy/pdoc/issues/428), [@mhils](https://github.com/mhils))
- pdoc now terminates if a module cannot be imported instead of raising a warning.
([#407](https://github.com/mitmproxy/pdoc/issues/407), [@mhils](https://github.com/mhils))

# 2022-06-08: pdoc 12.0.2

Expand Down
10 changes: 1 addition & 9 deletions pdoc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,6 @@ def bark(self, loud: bool) -> None:
__docformat__ = "markdown" # explicitly disable rST processing in the examples above.
__version__ = "12.0.2" # this is read from setup.py

import traceback
import warnings
from pathlib import Path
from typing import overload

Expand Down Expand Up @@ -503,13 +501,7 @@ def pdoc(

all_modules: dict[str, doc.Module] = {}
for module_name in extract.walk_specs(modules):
try:
all_modules[module_name] = doc.Module.from_name(module_name)
except RuntimeError:
warnings.warn(f"Error importing {module_name}:\n{traceback.format_exc()}")

if not all_modules:
raise RuntimeError("Unable to import any modules.")
all_modules[module_name] = doc.Module.from_name(module_name)

for module in all_modules.values():
if format == "html":
Expand Down
13 changes: 13 additions & 0 deletions test/test_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,16 @@ def __doc__(self):
x = Class(Foo.__module__, Foo.__qualname__, Foo, (Foo.__module__, Foo.__qualname__))
with pytest.warns(UserWarning, match="inspect.getdoc(.+) raised an exception"):
assert x.docstring == ""


def test_raising_submodules():
f = here / "syntax_err" / "syntax_err.py"
f.write_bytes(b"class")

try:
extract.parse_spec(f.parent)
m = Module.from_name("test.syntax_err")
with pytest.warns(UserWarning, match="Error importing"):
assert m.submodules
finally:
f.write_bytes(b"# syntax error will be inserted by test here\n")
7 changes: 3 additions & 4 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,14 @@ def test_api(tmp_path):
with pytest.warns(UserWarning, match="Cannot find spec"):
pdoc(here / "notfound.py")

with pytest.raises(RuntimeError, match="Unable to import any modules."):
with pytest.warns(UserWarning, match="Error importing"):
pdoc(here / "testdata" / "import_err_simple.py")
with pytest.raises(RuntimeError, match="Error importing"):
pdoc(here / "testdata" / "import_err_simple.py")

# temporarily insert syntax error - we don't leave it permanently to not confuse mypy, flake8 and black.
f = here / "syntax_err" / "syntax_err.py"
f.write_bytes(b"class")
try:
with pytest.warns(UserWarning, match="Error importing"):
with pytest.raises(RuntimeError, match="Error importing"):
pdoc(here / "syntax_err", output_directory=tmp_path)
finally:
f.write_bytes(b"# syntax error will be inserted by test here\n")
Expand Down

0 comments on commit cdce38f

Please sign in to comment.