Skip to content

Commit 4ee49c2

Browse files
committed
Propagate read-only metadata to the Thing Description.
1 parent 2e0eb20 commit 4ee49c2

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/labthings_fastapi/properties.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,8 @@ def property_affordance(
467467
title=self.title,
468468
forms=forms,
469469
description=self.description,
470+
readOnly=self.readonly,
471+
writeOnly=False, # write-only properties are not yet supported
470472
)
471473
# We merge the data schema with the property affordance (which subclasses the
472474
# DataSchema model) with the affordance second so its values take priority.

tests/test_property.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,49 @@ class BadIntModel(pydantic.BaseModel):
344344
assert example.badprop == 3
345345
with pytest.raises(TypeError):
346346
_ = example.properties["badprop"].model_instance
347+
348+
349+
def test_readonly_metadata():
350+
"""Check read-only data propagates to the Thing Description."""
351+
352+
class Example(lt.Thing):
353+
prop: int = lt.property(default=0)
354+
ro_property: int = lt.property(default=0, readonly=True)
355+
356+
@lt.property
357+
def ro_functional_property(self) -> int:
358+
"""This property should be read-only as there's no setter."""
359+
return 42
360+
361+
@lt.property
362+
def ro_functional_property_with_setter(self) -> int:
363+
return 42
364+
365+
@ro_functional_property_with_setter.setter
366+
def _set_ro_functional_property_with_setter(self, val: int) -> None:
367+
pass
368+
369+
ro_functional_property_with_setter.readonly = True
370+
371+
@lt.property
372+
def funcprop(self) -> int:
373+
return 42
374+
375+
@funcprop.setter
376+
def _set_funcprop(self, val: int) -> None:
377+
pass
378+
379+
example = create_thing_without_server(Example)
380+
381+
td = example.thing_description()
382+
383+
# Check read-write properties are not read-only
384+
for name in ["prop", "funcprop"]:
385+
assert td.properties[name].readOnly is False
386+
387+
for name in [
388+
"ro_property",
389+
"ro_functional_property",
390+
"ro_functional_property_with_setter",
391+
]:
392+
assert td.properties[name].readOnly is True

0 commit comments

Comments
 (0)