Skip to content

Commit 3e32238

Browse files
committed
Fix duplicates or error when __init__ assigns to a property
Fixes #466
1 parent a40327e commit 3e32238

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

autoapi/_parser.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,10 @@ def parse_classdef(self, node, data=None):
131131
self._full_name_stack.append(node.name)
132132
overridden = set()
133133
overloads = {}
134+
children = {}
134135
for base in itertools.chain(iter((node,)), node.ancestors()):
135136
seen = set()
137+
base_children = []
136138
if base.qname() in (
137139
"__builtins__.object",
138140
"builtins.object",
@@ -151,12 +153,29 @@ def parse_classdef(self, node, data=None):
151153
continue
152154
seen.add(name)
153155
child_data = self.parse(child)
154-
data["children"].extend(
156+
base_children.extend(
155157
_parse_child(node, child_data, overloads, base, name)
156158
)
157159

158160
overridden.update(seen)
159161

162+
for base_child in base_children:
163+
existing_child = children.get(base_child["name"])
164+
if (
165+
existing_child
166+
# If an attribute was assigned to but this class has a property
167+
# with the same name, then the property was assigned to,
168+
# and not an attribute.
169+
and not (
170+
base_child["type"] == "property"
171+
and existing_child["type"] == "attribute"
172+
)
173+
):
174+
continue
175+
176+
children[base_child["name"]] = base_child
177+
178+
data["children"].extend(children.values())
160179
self._qual_name_stack.pop()
161180
self._full_name_stack.pop()
162181

docs/changes/466.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix error or duplicates when __init__ assigns to a property

tests/python/pyexample/example/example.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ def __init__(self, attr):
4040
:type: str
4141
"""
4242

43+
@property
44+
def attr(self):
45+
return 5
46+
47+
@attr.setter
48+
def attr(self, value):
49+
pass
50+
4351
@property
4452
def property_simple(self) -> int:
4553
"""This property should parse okay."""

tests/python/test_pyintegration.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,10 @@ def check_integration(self, parse, example_path):
5151
assert foo.find(id="example.Foo.Meta")
5252

5353
# Check that class attributes are documented
54-
assert foo.find(id="example.Foo.attr")
54+
attr = foo.find(id="example.Foo.attr")
55+
assert attr
56+
assert attr.find(class_="pre").text.strip() == "property"
57+
5558
attr2 = foo.find(id="example.Foo.attr2")
5659
assert "attr2" in attr2.text
5760
# Check that attribute docstrings are used

0 commit comments

Comments
 (0)