Skip to content

Conversation

@rwb27
Copy link
Collaborator

@rwb27 rwb27 commented Nov 17, 2025

lt.property now passes some keyword arguments on to pydantic.Field. This allows basic numeric constraints to be added to properties. Adding regex patterns and length constraints to strings is also allowed.

This is limited at present:

  • Constraints are only checked when values are set over HTTP, not python.
  • Constraints are only supported on DataProperties
  • We don't proactively check constraints are OK, they will only be checked when the model is created. This happens lazily at present.

Adding constraints to settings should be trivial. Doing so for functional properties/settings will require a decorator with arguments, which is trickier but possible.

To do before merging:

  • Add to the docs on properties
  • Duplicate for settings

To think about for the future:

  • Should we add a decorator-with-args so we could have functional properties with constraints?
  • Should we check constraints when properties are set from Python
  • Should we validate defaults against constraints? If so, do we do it lazily?

Closes #186

`lt.property` now passes some keyword arguments on to `pydantic.Field`.
This allows basic numeric constraints to be added to properties.
Adding regex patterns and length constraints to strings is also allowed.

This is limited at present:
* Constraints are only checked when values are set over HTTP, not python.
* Constraints are only supported on DataProperties
* We don't proactively check constraints are OK, they will only be checked when the model is created. This happens lazily at present.

Adding constraints to settings should be trivial. Doing so for functional properties/settings will require a decorator with arguments, which is trickier but possible.
@rwb27 rwb27 force-pushed the constraints-on-dataproperty branch from 62fc5bf to f9269fb Compare November 26, 2025 10:20
This change also makes `constraints` a public attribute, allowing `FunctionalProperty` (or setting) instances to have constraints set.

Both settings and properties, data and functional, are tested.
This also moves the "properties" page out of the tutorial: it's too detailed for that. The new structure is much closer to my original intent.
Fixed a dead link and added an `__all__` to stop exceptions being duplicated.
The additional "constraints" argument was causing a test to fail: this is now fixed.
This wasn't flagged by Ruff, I should possibly just turn off the flake8 code.
@rwb27 rwb27 requested a review from julianstirling November 26, 2025 13:28
@barecheck
Copy link

barecheck bot commented Nov 26, 2025

Barecheck - Code coverage report

Total: 95.01%

Your code coverage diff: -0.03% ▾

Uncovered files and lines
FileLines
src/labthings_fastapi/properties.py689, 693, 716-719, 810, 943
src/labthings_fastapi/utilities/__init__.py123, 143

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.

Pass pydantic.Field constraints through lt.property

3 participants