From 92905f99d8904acc48dfac71986fd0c872744bb0 Mon Sep 17 00:00:00 2001 From: mgorsk1 Date: Tue, 26 Aug 2025 20:10:24 +0200 Subject: [PATCH 1/2] :tada: Init --- README.md | 2 +- setup.py | 2 +- src/lazydocs/generation.py | 15 +++++++++++---- tests/test_generation.py | 26 +++++++++++++++++++++++++- 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 73ffc38..882c082 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,7 @@ Lazydocs makes it easy to generate beautiful markdown documentation for your Pyt ### Installation -> _Requirements: Python 3.6+._ +> _Requirements: Python 3.9+._ ```bash pip install lazydocs diff --git a/setup.py b/setup.py index b036aa2..332fb4b 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,7 @@ EMAIL = "team@mltooling.org" AUTHOR = "ML Tooling Team" LICENSE = "MIT" -REQUIRES_PYTHON = ">=3.6" +REQUIRES_PYTHON = ">=3.9" VERSION = None # Only set version if you like to overwrite the version in _about.py PWD = os.path.abspath(os.path.dirname(__file__)) diff --git a/src/lazydocs/generation.py b/src/lazydocs/generation.py index e773445..04e0806 100755 --- a/src/lazydocs/generation.py +++ b/src/lazydocs/generation.py @@ -624,6 +624,15 @@ def _lines_isvalid(lines: list, start_index: int, blockindent: int, prev_blank_line_count += 1 return "".join(out) + +def get_module(loader, module_name: str) -> Optional[Any]: + spec = loader.find_spec(module_name) + if spec is None: + raise ImportError(f"Cannot find module {module_name}") + + return importlib.util.module_from_spec(spec) + + class MarkdownGenerator(object): """Markdown generator class.""" @@ -1250,8 +1259,7 @@ def generate_docs( mod = importlib.util.module_from_spec(mod_spec) mod_spec.loader.exec_module(mod) except AttributeError: - # For older python version compatibility - mod = loader.find_module(module_name).load_module(module_name) # type: ignore + mod = get_module(loader, module_name) module_md = generator.module2md(mod, is_mdx=is_mdx, include_toc=include_toc) if not module_md: # Module md is empty -> ignore module and all submodules @@ -1334,8 +1342,7 @@ def generate_docs( mod = importlib.util.module_from_spec(mod_spec) mod_spec.loader.exec_module(mod) except AttributeError: - # For older python version compatibility - mod = loader.find_module(module_name).load_module(module_name) # type: ignore + mod = get_module(loader, module_name) module_md = generator.module2md(mod, is_mdx=is_mdx, include_toc=include_toc) if not module_md: diff --git a/tests/test_generation.py b/tests/test_generation.py index c53f489..51571b3 100644 --- a/tests/test_generation.py +++ b/tests/test_generation.py @@ -1,6 +1,7 @@ import hashlib -from lazydocs import MarkdownGenerator +from lazydocs import MarkdownGenerator, generate_docs +from tempfile import TemporaryDirectory def test_import2md() -> None: @@ -35,3 +36,26 @@ def test_func2md() -> None: # Remove whitespaces: fix changes between py version 3.6 3.7 in signature method md_hash = hashlib.md5(markdown.replace(" ", "").encode("utf-8")).hexdigest() assert md_hash == "797bad8c00ee6f189cb6f578eaec02c4" + + +def test_integration_generate_docs(capsys) -> None: + with TemporaryDirectory() as d: + test_module_name = "test_module" + with open(f"{d}/{test_module_name}.py", "w") as f: + f.write("") + + overview_file_name = "DOCS.md" + overview_file = f"{d}/output/{overview_file_name}" + generate_docs( + paths=[d], + output_path=f"{d}/output/", + overview_file=overview_file_name + ) + + captured = capsys.readouterr() + + with open(overview_file) as f: + result = f.read() + + assert test_module_name in result + assert "Failed to generate docs for module" not in captured.out From 550e7d2c37bed2328d2d99d8f0fc010c1f509fec Mon Sep 17 00:00:00 2001 From: mgorsk1 Date: Tue, 26 Aug 2025 20:26:27 +0200 Subject: [PATCH 2/2] complete --- src/lazydocs/generation.py | 3 +-- tests/test_generation.py | 7 ++++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lazydocs/generation.py b/src/lazydocs/generation.py index 04e0806..7013dd9 100755 --- a/src/lazydocs/generation.py +++ b/src/lazydocs/generation.py @@ -629,8 +629,7 @@ def get_module(loader, module_name: str) -> Optional[Any]: spec = loader.find_spec(module_name) if spec is None: raise ImportError(f"Cannot find module {module_name}") - - return importlib.util.module_from_spec(spec) + return spec.loader.load_module(spec.name) class MarkdownGenerator(object): diff --git a/tests/test_generation.py b/tests/test_generation.py index 51571b3..1c81d9e 100644 --- a/tests/test_generation.py +++ b/tests/test_generation.py @@ -39,10 +39,14 @@ def test_func2md() -> None: def test_integration_generate_docs(capsys) -> None: + test_class = """ +class TestClass: + \"\"\"just a test class\"\"\" + """ with TemporaryDirectory() as d: test_module_name = "test_module" with open(f"{d}/{test_module_name}.py", "w") as f: - f.write("") + f.write(test_class) overview_file_name = "DOCS.md" overview_file = f"{d}/output/{overview_file_name}" @@ -58,4 +62,5 @@ def test_integration_generate_docs(capsys) -> None: result = f.read() assert test_module_name in result + assert f"{test_module_name}.TestClass" in result assert "Failed to generate docs for module" not in captured.out