Skip to content

Deserialise plan method arguments by replacing types instead of building custom validators - #154

Merged
DiamondJoseph merged 4 commits into
DiamondLightSource:mainfrom
tpoliaw:dev_validators
May 4, 2023
Merged

DiamondJoseph merged 4 commits into
DiamondLightSource:mainfrom
tpoliaw:dev_validators

Conversation

@tpoliaw

@tpoliaw tpoliaw commented Apr 27, 2023

Copy link
Copy Markdown
Contributor

Proof of concept for more flexible type checking/conversion of device types.

Should in theory fix #119

Not tried passing Spec[Movable] yet but if Spec is defined as something like

class Spec(Generic(TypeVar('T')):
    axis: T

it should also fix #118

@callumforrester

Copy link
Copy Markdown
Contributor

Makes sense and much more concise, good job! We also need to make sure we don't lose the functionality introduced by #131

@tpoliaw

tpoliaw commented Apr 28, 2023

Copy link
Copy Markdown
Contributor Author

RE the three failing tests, why is it invalid to have untyped arguments? I can imagine it's not really that useful but I can't see why it'd be a problem.

@tpoliaw

tpoliaw commented Apr 28, 2023

Copy link
Copy Markdown
Contributor Author

We also need to make sure we don't lose the functionality introduced by #131

Should now handle concrete types as well as protocols.

Comment thread src/blueapi/core/context.py Resolved
Comment thread tests/core/test_context.py Resolved
@callumforrester

Copy link
Copy Markdown
Contributor

RE the three failing tests, why is it invalid to have untyped arguments? I can imagine it's not really that useful but I can't see why it'd be a problem.

The idea is that the arguments are essentially turned into an API schema. So it's not a problem to have untyped arguments if we have a default type of "any valid JSON". I'm on the fence about allowing that, any strong opinions @keithralphs @DiamondJoseph?

@codecov

codecov Bot commented May 2, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #154 (2573f7f) into main (bcfabb7) will increase coverage by 8.53%.
The diff coverage is 90.90%.

@@            Coverage Diff             @@
##             main     #154      +/-   ##
==========================================
+ Coverage   74.47%   83.00%   +8.53%     
==========================================
  Files          37       39       +2     
  Lines        1050     1024      -26     
==========================================
+ Hits          782      850      +68     
+ Misses        268      174      -94     
Impacted Files Coverage Δ
src/blueapi/utils/__init__.py 100.00% <ø> (ø)
src/blueapi/core/context.py 95.45% <90.00%> (+20.45%) ⬆️
src/blueapi/core/bluesky_types.py 88.88% <100.00%> (+1.01%) ⬆️

... and 10 files with indirect coverage changes

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@DiamondJoseph

Copy link
Copy Markdown
Contributor

I think enforcing typing makes our life easier long term, as plans are eventually going under code review then sitting in repositories for years while support staff change until they're updated and makes potential auto-generation for GUIs etc. possible down the line. It seems a very small requirement that is easily enforced and has potential benefit.

@tpoliaw

tpoliaw commented May 2, 2023

Copy link
Copy Markdown
Contributor Author

Checks to enforce at least an Any annotation are back in

@callumforrester callumforrester left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Noted a few things but nothing merge-blocking. Thanks for this!

Comment thread tests/core/test_context.py Resolved
return get_type_hints(func).get("return") is MsgGenerator
try:
return get_type_hints(func).get("return") is MsgGenerator
except TypeError:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What would cause a TypeError here? Mind putting in a comment?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

>>> from typing import Union, get_type_hints
>>> get_type_hints(Union)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/scratch/usr/lib/python3.11/typing.py", line 2330, in get_type_hints
    raise TypeError('{!r} is not a module, class, method, '
TypeError: typing.Union is not a module, class, method, or function.

which means if your startup file imports Union (or Optional), the blueapi worker falls over.

Plans that reference bluesky types need an intermediate step in the
deserialisation process to allow them to be referenced by name only.
Types in plan signatures are now converted to a runtime generated type
that can access the context where the plan will be run allowing pydantic
to ensure that devices are present and are of the correct type.
Comment thread src/blueapi/core/context.py Resolved
tpoliaw added 3 commits May 4, 2023 12:05
Functions took an instance of context as an argument and were only
called from within existing context methods so it makes more sense for
them to be instance methods of the BlueskyContext instead of standalone
functions.
@DiamondJoseph
DiamondJoseph merged commit 6557da5 into DiamondLightSource:main May 4, 2023
@tpoliaw
tpoliaw deleted the dev_validators branch May 4, 2023 12:23
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.

Move plan parameter deserialization is broken Support scanspec deserialization in plans with pydantic

3 participants