Skip to content

fix: Fix getting parent module of decorated functions #109

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

Merged
merged 1 commit into from
Aug 20, 2021
Merged
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
4 changes: 2 additions & 2 deletions src/pytkdocs/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,9 @@ def get_module_documentation(self, node: ObjectNode, select_members=None) -> Mod
for member_name, member in inspect.getmembers(module):
if self.select(member_name, select_members): # type: ignore
child_node = ObjectNode(member, member_name, parent=node)
if child_node.is_class() and node.root.obj is inspect.getmodule(member):
if child_node.is_class() and node.root.obj is inspect.getmodule(child_node.obj):
root_object.add_child(self.get_class_documentation(child_node))
elif child_node.is_function() and node.root.obj is inspect.getmodule(member):
elif child_node.is_function() and node.root.obj is inspect.getmodule(child_node.obj):
root_object.add_child(self.get_function_documentation(child_node))
elif member_name in attributes_data:
root_object.add_child(self.get_attribute_documentation(child_node, attributes_data[member_name]))
Expand Down
16 changes: 16 additions & 0 deletions tests/fixtures/decorated_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from functools import lru_cache


@lru_cache()
def add(a, b):
return a + b


# control
def sub(a, b):
return a - b


# simulating a decorator that does not set __module__ properly
# on the wrapper object
del add.__module__
11 changes: 11 additions & 0 deletions tests/test_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,14 @@ def test_method_descriptor():
assert len(obj.signature.parameters) == 2
assert obj.docstring
assert obj.category == "method"


def test_load_decorated_function():
"""Load a decorated function."""
loader = Loader(new_path_syntax=True)
obj = loader.get_object_documentation("tests.fixtures.decorated_function")
assert [child.name for child in obj.children] == ["add", "sub"]
for child in obj.children:
assert child.category == "function"
assert child.parent is child.root
assert child.parent.name == "decorated_function"