Skip to content

bug: property and cached_property of dataclasses are class parameters #232

@has2k1

Description

@has2k1

Description of the bug

A @property or @cached_property of dataclass is taken to be a class parameter

To Reproduce

sample.py

from functools import cached_property
from dataclasses import dataclass

class PointA:
    def __init__(self, x: float, y: float):
        self.x = x
        self.y = y

    @property
    def distance(self):
        return 0

class PointB:
    def __init__(self, x: float, y: float):
        self.x = x
        self.y = y

    @cached_property
    def distance(self):
        return 0

@dataclass
class PointC:
    x: float
    y: float

    @property
    def distance(self):
        return 0

@dataclass
class PointD:
    x: float
    y: float

    @cached_property
    def distance(self):
        return 0

Demo code

import griffe

def print_parameters(path):
    """
    Print class/method and a list of its parameters
    """
    obj = griffe.load(path)
    params = ", ".join(p.name for p in obj.parameters if p.name != "self")
    print(f"{obj.name}({params})")

print_parameters("sample.PointA")
print_parameters("sample.PointB")
print_parameters("sample.PointC")
print_parameters("sample.PointD")

Output

PointA(x, y)
PointB(x, y)
PointC(x, y, distance)
PointD(x, y, distance)

Expected behavior

The parameters in all four cases should be (x, y).

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions