Skip to content

Raise better errors in observe_property, and test them. #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

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.45%

Your code coverage diff: 0.14% ▴

Uncovered files and lines
FileLines
src/labthings_fastapi/thing.py245, 303
src/labthings_fastapi/websockets.py69

@rwb27 rwb27 mentioned this pull request Aug 14, 2025
rwb27 added 8 commits August 15, 2025 00:19
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.
@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
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.
1 participant