Skip to content

Commit 5cfb176

Browse files
authored
[App] Enable properties for the Lightning flow (#15750)
1 parent 6c8ee01 commit 5cfb176

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/lightning_app/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
4949
- Fixed debugging with VSCode IDE ([#15747](https://github.com/Lightning-AI/lightning/pull/15747))
5050

5151

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

5356
## [1.8.1] - 2022-11-10
5457

src/lightning_app/core/flow.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ def name(self):
110110
"""Return the current LightningFlow name."""
111111
return self._name or "root"
112112

113-
def __setattr__(self, name, value):
113+
def __setattr__(self, name: str, value: Any) -> None:
114+
attr = getattr(self.__class__, name, None)
115+
if isinstance(attr, property) and attr.fset is not None:
116+
return attr.fset(self, value)
117+
114118
from lightning_app.structures import Dict, List
115119

116120
if (

tests/tests_app/core/test_lightning_app.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,3 +1108,36 @@ def test_cloud_compute_binding():
11081108

11091109
with pytest.raises(Exception, match="A Cloud Compute can be assigned only to a single Work"):
11101110
FlowCC()
1111+
1112+
1113+
class FlowValue(LightningFlow):
1114+
def __init__(self):
1115+
super().__init__()
1116+
self._value = None
1117+
self._has_found = False
1118+
1119+
@property
1120+
def value(self):
1121+
return self._value
1122+
1123+
@value.setter
1124+
def value(self, value):
1125+
self._value = value
1126+
1127+
def run(self):
1128+
if self.value is None:
1129+
self.value = True
1130+
1131+
def __setattr__(self, name, value):
1132+
if name == "_value" and value is True:
1133+
self._has_found = True
1134+
super().__setattr__(name, value)
1135+
1136+
1137+
def test_lightning_flow_properties():
1138+
"""Validates setting properties to the LightningFlow properly calls property.fset."""
1139+
1140+
flow = FlowValue()
1141+
assert not flow._has_found
1142+
flow.run()
1143+
assert flow._has_found

0 commit comments

Comments
 (0)