From 8c48397e7301bbb296e2f2630405f2d22f7222e3 Mon Sep 17 00:00:00 2001 From: Hassan Kibirige Date: Mon, 11 Mar 2024 16:01:32 +0300 Subject: [PATCH] fix: Don't return properties as parameters of dataclasses (again) This was fixed in 5a5c03b, but broken in 82a9d57. The tests broke as well and did not catch the regression. Issue-232: https://github.com/mkdocstrings/griffe/issues/232 PR-248: https://github.com/mkdocstrings/griffe/pull/248 --- src/griffe/extensions/dataclasses.py | 4 ++++ tests/test_dataclasses.py | 12 +++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/griffe/extensions/dataclasses.py b/src/griffe/extensions/dataclasses.py index 41141202..b679def1 100644 --- a/src/griffe/extensions/dataclasses.py +++ b/src/griffe/extensions/dataclasses.py @@ -80,6 +80,10 @@ def _dataclass_parameters(class_: Class) -> list[Parameter]: if member.is_attribute: member = cast(Attribute, member) + # Exclude @property and @cached_property attributes + if "property" in member.labels: + continue + # Start of keyword-only parameters. if isinstance(member.annotation, Expr) and member.annotation.canonical_path == "dataclasses.KW_ONLY": kw_only = True diff --git a/tests/test_dataclasses.py b/tests/test_dataclasses.py index bbbac106..ddc80966 100644 --- a/tests/test_dataclasses.py +++ b/tests/test_dataclasses.py @@ -10,7 +10,7 @@ import griffe from griffe.dataclasses import Docstring, Module from griffe.loader import GriffeLoader -from griffe.tests import module_vtree, temporary_pypackage, temporary_visited_module, temporary_visited_package +from griffe.tests import module_vtree, temporary_pypackage, temporary_visited_package def test_submodule_exports() -> None: @@ -93,8 +93,7 @@ def test_alias_proxies() -> None: def test_dataclass_properties() -> None: """Don't return properties as parameters of dataclasses.""" - with temporary_visited_module( - """ + code = """ from dataclasses import dataclass from functools import cached_property @@ -110,11 +109,10 @@ def a(self): @cached_property def b(self): return 0 - """, - ) as module: + """ + with temporary_visited_package("package", {"__init__.py": code}) as module: params = module["Point"].parameters - assert "a" not in params - assert "b" not in params + assert [p.name for p in params] == ["self", "x", "y"] @pytest.mark.parametrize(