Skip to content

Commit fc1b6fe

Browse files
authored
fix: Fixes Issue 89 with missing find_module (#91)
* 🎉 Init * complete
1 parent 9c4e37c commit fc1b6fe

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Lazydocs makes it easy to generate beautiful markdown documentation for your Pyt
3838

3939
### Installation
4040

41-
> _Requirements: Python 3.6+._
41+
> _Requirements: Python 3.9+._
4242
4343
```bash
4444
pip install lazydocs

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
EMAIL = "team@mltooling.org"
1616
AUTHOR = "ML Tooling Team"
1717
LICENSE = "MIT"
18-
REQUIRES_PYTHON = ">=3.6"
18+
REQUIRES_PYTHON = ">=3.9"
1919
VERSION = None # Only set version if you like to overwrite the version in _about.py
2020

2121
PWD = os.path.abspath(os.path.dirname(__file__))

src/lazydocs/generation.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,14 @@ def _lines_isvalid(lines: list, start_index: int, blockindent: int,
624624
prev_blank_line_count += 1
625625
return "".join(out)
626626

627+
628+
def get_module(loader, module_name: str) -> Optional[Any]:
629+
spec = loader.find_spec(module_name)
630+
if spec is None:
631+
raise ImportError(f"Cannot find module {module_name}")
632+
return spec.loader.load_module(spec.name)
633+
634+
627635
class MarkdownGenerator(object):
628636
"""Markdown generator class."""
629637

@@ -1250,8 +1258,7 @@ def generate_docs(
12501258
mod = importlib.util.module_from_spec(mod_spec)
12511259
mod_spec.loader.exec_module(mod)
12521260
except AttributeError:
1253-
# For older python version compatibility
1254-
mod = loader.find_module(module_name).load_module(module_name) # type: ignore
1261+
mod = get_module(loader, module_name)
12551262
module_md = generator.module2md(mod, is_mdx=is_mdx, include_toc=include_toc)
12561263
if not module_md:
12571264
# Module md is empty -> ignore module and all submodules
@@ -1334,8 +1341,7 @@ def generate_docs(
13341341
mod = importlib.util.module_from_spec(mod_spec)
13351342
mod_spec.loader.exec_module(mod)
13361343
except AttributeError:
1337-
# For older python version compatibility
1338-
mod = loader.find_module(module_name).load_module(module_name) # type: ignore
1344+
mod = get_module(loader, module_name)
13391345
module_md = generator.module2md(mod, is_mdx=is_mdx, include_toc=include_toc)
13401346

13411347
if not module_md:

tests/test_generation.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import hashlib
22

3-
from lazydocs import MarkdownGenerator
3+
from lazydocs import MarkdownGenerator, generate_docs
4+
from tempfile import TemporaryDirectory
45

56

67
def test_import2md() -> None:
@@ -35,3 +36,31 @@ def test_func2md() -> None:
3536
# Remove whitespaces: fix changes between py version 3.6 3.7 in signature method
3637
md_hash = hashlib.md5(markdown.replace(" ", "").encode("utf-8")).hexdigest()
3738
assert md_hash == "797bad8c00ee6f189cb6f578eaec02c4"
39+
40+
41+
def test_integration_generate_docs(capsys) -> None:
42+
test_class = """
43+
class TestClass:
44+
\"\"\"just a test class\"\"\"
45+
"""
46+
with TemporaryDirectory() as d:
47+
test_module_name = "test_module"
48+
with open(f"{d}/{test_module_name}.py", "w") as f:
49+
f.write(test_class)
50+
51+
overview_file_name = "DOCS.md"
52+
overview_file = f"{d}/output/{overview_file_name}"
53+
generate_docs(
54+
paths=[d],
55+
output_path=f"{d}/output/",
56+
overview_file=overview_file_name
57+
)
58+
59+
captured = capsys.readouterr()
60+
61+
with open(overview_file) as f:
62+
result = f.read()
63+
64+
assert test_module_name in result
65+
assert f"{test_module_name}.TestClass" in result
66+
assert "Failed to generate docs for module" not in captured.out

0 commit comments

Comments
 (0)