Coding guidelines - SwissDataScienceCenter/renku-data-services GitHub Wiki

Coding guidelines for renku data services.

@property

Rule: Do not use @property if there are side effects in the function.

Good:

@dataclass
class Dog:
    color: str
    name: str

    @property
    def description(self) -> str:
        f"{self.color} dog, named {self.name}"

Bad:

@dataclass
class DogFactory:
    __dogs: list[str] = field(init=False, repr=False, default_factory=list)

    @property
    def dog(self) -> str:
        if len(self.__dogs) == 0:
            self.__dogs.append("Lassie")
        return self.__dogs[0]

Fix:

@dataclass
class DogFactory:
    __dogs: list[str] = field(init=False, repr=False, default_factory=list)

    def dog(self) -> str:
        # NOTE: Just use a method if you have side effects
        if len(self.__dogs) == 0:
            self.__dogs.append("Lassie")
        return self.__dogs[0]