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

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

Closed
has2k1 opened this issue Feb 8, 2024 · 1 comment
Closed
Assignees

Comments

@has2k1
Copy link
Contributor

has2k1 commented Feb 8, 2024

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).

@pawamoy
Copy link
Member

pawamoy commented Feb 8, 2024

Thanks for the high-quality report. Fixed in v0.40.1 🙂

@pawamoy pawamoy closed this as completed Feb 8, 2024
pawamoy pushed a commit that referenced this issue Mar 11, 2024
This was fixed in 5a5c03b, but broken in 82a9d57. The tests
broke as well and did not catch the regression.

Issue-232: #232
PR-248: #248
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants