Skip to content

Commit 81ce4c5

Browse files
authored
Merge pull request #231 from machow/fix-dynamic-load
Fix dynamic load
2 parents 8e3d4f9 + 575d859 commit 81ce4c5

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

quartodoc/autosummary.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import inspect
34
import logging
45
import warnings
56
import yaml
@@ -276,18 +277,24 @@ def dynamic_alias(
276277
splits = object_path.split(".")
277278

278279
canonical_path = None
279-
parts = []
280280
crnt_part = mod
281281
for ii, attr_name in enumerate(splits):
282282
try:
283283
crnt_part = getattr(crnt_part, attr_name)
284284
if not isinstance(crnt_part, ModuleType) and not canonical_path:
285-
canonical_path = crnt_part.__module__ + ":" + ".".join(splits[ii:])
285+
if inspect.isclass(crnt_part) or inspect.isfunction(crnt_part):
286+
_mod = getattr(crnt_part, "__module__", None)
287+
288+
if _mod is None:
289+
canonical_path = path
290+
else:
291+
canonical_path = _mod + ":" + ".".join(splits[ii:])
292+
else:
293+
canonical_path = path
286294
elif isinstance(crnt_part, ModuleType) and ii == (len(splits) - 1):
287295
# final object is module
288296
canonical_path = crnt_part.__name__
289297

290-
parts.append(crnt_part)
291298
except AttributeError:
292299
# Fetching the attribute can fail if it is purely a type hint,
293300
# and has no value. This can be an issue if you have added a

quartodoc/tests/example_dynamic.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,16 @@ def dynamic_doc(self, x):
2929

3030

3131
class InstanceAttrs:
32+
"""Some InstanceAttrs class"""
33+
3234
z: int
3335
"""The z attribute"""
3436

3537
def __init__(self, a: int, b: str):
3638
self.a = a
3739
self.b = b
3840
"""The b attribute"""
41+
42+
43+
some_instance = InstanceAttrs(1, 1)
44+
some_instance.__doc__ = "Dynamic instance doc"

quartodoc/tests/test_basic.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def test_get_object_dynamic_class_method_doc():
7979
assert meth.docstring.value == "A dynamic method"
8080

8181

82-
def test_get_object_dynamic_class_method_doc():
82+
def test_get_object_dynamic_class_method_doc_partial():
8383
obj = get_object("quartodoc.tests.example_dynamic:AClass", dynamic=True)
8484

8585
meth = obj.members["dynamic_create"]
@@ -92,7 +92,28 @@ def test_get_object_dynamic_class_instance_attr_doc():
9292
assert obj.members["b"].docstring.value == "The b attribute"
9393

9494

95-
def test_get_object_dynamic_class_instance_attr_doc():
95+
def test_get_object_dynamic_class_instance_attr_doc_class_attr_valueless():
9696
obj = get_object("quartodoc.tests.example_dynamic:InstanceAttrs", dynamic=True)
9797

9898
assert obj.members["z"].docstring.value == "The z attribute"
99+
100+
101+
def test_get_object_dynamic_module_attr_str():
102+
# a key behavior here is that it does not error attempting to look up
103+
# str.__module__, which does not exist
104+
obj = get_object("quartodoc.tests.example_dynamic:NOTE", dynamic=True)
105+
106+
assert obj.name == "NOTE"
107+
108+
# this case is weird, but we are dynamically looking up a string
109+
# so our __doc__ is technically str.__doc__
110+
assert obj.docstring.value == str.__doc__
111+
112+
113+
def test_get_object_dynamic_module_attr_class_instance():
114+
# a key behavior here is that it does not error attempting to look up
115+
# str.__module__, which does not exist
116+
obj = get_object("quartodoc.tests.example_dynamic:some_instance", dynamic=True)
117+
118+
assert obj.path == "quartodoc.tests.example_dynamic.some_instance"
119+
assert obj.docstring.value == "Dynamic instance doc"

0 commit comments

Comments
 (0)