Skip to content

Commit

Permalink
Fix duplicates or error when __init__ assigns to a property
Browse files Browse the repository at this point in the history
Fixes #466
  • Loading branch information
AWhetter committed Jul 23, 2024
1 parent a40327e commit 3e32238
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
21 changes: 20 additions & 1 deletion autoapi/_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,10 @@ def parse_classdef(self, node, data=None):
self._full_name_stack.append(node.name)
overridden = set()
overloads = {}
children = {}
for base in itertools.chain(iter((node,)), node.ancestors()):
seen = set()
base_children = []
if base.qname() in (
"__builtins__.object",
"builtins.object",
Expand All @@ -151,12 +153,29 @@ def parse_classdef(self, node, data=None):
continue
seen.add(name)
child_data = self.parse(child)
data["children"].extend(
base_children.extend(
_parse_child(node, child_data, overloads, base, name)
)

overridden.update(seen)

for base_child in base_children:
existing_child = children.get(base_child["name"])
if (
existing_child
# If an attribute was assigned to but this class has a property
# with the same name, then the property was assigned to,
# and not an attribute.
and not (
base_child["type"] == "property"
and existing_child["type"] == "attribute"
)
):
continue

children[base_child["name"]] = base_child

data["children"].extend(children.values())
self._qual_name_stack.pop()
self._full_name_stack.pop()

Expand Down
1 change: 1 addition & 0 deletions docs/changes/466.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix error or duplicates when __init__ assigns to a property
8 changes: 8 additions & 0 deletions tests/python/pyexample/example/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ def __init__(self, attr):
:type: str
"""

@property
def attr(self):
return 5

@attr.setter
def attr(self, value):
pass

@property
def property_simple(self) -> int:
"""This property should parse okay."""
Expand Down
5 changes: 4 additions & 1 deletion tests/python/test_pyintegration.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ def check_integration(self, parse, example_path):
assert foo.find(id="example.Foo.Meta")

# Check that class attributes are documented
assert foo.find(id="example.Foo.attr")
attr = foo.find(id="example.Foo.attr")
assert attr
assert attr.find(class_="pre").text.strip() == "property"

attr2 = foo.find(id="example.Foo.attr2")
assert "attr2" in attr2.text
# Check that attribute docstrings are used
Expand Down

0 comments on commit 3e32238

Please sign in to comment.