Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: pass loaders to replace_docstring for parser options #365

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions quartodoc/autosummary.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def _resolve_target(obj: dc.Alias):
return target


def replace_docstring(obj: dc.Object | dc.Alias, f=None):
def replace_docstring(obj: dc.Object | dc.Alias, f=None, loader=None):
"""Replace (in place) a docstring for a griffe object.

Parameters
Expand All @@ -200,6 +200,9 @@ def replace_docstring(obj: dc.Object | dc.Alias, f=None):
"""
import importlib

if loader is None:
raise NotImplementedError("Loader may not be None.")

if isinstance(obj, dc.Alias):
obj = _resolve_target(obj)

Expand All @@ -208,7 +211,7 @@ def replace_docstring(obj: dc.Object | dc.Alias, f=None):
# also have the effect of updating the class docstring.
if isinstance(obj, dc.Class):
for child_obj in obj.members.values():
replace_docstring(child_obj)
replace_docstring(child_obj, loader=loader)

if f is None:
mod = importlib.import_module(obj.module.canonical_path)
Expand Down Expand Up @@ -237,8 +240,8 @@ def replace_docstring(obj: dc.Object | dc.Alias, f=None):
lineno=getattr(old, "lineno", None),
endlineno=getattr(old, "endlineno", None),
parent=getattr(old, "parent", None),
parser=getattr(old, "parser", None),
parser_options=getattr(old, "parser_options", None),
parser=loader.docstring_parser,
parser_options=loader.docstring_options,
)

obj.docstring = new
Expand Down Expand Up @@ -344,7 +347,7 @@ def dynamic_alias(
obj = get_object(canonical_path, loader=loader)

# use dynamically imported object's docstring
replace_docstring(obj, attr)
replace_docstring(obj, attr, loader=loader)

if obj.canonical_path == path.replace(":", "."):
return obj
Expand Down
18 changes: 17 additions & 1 deletion quartodoc/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,26 @@ def test_replace_docstring():
from quartodoc.autosummary import get_object, replace_docstring
from quartodoc.tests.example_dynamic import f

# TODO: expose a default loader factory, etc..
from quartodoc._griffe_compat import (
GriffeLoader,
Parser,
ModulesCollection,
LinesCollection,
)
from quartodoc.parsers import get_parser_defaults

loader = GriffeLoader(
docstring_parser=Parser("numpy"),
docstring_options=get_parser_defaults("numpy"),
modules_collection=ModulesCollection(),
lines_collection=LinesCollection(),
)

obj = get_object("quartodoc", "tests.example_dynamic.f")
old = obj.docstring

replace_docstring(obj, f)
replace_docstring(obj, f, loader=loader)
assert obj.docstring is not old

# just check the end of the piece dynamically added to docstring, since
Expand Down
Loading