From 5d9e38213468d1398f964068bc0e369b2263cf34 Mon Sep 17 00:00:00 2001 From: David Selassie Date: Wed, 31 Jul 2024 15:37:55 -0700 Subject: [PATCH 1/3] Support documenting namespace modules --- src/autodoc2/analysis.py | 7 +++++-- src/autodoc2/sphinx/extension.py | 3 ++- src/autodoc2/utils.py | 7 +++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/autodoc2/analysis.py b/src/autodoc2/analysis.py index f84443e..6fa3c67 100644 --- a/src/autodoc2/analysis.py +++ b/src/autodoc2/analysis.py @@ -14,7 +14,7 @@ import typing as t from astroid import nodes -from astroid.builder import AstroidBuilder +from astroid.builder import AstroidBuilder, build_namespace_package_module from . import astroid_utils @@ -38,7 +38,10 @@ def analyse_module( you can use this to record them. """ # TODO expose record_external_imports everywhere analyse_module is used - node = AstroidBuilder().file_build(os.fsdecode(file_path), name) + if not file_path.is_dir(): + node = AstroidBuilder().file_build(os.fsdecode(file_path), name) + else: + node = build_namespace_package_module(name, file_path.parts) yield from walk_node( node, State(node.name.split(".", 1)[0], [], exclude_external_imports) ) diff --git a/src/autodoc2/sphinx/extension.py b/src/autodoc2/sphinx/extension.py index 50ad969..a20ffc5 100644 --- a/src/autodoc2/sphinx/extension.py +++ b/src/autodoc2/sphinx/extension.py @@ -140,7 +140,8 @@ def run_autodoc_package(app: Sphinx, config: Config, pkg_index: int) -> str | No # so we can check if we need to re-analyse them hasher = hashlib.sha256() for mod_path, _ in sorted(modules): - hasher.update(mod_path.read_bytes()) + if mod_path.is_file(): + hasher.update(mod_path.read_bytes()) hash_str = hasher.hexdigest() if ( diff --git a/src/autodoc2/utils.py b/src/autodoc2/utils.py index be56bdd..942670e 100644 --- a/src/autodoc2/utils.py +++ b/src/autodoc2/utils.py @@ -108,8 +108,15 @@ def _suffix_sort_key(s: str) -> int: name, suffix = os.path.splitext(filename) # noqa: PTH122 if suffix in extensions: to_yield.setdefault(name, []).append(suffix) + root_path = Path(root) rel_mod = root_path.relative_to(folder).parts + + # This is a namespace module. + if "__init__.py" not in filenames: + yield (root_path, ".".join([*root_mod, *rel_mod])) + # Otherwise, the specific `__init__.py` file will be included + # below. for name, suffixes in to_yield.items(): suffix = sorted(suffixes, key=_suffix_sort_key)[0] yield (root_path / f"{name}{suffix}", ".".join([*root_mod, *rel_mod, name])) From 564b7169da59bbe154deb380b99c4ffae72dcc60 Mon Sep 17 00:00:00 2001 From: Sergio Oller Moreno Date: Sun, 22 Sep 2024 08:58:01 +0200 Subject: [PATCH 2/3] Fix TypeError when docutils can't provide a line --- src/autodoc2/sphinx/docstring.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/autodoc2/sphinx/docstring.py b/src/autodoc2/sphinx/docstring.py index 9508db8..9461af1 100644 --- a/src/autodoc2/sphinx/docstring.py +++ b/src/autodoc2/sphinx/docstring.py @@ -140,7 +140,7 @@ def run(self) -> list[nodes.Node]: ) document.reporter.get_source_and_line = lambda li: ( source_path, - li + source_offset, + li + source_offset if li is not None else None, ) with parsing_context(): parser.parse(item["doc"], document) @@ -215,7 +215,7 @@ def change_source( state.reporter.source = source_path state.reporter.get_source_and_line = lambda li: ( source_path, - li + line_offset, + li + line_offset if li is not None else None, ) yield finally: From a33a5946489cc124a4b49425f424224040c74a2b Mon Sep 17 00:00:00 2001 From: Sergio Oller Date: Sat, 28 Sep 2024 10:03:43 +0200 Subject: [PATCH 3/3] Give yield_module what it expects Otherwise I get File "", line 152, in _check_arg_types TypeError: join() argument must be str, bytes, or os.PathLike object, not 'tuple' When I rerun sphinx-build --- src/autodoc2/analysis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autodoc2/analysis.py b/src/autodoc2/analysis.py index 6fa3c67..738004a 100644 --- a/src/autodoc2/analysis.py +++ b/src/autodoc2/analysis.py @@ -41,7 +41,7 @@ def analyse_module( if not file_path.is_dir(): node = AstroidBuilder().file_build(os.fsdecode(file_path), name) else: - node = build_namespace_package_module(name, file_path.parts) + node = build_namespace_package_module(name, [str(file_path)]) yield from walk_node( node, State(node.name.split(".", 1)[0], [], exclude_external_imports) )