Skip to content

Conversation

rwb27
Copy link
Collaborator

@rwb27 rwb27 commented Aug 12, 2025

This raises a helpful error if observe_property is called with an argument that isn't a DataProperty. Usually, this will be a functional property, which isn't observable.

To-do before merging:

  • Handle errors gracefully in the websocket endpoint.

Closes #169

@rwb27 rwb27 marked this pull request as draft August 12, 2025 17:59
Copy link

barecheck bot commented Aug 13, 2025

Barecheck - Code coverage report

Total: 93.48%

Your code coverage diff: 0.15% ▴

Uncovered files and lines
FileLines
src/labthings_fastapi/thing.py246, 304
src/labthings_fastapi/websockets.py69

@rwb27 rwb27 mentioned this pull request Aug 14, 2025
@rwb27 rwb27 force-pushed the better-observeproperty-errors branch from baccfff to ed03371 Compare August 14, 2025 23:19
@rwb27 rwb27 marked this pull request as ready for review August 16, 2025 08:52
Copy link
Contributor

@julianstirling julianstirling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of things suggested to improve testing

@rwb27 rwb27 requested a review from julianstirling August 25, 2025 19:49
Copy link
Contributor

@julianstirling julianstirling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One final thing then this looks good to go

rwb27 added 12 commits August 26, 2025 15:12
This raises a helpful error if observe_property is called with an
argument that isn't a DataProperty. Usually, this will be a
functional property, which isn't observable.

Currently, tests fail because the websocket doesn't handle errors.
This allows us to test specifically that we're raising the right error
when a websocket tries to observe a non-observable property.
This now sends an error response to the websocket in
response to an `observeproperty` or `observeaction` message
with an invalid or non-observable target.

The error is tested for in test_websocket.py.
There was no test for observing something that wasn't a property, or something that wasn't an action. These are added.
I've added a default to the `getattr` call, so that we don't raise
`AttributeError` if we attempt to observe something that's
not an attribute. Instead, we will raise KeyError, which is
the same as if we attempted to observe something that's
not a property.

I think it makes sense not to distinguish between missing
entirely and not-a-property: the client shouldn't be aware
if something's a Python attribute that isn't exposed
as an affordance.

I raise KeyError because I'm thinking the string passed in
is being used as a key to look up properties.
I've gotten rid of the previous tests in favour of code that
tests the same things much more cleanly. Changes include:

1. Not using MyThing, instead using a minimal Thing
    defined in the module. This might be moved to a
    common things_to_test module at some point.
2. Using fixtures better, in particular making use of yield
    to ensure everything's cleaned up properly after each test,
    and to avoid the need for tests to call functions that do
    parts of the test (rather than just the set-up).
3. Docstrings to explain what all the tests are doing.

I also found and fixed a bug, where the websocket test for
observing an action hung indefinitely, as we were waiting for
a message that never came.

Frustratingly, there is no timeout option for Starlette test
websocket connections.
I've added actions, and now test all possible status messages.
In response to PR comments I've added more test cases that
are not observable properties or actions. These should use the same code path as "non_property".
@rwb27 rwb27 force-pushed the better-observeproperty-errors branch from 6c0d018 to 8c27f65 Compare August 26, 2025 14:12
@rwb27 rwb27 merged commit 1bf88a8 into main Aug 26, 2025
14 checks passed
@rwb27 rwb27 deleted the better-observeproperty-errors branch August 26, 2025 14:14
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.

Thing.observe_property gives a confusing error message for functional properties.
2 participants