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

[App] Enable properties for the Lightning flow #15750

Merged
merged 12 commits into from Nov 21, 2022
3 changes: 3 additions & 0 deletions src/lightning_app/CHANGELOG.md
Expand Up @@ -49,6 +49,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed debugging with VSCode IDE ([#15747](https://github.com/Lightning-AI/lightning/pull/15747))


- Fixed setting property to the LightningFlow ([#15750](https://github.com/Lightning-AI/lightning/pull/15750))



## [1.8.1] - 2022-11-10

Expand Down
6 changes: 5 additions & 1 deletion src/lightning_app/core/flow.py
Expand Up @@ -110,7 +110,11 @@ def name(self):
"""Return the current LightningFlow name."""
return self._name or "root"

def __setattr__(self, name, value):
def __setattr__(self, name: str, value: Any) -> None:
attr = getattr(self.__class__, name, None)
if isinstance(attr, property) and attr.fset is not None:
return attr.fset(self, value)

from lightning_app.structures import Dict, List

if (
Expand Down
33 changes: 33 additions & 0 deletions tests/tests_app/core/test_lightning_app.py
Expand Up @@ -1108,3 +1108,36 @@ def test_cloud_compute_binding():

with pytest.raises(Exception, match="A Cloud Compute can be assigned only to a single Work"):
FlowCC()


class FlowValue(LightningFlow):
def __init__(self):
super().__init__()
self._value = None
self._has_found = False

@property
def value(self):
return self._value

@value.setter
def value(self, value):
self._value = value

def run(self):
if self.value is None:
self.value = True

def __setattr__(self, name, value):
if name == "_value" and value is True:
self._has_found = True
super().__setattr__(name, value)


def test_lightning_flow_properties():
"""Validates setting properties to the LightningFlow properly calls property.fset."""

flow = FlowValue()
assert not flow._has_found
flow.run()
assert flow._has_found