Skip to content

Commit

Permalink
fix: Don't return properties as parameters of dataclasses
Browse files Browse the repository at this point in the history
Issue #232: #232
  • Loading branch information
pawamoy committed Feb 8, 2024
1 parent d863831 commit 5a5c03b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1566,6 +1566,7 @@ def parameters(self) -> Parameters:
*[
Parameter(attr.name, annotation=attr.annotation, default=attr.value)
for attr in self.attributes.values()
if "property" not in attr.labels

This comment has been minimized.

Copy link
@has2k1

has2k1 Feb 8, 2024

Contributor

There is also the case of non-init fields and so are not parameters.

from dataclasses import dataclass, field

@dataclass
class Point:
    x: float
    y: float
    z: float = field(init=False, default=0)

z should not be a parameter.

This comment has been minimized.

Copy link
@pawamoy

pawamoy Feb 8, 2024

Author Member

Ha, that's true. Can you open a new issue? You can skip the issue template questions, just paste this snippet. If you know any other scenario where an attribute shouldn't be detected as a parameter, please share!

],
)
return Parameters()
Expand Down
28 changes: 27 additions & 1 deletion tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import griffe
from griffe.dataclasses import Docstring, Module
from griffe.loader import GriffeLoader
from griffe.tests import module_vtree, temporary_pypackage
from griffe.tests import module_vtree, temporary_pypackage, temporary_visited_module


def test_submodule_exports() -> None:
Expand Down Expand Up @@ -86,3 +86,29 @@ def test_alias_proxies() -> None:
for name in cls.all_members:
if not name.startswith("_") or name.startswith("__"):
assert name in alias_members


def test_dataclass_parameters() -> None:
"""Don't return properties as parameters of dataclasses."""
with temporary_visited_module(
"""
from dataclasses import dataclass
from functools import cached_property
@dataclass
class Point:
x: float
y: float
@property
def a(self):
return 0
@cached_property
def b(self):
return 0
""",
) as module:
params = module["Point"].parameters
assert "a" not in params
assert "b" not in params

0 comments on commit 5a5c03b

Please sign in to comment.