Skip to content

Commit

Permalink
Add deprecation warning to inject (#854)
Browse files Browse the repository at this point in the history
* Add deprecation warning to inject

* Handle inject deprecation in tests

* Docs page about including devices in plans
  • Loading branch information
callumforrester authored Oct 18, 2024
1 parent ecc1a20 commit 0f8989e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
37 changes: 37 additions & 0 deletions docs/how-to/include-devices-in-plans.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Include Devices in Plans

There are two 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 or 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
16 changes: 16 additions & 0 deletions tests/common/test_coordination.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ def test_group_uid(group: str):
assert not gid.endswith(f"{group}-")


@pytest.mark.filterwarnings("ignore::DeprecationWarning")
def test_inject_returns_value():
assert inject("foo") == "foo"


@pytest.mark.filterwarnings("ignore::DeprecationWarning")
def test_type_checking_ignores_inject():
def example_function(x: Movable = inject("foo")) -> MsgGenerator: # noqa: B008
yield from {}
Expand All @@ -25,3 +31,13 @@ def example_function(x: Movable = inject("foo")) -> MsgGenerator: # noqa: B008
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 {}

0 comments on commit 0f8989e

Please sign in to comment.