Skip to content
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

fix: Don't return class variables as parameters of dataclasses #253

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
10 changes: 8 additions & 2 deletions src/griffe/extensions/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,20 @@ def _dataclass_parameters(class_: Class) -> list[Parameter]:
# All parameters marked as keyword-only.
kw_only = dec_args.get("kw_only") == "True"

# Attributes that have labels for these characteristics are
# not class parameters:
# - @property
# - @cached_property
# - ClassVar annotation
non_parameter_labels = {"property", "class-attribute"}

# Iterate on current attributes to find parameters.
parameters = []
for member in class_.members.values():
if member.is_attribute:
member = cast(Attribute, member)

# Exclude @property and @cached_property attributes
if "property" in member.labels:
if member.labels & non_parameter_labels:
continue

# Start of keyword-only parameters.
Expand Down
6 changes: 4 additions & 2 deletions tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,18 @@ def test_alias_proxies() -> None:
assert name in alias_members


def test_dataclass_properties() -> None:
"""Don't return properties as parameters of dataclasses."""
def test_dataclass_properties_and_class_variables() -> None:
"""Don't return properties or typed class variables as parameters of dataclasses."""
code = """
from dataclasses import dataclass
from functools import cached_property
from typing import ClassVar

@dataclass
class Point:
x: float
y: float
z: ClassVar[float] = 3
has2k1 marked this conversation as resolved.
Show resolved Hide resolved

@property
def a(self):
Expand Down
Loading