diff --git a/autoapi/_parser.py b/autoapi/_parser.py index ff28e1d9..56672a24 100644 --- a/autoapi/_parser.py +++ b/autoapi/_parser.py @@ -267,6 +267,23 @@ def parse_functiondef(self, node): if node.name == "__init__": for child in node.get_children(): if isinstance(child, (astroid.nodes.Assign, astroid.nodes.AnnAssign)): + # Verify we are assigning to self. + if isinstance(child, astroid.nodes.Assign): + targets = child.targets + else: + targets = [child.target] + + target_ok = True + for target in targets: + if not isinstance(target, astroid.nodes.AssignAttr): + target_ok = False + break + _object = target.expr + if not isinstance(_object, astroid.nodes.Name) or _object.name != "self": + target_ok = False + break + if not target_ok: + continue child_data = self._parse_assign(child) result.extend(data for data in child_data) diff --git a/tests/python/py3example/example/example.py b/tests/python/py3example/example/example.py index 73efa2e4..4c459ae3 100644 --- a/tests/python/py3example/example/example.py +++ b/tests/python/py3example/example/example.py @@ -92,6 +92,12 @@ def __init__(self): self.instance_var: bool = True """This is an instance_var.""" + self.subobject: object = object() + self.subobject.subobject_variable = 1 + + local_variable_typed: int = 0 + local_variable_untyped = 2 + async def async_method(self, wait: bool) -> int: if wait: await asyncio.sleep(1) diff --git a/tests/python/test_pyintegration.py b/tests/python/test_pyintegration.py index 4e3dbd61..c5c4aca3 100644 --- a/tests/python/test_pyintegration.py +++ b/tests/python/test_pyintegration.py @@ -458,6 +458,13 @@ def test_annotations(self, parse): assert example_file.find(id="example.A.instance_var") + # Locals are excluded + assert not example_file.find(id="example.A.local_variable_typed") + assert not example_file.find(id="example.A.local_variable_untyped") + + # Assignments to subobjects are excluded + assert not example_file.find(id="example.A.subobject_variable") + global_a = example_file.find(id="example.global_a") assert global_a global_a_value = global_a.find_all(class_="property")