Skip to content

Commit

Permalink
Don't document module level class attribute assignments
Browse files Browse the repository at this point in the history
Fixes #341
  • Loading branch information
AWhetter committed Oct 23, 2022
1 parent 0963c6d commit 94295a4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Bug Fixes
^^^^^^^^^
* `#330 <https://github.com/readthedocs/sphinx-autoapi/issues/330>`: (Python)
Render tuple values as tuples, not lists.
* `#341 <https://github.com/readthedocs/sphinx-autoapi/issues/341>`: (Python)
Fix module level assignments to class attributes being documented as
module level attributes.

Trivial/Internal Changes
^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
15 changes: 13 additions & 2 deletions autoapi/mappers/python/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ def parse_file_in_namespace(self, file_path, dir_root):
)

def parse_annassign(self, node):
return self.parse_assign(node)
# Don't document module level assignments to class attributes
if isinstance(node.target, astroid.nodes.AssignAttr):
return []

return self._parse_assign(node)

def parse_assign(self, node):
# Don't document module level assignments to class attributes
if any(isinstance(target, astroid.nodes.AssignAttr) for target in node.targets):
return []

return self._parse_assign(node)

def _parse_assign(self, node):
doc = ""
doc_node = node.next_sibling()
if isinstance(doc_node, astroid.nodes.Expr) and isinstance(
Expand Down Expand Up @@ -187,7 +198,7 @@ def parse_functiondef(self, node): # pylint: disable=too-many-branches
if node.name == "__init__":
for child in node.get_children():
if isinstance(child, (astroid.nodes.Assign, astroid.nodes.AnnAssign)):
child_data = self.parse_assign(child)
child_data = self._parse_assign(child)
result.extend(data for data in child_data if data["doc"])

return result
Expand Down
3 changes: 3 additions & 0 deletions tests/python/pyexample/example/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ def method_google_unicode(self):
return "google"


Foo.bar = "dinglebop"


def decorator_okay(func):
"""This decorator should parse okay."""

Expand Down
4 changes: 4 additions & 0 deletions tests/python/test_pyintegration.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def check_integration(self, example_path):
# Lists should be rendered as lists, not tuples
assert "['c', 'd']" in example_file

# Assigning a class level attribute at the module level
# should not get documented as a module level attribute.
assert "dinglebop" not in example_file

index_path = "_build/text/index.txt"
with io.open(index_path, encoding="utf8") as index_handle:
index_file = index_handle.read()
Expand Down

0 comments on commit 94295a4

Please sign in to comment.