Skip to content

Commit

Permalink
Fix adding incorrect attributes from __init__
Browse files Browse the repository at this point in the history
These cases in __init__ are fixed:
* `local_variable : int = 1`
* `local_variable = 1`
* `self.foo.bar = 1` (Isn't .bar in the object.)

This assumes that the first parameter to __init__ is called "self".
  • Loading branch information
tannewt authored and AWhetter committed Oct 25, 2024
1 parent bedadd4 commit 9dd7851
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
17 changes: 17 additions & 0 deletions autoapi/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
6 changes: 6 additions & 0 deletions tests/python/py3example/example/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions tests/python/test_pyintegration.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,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")
Expand Down

0 comments on commit 9dd7851

Please sign in to comment.