Skip to content

Property side-effects with @lt.on_set#331

Open
rwb27 wants to merge 2 commits intomainfrom
side-effects-for-properties
Open

Property side-effects with @lt.on_set#331
rwb27 wants to merge 2 commits intomainfrom
side-effects-for-properties

Conversation

@rwb27
Copy link
Copy Markdown
Collaborator

@rwb27 rwb27 commented May 5, 2026

This adds basic side-effects to properties, along with some tests.

Methods may be decorated with @lt.on_set. The method will then be run whenever the property is set.

To-do:

  • Add to public API docs
  • Add to conceptual docs on data properties, and link from functional properties
  • Test this against DataSetting as well as properties

This is intended to allow for basic validation or synchronisation, where the full power of a functional property is not needed. It runs before validation (if enabled), to allow coercion and to catch None in case someone's forgotten to return a value from on_set. This could become configurable in the future.

It may be desirable to also allow a similar decorator to add a validator to the Pydantic model, but that might require more thought, and is not for this PR.

Closes #237

@barecheck
Copy link
Copy Markdown

barecheck Bot commented May 5, 2026

Barecheck - Code coverage report

Total: 96.68%

Your code coverage diff: -0.06% ▾

Uncovered files and lines
FileLines
src/labthings_fastapi/properties.py464, 482, 562, 925-926, 928, 968, 972, 1007-1010, 1082, 1105, 1504

@rwb27
Copy link
Copy Markdown
Collaborator Author

rwb27 commented May 6, 2026

From @julianstirling out-of-band:

  • Simulation camera updates the canvas on certain propery changes
  • Also when we have mapping thingslots we check if the key is available in the mapping when we change our selectors

rwb27 added 2 commits May 6, 2026 11:34
This adds basic side-effects to properties, along with some tests.
Running the `on_set` function before validating the property (if enabled) has a few advantages:
* If the user has forgotten to return a value, it's more likely to catch the `None` if it's invalid.
* It allows user code
@rwb27 rwb27 force-pushed the side-effects-for-properties branch from 0c864e0 to 1d8bd1c Compare May 6, 2026 10:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a side_effect option to data properties.

1 participant