Skip to content

Commit 5a391e5

Browse files
authored
Merge pull request #100 from bstadlbauer/bugfix-duplicate-dataclass-attributes
Bugfix duplicate dataclass attributes
2 parents 014171b + a3b1229 commit 5a391e5

4 files changed

Lines changed: 13 additions & 3 deletions

File tree

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ make setup
2828
```
2929

3030
Now you can try running `make setup` again,
31-
or simply `poetry install`.
31+
or simply `poetry install -E numpy-style`.
3232

3333
You now have the dependencies installed.
3434

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ include = [
1919
]
2020

2121
[tool.poetry.dependencies]
22-
python = "^3.6"
22+
python = "^3.6.1"
2323
astunparse = {version = "^1.6.3", python = "<3.9"}
2424
cached-property = {version = "^1.5.2", python = "<3.8"}
2525
dataclasses = {version = ">=0.7,<0.9", python = "3.6"}
@@ -57,6 +57,8 @@ pytest-sugar = "^0.9.4"
5757
pytest-xdist = "^2.2.0"
5858
toml = "^0.10.2"
5959
wemake-python-styleguide = "^0.14.1"
60+
pydantic = "^1.8.1"
61+
marshmallow = "^3.11.1"
6062

6163
[tool.poetry.scripts]
6264
pytkdocs = "pytkdocs.cli:main"

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. As 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

tests/fixtures/dataclass.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Person:
66
"""Simple dataclass for a person's information"""
77

88
name: str
9-
age: int
9+
age: int = 2
1010
"""Field description."""
1111

1212

0 commit comments

Comments
 (0)