Skip to content

Bugfix duplicate dataclass attributes #100

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

Merged
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
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ make setup
```

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

You now have the dependencies installed.

Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ include = [
]

[tool.poetry.dependencies]
python = "^3.6"
python = "^3.6.1"
astunparse = {version = "^1.6.3", python = "<3.9"}
cached-property = {version = "^1.5.2", python = "<3.8"}
dataclasses = {version = ">=0.7,<0.9", python = "3.6"}
Expand Down Expand Up @@ -57,6 +57,8 @@ pytest-sugar = "^0.9.4"
pytest-xdist = "^2.2.0"
toml = "^0.10.2"
wemake-python-styleguide = "^0.14.1"
pydantic = "^1.8.1"
marshmallow = "^3.11.1"

[tool.poetry.scripts]
pytkdocs = "pytkdocs.cli:main"
Expand Down
8 changes: 8 additions & 0 deletions src/pytkdocs/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@ def add_child(self, obj: "Object") -> None: # noqa: WPS231 (not complex)
elif isinstance(obj, Method):
self.methods.append(obj) # type: ignore
elif isinstance(obj, Attribute):
# Dataclass attributes with default values will already be present in `self.attributes` as they are
# resolved differently by the python interpreter. As they have a concrete value, they are already present
# in the "original" class. They should be overridden with the new "dataclass" attribute coming in here
# (having the "dataclass_field" property set)
new_attribute_name = obj.name
for attribute in self.attributes:
if attribute.name == new_attribute_name:
self.attributes.remove(attribute)
self.attributes.append(obj) # type: ignore
obj.parent = self

Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Person:
"""Simple dataclass for a person's information"""

name: str
age: int
age: int = 2
"""Field description."""


Expand Down