Skip to content

Update client to support better typing - #1663

Draft
oliwenmandiamond wants to merge 3 commits into
support_star_argsfrom
update_client_to_support_better_typing
Draft

Update client to support better typing#1663
oliwenmandiamond wants to merge 3 commits into
support_star_argsfrom
update_client_to_support_better_typing

Conversation

@oliwenmandiamond

@oliwenmandiamond oliwenmandiamond commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

When used with DiamondLightSource/dodal#2050

Without this change, the typing looks like this:

>>> bc.plans.step_grid_scan
step_grid_scan(
    detectors: list[Readable | AsyncReadable],
    trajectory: list[Any],
    extra_trajectories: list[list[Any]] | None = None,
    snake_axes: bool = True,
    metadata: dict | None = None
)

Which is not useful for a user and is wrong. E.g no *args, uses lists rather tuple, has Any.

With this change, demo of concept using BlueapiClient

>>> pl.step_grid_scan
step_grid_scan(
    detectors: Sequence[Readable | AsyncReadable],
    trajectory: tuple[Movable[float], float, float, float],
    *extra_trajectories: tuple[Movable[float], float, float, float],
    snake_axes: bool = True,
    metadata: dict[str, Any] | None = None
)

1 axis

>>> pl.step_grid_scan([], (devs.dd.y, 0, 0.5, 0.1))
Run started
    2026-09-10 16:49:22 - Point 1: {'dd-y': 0.0}
    2026-09-10 16:49:22 - Point 2: {'dd-y': 0.1}
    2026-09-10 16:49:22 - Point 3: {'dd-y': 0.2}
    2026-09-10 16:49:22 - Point 4: {'dd-y': 0.3}
    2026-09-10 16:49:22 - Point 5: {'dd-y': 0.4}
    2026-09-10 16:49:22 - Point 6: {'dd-y': 0.5}
Run complete:  success

2 axes

>>> pl.step_grid_scan([], (devs.dd.y, 0, 0.5, 0.1), (devs.dd.x, 0, 10, 1))
Run started
    2026-09-10 16:49:51 - Point 1: {'dd-y': 0.0, 'dd-x': 0.0}
    2026-09-10 16:49:51 - Point 2: {'dd-y': 0.0, 'dd-x': 1.0}
    2026-09-10 16:49:51 - Point 3: {'dd-y': 0.0, 'dd-x': 2.0}
    2026-09-10 16:49:51 - Point 4: {'dd-y': 0.0, 'dd-x': 3.0}
    2026-09-10 16:49:51 - Point 5: {'dd-y': 0.0, 'dd-x': 4.0}
    2026-09-10 16:49:51 - Point 6: {'dd-y': 0.0, 'dd-x': 5.0}
    2026-09-10 16:49:51 - Point 7: {'dd-y': 0.0, 'dd-x': 6.0}
    ...
    2026-09-10 16:49:52 - Point 64: {'dd-y': 0.5, 'dd-x': 2.0}
    2026-09-10 16:49:52 - Point 65: {'dd-y': 0.5, 'dd-x': 1.0}
    2026-09-10 16:49:52 - Point 66: {'dd-y': 0.5, 'dd-x': 0.0}
Run complete:  success

3 axes

>>> pl.step_grid_scan([], (devs.dd.y, 0, 0.5, 0.1), (devs.dd.x, 0, 10, 1), (devs.pgm.energy, 500, 501, 0.1))
Run started
    2026-09-10 16:50:46 - Point 1: {'dd-y': 0.0, 'dd-x': 0.0, 'pgm-energy': 500.0}
    2026-09-10 16:50:46 - Point 2: {'dd-y': 0.0, 'dd-x': 0.0, 'pgm-energy': 500.1}
    2026-09-10 16:50:46 - Point 3: {'dd-y': 0.0, 'dd-x': 0.0, 'pgm-energy': 500.2}
    2026-09-10 16:50:46 - Point 4: {'dd-y': 0.0, 'dd-x': 0.0, 'pgm-energy': 500.3}
    ...
    2026-09-10 16:50:48 - Point 721: {'dd-y': 0.5, 'dd-x': 0.0, 'pgm-energy': 500.5}
    2026-09-10 16:50:48 - Point 722: {'dd-y': 0.5, 'dd-x': 0.0, 'pgm-energy': 500.4}
    2026-09-10 16:50:48 - Point 723: {'dd-y': 0.5, 'dd-x': 0.0, 'pgm-energy': 500.3}
    2026-09-10 16:50:48 - Point 724: {'dd-y': 0.5, 'dd-x': 0.0, 'pgm-energy': 500.2}
    2026-09-10 16:50:48 - Point 725: {'dd-y': 0.5, 'dd-x': 0.0, 'pgm-energy': 500.1}
    2026-09-10 16:50:48 - Point 726: {'dd-y': 0.5, 'dd-x': 0.0, 'pgm-energy': 500.0}
Run complete:  success

Wrong number of args (would like this error message to be improved as there is a lot more useful error messages within the plan telling you how to fix, see here for examples.)

>>> pl.step_grid_scan([], (devs.dd.y, 0, 0.5, 0.1), (devs.dd.x, 0, 10, 1), (devs.pgm.energy, 500))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
    pl.step_grid_scan([], (devs.dd.y, 0, 0.5, 0.1), (devs.dd.x, 0, 10, 1), (devs.pgm.energy, 500)
    ~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/scratch/bluesky_development/blueapi/src/blueapi/client/client.py", line 158, in __call__
    match self._client.run_task(req):
          ~~~~~~~~~~~~~~~~~~~~~^^^^^
  File "/scratch/bluesky_development/blueapi/.venv/lib/python3.14/site-packages/observability_utils/tracing/decorators.py", line 151, in wrapper
    return func(*args, **kwargs)
  File "/scratch/bluesky_development/blueapi/src/blueapi/client/client.py", line 583, in run_task
    task_response = self._rest.create_task(task)
  File "/scratch/bluesky_development/blueapi/src/blueapi/client/rest.py", line 256, in create_task
    return self._request_and_deserialize(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        "/tasks",
        ^^^^^^^^^
    ...<3 lines>...
        data=task.model_dump(mode="json", fallback=_task_model_fallback),
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "/scratch/bluesky_development/blueapi/.venv/lib/python3.14/site-packages/observability_utils/tracing/decorators.py", line 151, in wrapper
    return func(*args, **kwargs)
  File "/scratch/bluesky_development/blueapi/src/blueapi/client/rest.py", line 340, in _request_and_deserialize
    raise exception
blueapi.client.rest.InvalidParametersError: [ParameterError(loc=['body', 'params', 'extra_trajectories', 1, 2], msg='Field required', type='missing', input=['pgm.energy', 500]), ParameterError(loc=['body', 'params', 'extra_trajectories', 1, 3], msg='Field required', type='missing', input=['pgm.energy', 500])]

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.

1 participant