Skip to content

Commit c9b2e09

Browse files
authored
fix: Attributes without annotations cannot be dataclass parameters
All dataclass parameters have annotations. When determining which of the attributes of dataclass are parameters, we explicitly check against that condition. This help us filter out obvious non-candidates that may otherwise fall through. PR-297: #297
1 parent 72c8fc8 commit c9b2e09

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/griffe/extensions/dataclasses.py

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ def _dataclass_parameters(class_: Class) -> list[Parameter]:
8080
if member.is_attribute:
8181
member = cast(Attribute, member)
8282

83+
# All dataclass parameters have annotations
84+
if member.annotation is None:
85+
continue
86+
8387
# Attributes that have labels for these characteristics are
8488
# not class parameters:
8589
# - @property

tests/test_dataclasses.py

+24
Original file line numberDiff line numberDiff line change
@@ -421,3 +421,27 @@ class Derived(Base):
421421
assert param_b.docstring.value == "Parameter b"
422422
assert param_c.docstring is None
423423
assert param_d.docstring.value == "Parameter d"
424+
425+
426+
def test_attributes_that_have_no_annotations() -> None:
427+
"""Dataclass attributes that have no annotatations are not parameters."""
428+
code = """
429+
from dataclasses import dataclass, field
430+
431+
@dataclass
432+
class Base:
433+
a: int
434+
b: str = field(init=False)
435+
c = 3 # class attribute
436+
437+
@dataclass
438+
class Derived(Base):
439+
a = 1 # no effect on the parameter status of a
440+
b = "b" # inherited non-parameter
441+
d: float = 4
442+
"""
443+
with temporary_visited_package("package", {"__init__.py": code}) as module:
444+
base_params = [p.name for p in module["Base"].parameters]
445+
derived_params = [p.name for p in module["Derived"].parameters]
446+
assert base_params == ["self", "a"]
447+
assert derived_params == ["self", "a", "d"]

0 commit comments

Comments
 (0)