Skip to content
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

Enumerate devices in plan schemas #526

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/blueapi/core/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,18 @@ def valid(cls, value):
def __modify_schema__(
cls, field_schema: dict[str, Any], field: ModelField | None
):
target_type = f"{target.__module__}.{target.__qualname__}"
if field:
field_schema.update(
{"type": f"{target.__module__}.{target.__qualname__}"}
{
"type": "string",
"enum": [
k
for k, v in self.devices.items()
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't believe there is any guaranteed order for registration of plans and devices, meaning that some devices may not be registered yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Isn't this method only called when the schema is called for? Does anything query schemas before everything is loaded?

Copy link
Contributor

Choose a reason for hiding this comment

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

No, the schemas are generated upfront when the plan is registered with the context. I don't mind if you make them lazy instead though.

if isinstance(v, target)
],
"target_type": target_type,
}
)

self._reference_cache[target] = Reference
Expand Down
13 changes: 9 additions & 4 deletions tests/core/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,21 @@ def test_add_plan(empty_context: BlueskyContext, plan: PlanGenerator) -> None:


def test_generated_schema(
empty_context: BlueskyContext,
devicey_context: BlueskyContext,
):
def demo_plan(foo: int, mov: Movable) -> MsgGenerator: # type: ignore
...

empty_context.plan(demo_plan)
schema = empty_context.plans["demo_plan"].model.schema()
devicey_context.plan(demo_plan)
schema = devicey_context.plans["demo_plan"].model.schema()
assert schema["properties"] == {
"foo": {"title": "Foo", "type": "integer"},
"mov": {"title": "Mov", "type": "bluesky.protocols.Movable"},
"mov": {
"title": "Mov",
"type": "string",
"target_type": "bluesky.protocols.Movable",
"enum": ["sim"],
},
}


Expand Down
16 changes: 15 additions & 1 deletion tests/service/test_rest_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,21 @@ def test_get_plan_with_device_reference(handler: Handler, client: TestClient) ->
"title": "Delay",
},
"detectors": {
"items": {"type": "bluesky.protocols.Readable"},
"items": {
"type": "string",
"target_type": "bluesky.protocols.Readable",
"enum": [
"sample_pressure",
"x_err",
"theta",
"z",
"y",
"x",
"current_det",
"image_det",
"sample_temperature",
],
},
"title": "Detectors",
"type": "array",
},
Expand Down
Loading