Skip to content

Property Setters

When using the Execution Flows fy tool, writing a property setter manually involves defining a property and then adding a @<property_name>.setter method to manage how the property value is assigned. Alternatively, the fy tool simplifies this process by generating the necessary boilerplate code. You can declare an abstract property and include property <property_name> using setter in the flow mixins, allowing the tool to automatically handle the setter implementation. This approach streamlines property management in your code.

Automatically Generated Setter Implementation

When a flow declares a setter, the fy tool checks if a setter implementation for the defined property exists. If it does not find an implementation, it generates one. Additionally, the fy tool generates an __init__ method for the encapsulating flow to initialize the setters.

Below is an example of the generated code for a setter for the property greeting: str.

Example

Abstract property definition:

property greeting: str
mixins/property/greeting/abc_fy.py
"""fy
property greeting: str
"""
import abc


# fy:start ===>>>
class Greeting_PropertyMixin_ABC(abc.ABC):
    @property
    @abc.abstractmethod
    def _greeting(self) -> str:
        raise NotImplementedError()

Property setter defined as a flow mixin:

flow HelloWorld_UsingGreeting -> None:
    property greeting using setter
flows/hello_world_using_setter_fy.py
"""fy
flow HelloWorld_UsingGreeting -> None:
    property greeting using setter
"""
from typing import Any

from fy_core.base.flow_base import FlowBase

from mixins.property.greeting.using_setter_fy import (
    Greeting_UsingSetter_PropertyMixin,
)


# fy:start ===>>>
class HelloWorld_UsingSetter_Flow(
    # Property Mixins
    Greeting_UsingSetter_PropertyMixin,
    # Base
    FlowBase[None],
):
    def __init__(
        self,
        *args: Any,
        greeting: str,
        **kwargs: Any,
    ) -> None:
        self._greeting = greeting
        super().__init__(*args, **kwargs)

    def __call__(self) -> None:
        # fy:end <<<===
        print(self._greeting)

A Python file generated by the fy tool

mixins/property/greeting/using_setter.py
# fy:start ===>>>
class Greeting_UsingSetter_PropertyMixin:
    @property
    def _greeting(self) -> str:
        raise NotImplementedError()

    @_greeting.setter
    def _greeting(self, value: str) -> None:
        raise NotImplementedError()
        # fy:end <<<===