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

Add deprecation warning to inject #854

Merged
merged 6 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
60 changes: 60 additions & 0 deletions docs/how-to/include-devices-in-plans.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Include Devices in Plans

There are three main ways to include dodal devices in plans

## 1. Pass as Argument

```python
import bluesky.plans as bp

from bluesky.protocols import Readable
from bluesky.utils import MsgGenerator
from dodal.beamlines import i22

def my_plan(detector: Readable) -> MsgGenerator:
yield from bp.count([detector])

RE(my_plan(i22.saxs()))
```

This is useful for generic plans that can run on a variety of devices and are not designed with any specific device in mind.

## 2. Pass as Default Argument

```python
import bluesky.plans as bp

from bluesky.protocols import Readable
from bluesky.utils import MsgGenerator
from dodal.beamlines import i22

def my_plan(detector: Readable = i22.saxs(connect_immediately=False)) -> MsgGenerator:
yield from bp.count([detector])

RE(my_plan()))
```

This is useful for plans that will usually, but not exclusively, use the same device.

## 3. Instantiate Within the Plan

```python
import bluesky.plans as bp
import ophyd_async.plan_stubs as ops

from bluesky.protocols import Readable
from bluesky.utils import MsgGenerator
from dodal.beamlines import i22

def my_plan() -> MsgGenerator:
detector = i22.saxs(connect_immediately=False)
# We need to connect via the stub instead of directly
# so that the RunEngine performs the connection in the
# correct event loop
yield from ops.ensure_connected(detector)
yield from bp.count([detector])

RE(my_plan()))
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 want to encourage this, I want to always have the device as a default argument.
This prevents any metrics/tracing/logging/documenting on the arguments of the plan, leads to N different plans that are just using a different device, makes it easier for the scope of the device to be managed incorrectly and cause issues (conditionally instantiating my device as mock/connecting my device?).
I think the Finder in GDA was a mistake that made every momentary decision to use it slightly easier and every subsequent attempt to extract what the hell was going on a lot harder.

I think it's risking making debugging a lot harder for a small benefit in verbosity, so I would like more attention paid to the above and comments about the benefits of doing it that way.

I'm prepared to be told I'm getting in the way of writing plans for this standpoint. I really don't like it.

Copy link
Contributor

Choose a reason for hiding this comment

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

It also means we can call ensure_connected at the top of the plan and fail fast if any device won't be available, rather than after completing the first iteration of the fastest axis: and we can call connect on all of the devices in parallel rather than on each one as we encounter it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be honest I broadly agree. Dependency injection is a good thing and, in my opinion, having lots of defaulted parameters is not a particularly terrible thing. Maybe we take this out for now and add it later if demand calls for it?

```

This is useful for plans that are designed to only ever work with a specific device.
20 changes: 20 additions & 0 deletions src/dodal/common/coordination.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import uuid
import warnings
from textwrap import dedent
from typing import Any

from dodal.common.types import Group
Expand Down Expand Up @@ -37,4 +39,22 @@ def scan(x: Movable = inject("stage_x"), start: float = 0.0 ...)

"""

warnings.warn(
dedent("""
Inject is deprecated, users are now expected to call the device factory
functions in dodal directly, these will cache devices as singletons after
they have been called once. For example:

from bluesky.protocols import Readable
from bluesky.utils import MsgGenerator
from dodal.beamlines import i22

def my_plan(detector: Readable = i22.saxs(connect_immediately=False)) -> MsgGenerator:
...

Where previously the default would have been inject("saxs")
"""),
DeprecationWarning,
stacklevel=2,
)
return name
28 changes: 20 additions & 8 deletions tests/common/test_coordination.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@ def test_group_uid(group: str):


def test_type_checking_ignores_inject():
def example_function(x: Movable = inject("foo")) -> MsgGenerator: # noqa: B008
yield from {}

# These asserts are sanity checks
# the real test is whether this test passes type checking
x: Parameter = signature(example_function).parameters["x"]
assert x.annotation == Movable
assert x.default == "foo"
with pytest.raises(DeprecationWarning):

def example_function(x: Movable = inject("foo")) -> MsgGenerator: # noqa: B008
yield from {}

# These asserts are sanity checks
# the real test is whether this test passes type checking
x: Parameter = signature(example_function).parameters["x"]
assert x.annotation == Movable
assert x.default == "foo"


def test_inject_is_deprecated():
with pytest.raises(
DeprecationWarning,
match="Inject is deprecated, users are now expected to call the device factory",
):

def example_function(x: Movable = inject("foo")) -> MsgGenerator: # noqa: B008
yield from {}
Loading