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

BUG?: prepare_plan does not attempt to convert arguments to devices or plans #286

Open
tangkong opened this issue Jul 31, 2023 · 2 comments

Comments

@tangkong
Copy link

Expected Behavior

When using native python type hints with the standard bluesky.plans, plans should prepare successfully

Current Behavior

This behavior varies depending on the plan annotations.

This works (status quo):

def count(
    detectors,
    num = 1,
    delay = None,
    *,
    per_shot = None,
    md = None
):

In this case, bluesky_queueserver.prepare_plan replaces my detector as expected:

{'callable': <function count at 0x100fee040>, 'args': [[SynAxis(prefix='', name='sim_motor', read_attrs=['readback', 'setpoint'], configuration_attrs=['velocity', 'acceleration'])]], 'kwargs': {}, 'meta': {}}

This fails:

def count(
    detectors: List[Any],
    num: int = 1,
    delay: Union[Iterable[Union[float, int]], Union[float, int]] = None,
    *,
    per_shot: Optional[Callable] = None,
    md: Optional[Dict[str, Any]] = None
):

The traceback I see here complains that we can't do ophyd.Device things with str

traceback

Traceback (most recent call last):
  File "/Users/roberttk/src/bluesky/bluesky/run_engine.py", line 1528, in _run
    msg = self._plan_stack[-1].send(resp)
  File "/Users/roberttk/src/bluesky/bluesky/plans.py", line 66, in count
    _md = {'detectors': [det.name for det in detectors],
  File "/Users/roberttk/src/bluesky/bluesky/plans.py", line 66, in <listcomp>
    _md = {'detectors': [det.name for det in detectors],
AttributeError: 'str' object has no attribute 'name'

Looking further, bluesky_queueserver.prepare_plan returned a parsed plan that did not substitute the detector for its ophyd object

{'callable': <function count at 0x16deae3a0>, 'args': [['sim_motor']], 'kwargs': {}, 'meta': {}} 

and this succeeds again:

@parameter_annotation_decorator({
    "parameters": {
        "detectors": {"annotation": "typing.List[__DEVICE__]"},
        "num": {"annotation": "int"},
        "delay": {"annotation": "typing.Union[typing.Iterable[float], float]"},
    }
})
def count(
    detectors: List[Any],
    num: int = 1,
    delay: Union[Iterable[Union[float, int]], Union[float, int]] = None,
    *,
    per_shot: Optional[Callable] = None,
    md: Optional[Dict[str, Any]] = None
):

Possible Solution

Perhaps when encountering a typing.Any hint, we treat the annotation the same way we do when it's missing and attempt to convert the argument to a device or plan?

if "annotation" not in pp:
convert_all_plan_names = True if (convert_all_plan_names is None) else convert_all_plan_names
convert_all_device_names = True if (convert_all_device_names is None) else convert_all_device_names

Properly type hinting without the extra information in parameter_annotation_decorator seems tough. Is there something in the bluesky.protocols that could help us here?

Steps to Reproduce (for bugs)

see above

Context

I annotated all the bluesky plans and they stopped working in my queueserver tests

Your Environment

bluesky_queueserver 0.18, pydantic 2.1.1

@tangkong
Copy link
Author

Perhaps I'm just not using the bluesky_queueserver utilities how they're supposed to be used, but I think this is a more basic issue with being able to use the native python type hints.

@dmgav
Copy link
Contributor

dmgav commented Jul 31, 2023

As I mentioned in bluesky/bluesky#1600 (comment) , we could make the queue server replace type names from some limited fixed set (e.g. protocols.Readable, protocols.Movable etc.) by __DEVICE__. The main problems is to select right set of types so that they make sense if the Python code is checked with mypy or some other tool.

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

No branches or pull requests

2 participants