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 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
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 {}
Loading