Skip to content

Commit

Permalink
Improve test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-Steinmann committed Nov 21, 2024
1 parent 466b817 commit 466e079
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
16 changes: 10 additions & 6 deletions racad.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ def visit_ClassDef(self, node: ast.ClassDef) -> None:
self.visit(stmt)
# Reset the last attribute name after processing the class
self.last_attr_name = None

def _store_target_attr_name(self, target: ast.expr) -> None:
if isinstance(target, ast.Name):
self.last_attr_name = target.id
else:
self.last_attr_name = None

def visit_Assign(self, node: ast.Assign) -> None:
"""Visit an assignment node.
Expand All @@ -32,9 +38,10 @@ def visit_Assign(self, node: ast.Assign) -> None:
node: The assignment AST node to visit.
"""
# Handle simple assignments
if len(node.targets) == 1 and isinstance(node.targets[0], ast.Name):
self.last_attr_name = node.targets[0].id
if len(node.targets) == 1:
self._store_target_attr_name(node.targets[0])
else:
# Ignore multi assignments such as `a = b = 5`
self.last_attr_name = None

def visit_AnnAssign(self, node: ast.AnnAssign) -> None:
Expand All @@ -44,10 +51,7 @@ def visit_AnnAssign(self, node: ast.AnnAssign) -> None:
node: The annotated assignment AST node to visit.
"""
# Handle annotated assignments
if isinstance(node.target, ast.Name):
self.last_attr_name = node.target.id
else:
self.last_attr_name = None
self._store_target_attr_name(node.target)

def visit_Expr(self, node: ast.Expr) -> None:
"""Visit an expression node.
Expand Down
13 changes: 6 additions & 7 deletions test_racad.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ class MyWrongClass:
"""Hard-coding length."""

b, c = 1, 2
"""Document two at once."""
"""Document unpacked tuple at once."""

d = e = 3
"""Document multiple targets at once."""

docs = get_attribute_docstrings(MyWrongClass)
self.assertEqual(docs, {})
Expand Down Expand Up @@ -194,15 +197,15 @@ class Parent:
a = 5
"""This is the docstring for a."""

if sys.version_info < (3, 9):
if sys.version_info < (3, 9): # pragma: no cover
# Before python 3.9, getsource always returned the first class with
# the given name. In this case, it would return the class of a previous
# test.
class Child_(Parent):
a = 6
"""This is the overridden docstring."""
Child = Child_
else:
else: # pragma: no cover
class Child(Parent):
a = 6
"""This is the overridden docstring."""
Expand All @@ -211,7 +214,3 @@ class Child(Parent):
self.assertEqual(docs['a'], 'This is the overridden docstring.')
doc_a = get_attribute_docstring(Child, 'a', search_bases=True)
self.assertEqual(doc_a, 'This is the overridden docstring.')


if __name__ == '__main__':
unittest.main()

0 comments on commit 466e079

Please sign in to comment.