Skip to content

Commit c0277b2

Browse files
committed
fix: Remove duplicate dataclass attributes when they have defaults
Fixes #52
1 parent 456736a commit c0277b2

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

src/pytkdocs/objects.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ def add_child(self, obj: "Object") -> None: # noqa: WPS231 (not complex)
255255
elif isinstance(obj, Method):
256256
self.methods.append(obj) # type: ignore
257257
elif isinstance(obj, Attribute):
258+
# Dataclass attributes with default values will already be present in `self.attributes` as they are
259+
# resolved differently by the python interpreter. Aas they have a concrete value, they are already present
260+
# in the "original" class. They should be overridden with the new "dataclass" attribute coming in here
261+
# (having the "dataclass_field" property set)
262+
new_attribute_name = obj.name
263+
for attribute in self.attributes:
264+
if attribute.name == new_attribute_name:
265+
self.attributes.remove(attribute)
258266
self.attributes.append(obj) # type: ignore
259267
obj.parent = self
260268

0 commit comments

Comments
 (0)