Skip to content

Commit 88b457f

Browse files
authored
fix: Fix getting parent module of decorated functions
Issue mkdocstrings#162: mkdocstrings/mkdocstrings#162 PR #109: #109
1 parent 5dd856e commit 88b457f

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

Diff for: src/pytkdocs/loader.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -434,9 +434,9 @@ def get_module_documentation(self, node: ObjectNode, select_members=None) -> Mod
434434
for member_name, member in inspect.getmembers(module):
435435
if self.select(member_name, select_members): # type: ignore
436436
child_node = ObjectNode(member, member_name, parent=node)
437-
if child_node.is_class() and node.root.obj is inspect.getmodule(member):
437+
if child_node.is_class() and node.root.obj is inspect.getmodule(child_node.obj):
438438
root_object.add_child(self.get_class_documentation(child_node))
439-
elif child_node.is_function() and node.root.obj is inspect.getmodule(member):
439+
elif child_node.is_function() and node.root.obj is inspect.getmodule(child_node.obj):
440440
root_object.add_child(self.get_function_documentation(child_node))
441441
elif member_name in attributes_data:
442442
root_object.add_child(self.get_attribute_documentation(child_node, attributes_data[member_name]))

Diff for: tests/fixtures/decorated_function.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from functools import lru_cache
2+
3+
4+
@lru_cache()
5+
def add(a, b):
6+
return a + b
7+
8+
9+
# control
10+
def sub(a, b):
11+
return a - b
12+
13+
14+
# simulating a decorator that does not set __module__ properly
15+
# on the wrapper object
16+
del add.__module__

Diff for: tests/test_loader.py

+11
Original file line numberDiff line numberDiff line change
@@ -534,3 +534,14 @@ def test_method_descriptor():
534534
assert len(obj.signature.parameters) == 2
535535
assert obj.docstring
536536
assert obj.category == "method"
537+
538+
539+
def test_load_decorated_function():
540+
"""Load a decorated function."""
541+
loader = Loader(new_path_syntax=True)
542+
obj = loader.get_object_documentation("tests.fixtures.decorated_function")
543+
assert [child.name for child in obj.children] == ["add", "sub"]
544+
for child in obj.children:
545+
assert child.category == "function"
546+
assert child.parent is child.root
547+
assert child.parent.name == "decorated_function"

0 commit comments

Comments
 (0)