Skip to content

Commit

Permalink
[App] Enable properties for the Lightning flow (#15750)
Browse files Browse the repository at this point in the history
(cherry picked from commit 5cfb176)
  • Loading branch information
tchaton authored and Borda committed Nov 21, 2022
1 parent 69f4aea commit 636a46c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/lightning_app/CHANGELOG.md
Expand Up @@ -47,6 +47,9 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Fixed catimage import ([#15712](https://github.com/Lightning-AI/lightning/pull/15712))
- Parse all lines in app file looking for shebangs to run commands ([#15714](https://github.com/Lightning-AI/lightning/pull/15714))

- 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

0 comments on commit 636a46c

Please sign in to comment.