Skip to content
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

Fix adding incorrect attributes from __init__ #497

Merged
merged 1 commit into from
Oct 25, 2024
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
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 @@ -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")
Expand Down