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

refactor(api): add explicit Flex hardware protocol #14091

Merged
merged 15 commits into from
Dec 12, 2023

Conversation

fsinapi
Copy link
Contributor

@fsinapi fsinapi commented Dec 5, 2023

Overview

We currently differentiate between the OT3 and OT2 API classes with a simple isinstance check. This works okay when the simulator for each class is an instance of the normal hardware API, but there are a few reasons that we want to move over to having a separate simulator class for each API that does not depend on the hardware-specific code. In that case, checking the types to differentiate the robot model doesn't work.

This PR establishes extensions to the Hardware API MyPy protocols in order to explicitly differentiate an OT-2 from an OT-3. It also adds a function that returns the enumerated model of the robot (OT2 or FLEX). This is preferable to type-checking the class because isinstance is slow/not recommended on Protocol classes.

Test Plan

Check that

  • Calibration still works
  • Model-specific endpoints are filtered properly (can't send GET /instruments on OT-2)
  • Protocol engine can run a protocol fine

Changelog

Review requests

Risk assessment

Medium. Touches some core stuff but doesn't really change any behavior.

@fsinapi fsinapi requested review from a team December 5, 2023 16:07
@fsinapi fsinapi self-assigned this Dec 5, 2023
@fsinapi fsinapi requested a review from a team as a code owner December 5, 2023 16:07
Copy link
Member

@sfoster1 sfoster1 left a comment

Choose a reason for hiding this comment

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

Love it, big step forward. Can we do enum literal returns for the robot_type and get mypy automatically narrowing union types?

@@ -302,6 +302,9 @@ async def build_hardware_simulator(
def __repr__(self) -> str:
return "<{} using backend {}>".format(type(self), type(self._backend))

def get_robot_type(self) -> RobotTypeEnum:
Copy link
Member

Choose a reason for hiding this comment

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

If you define this to return an enum literal Literal[RobotTypeEnum.OT2], and define the Flex protocol's version of the method to return Literal[RobotTypeEnum.FLEX] (or however we spelled it), then in theory that should let the union of the two protocols get narrowed by the result of calling get_robot_type() which should remove some casts

Copy link
Contributor Author

Choose a reason for hiding this comment

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

raise HardwareNotSupportedError(
error_msg or "This command is supported by OT-3 only."
)

return hardware_api
return hardware_api # type: ignore
Copy link
Member

Choose a reason for hiding this comment

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

here this cast should be removable if you do the enum-literal thing above

Copy link
Contributor Author

Choose a reason for hiding this comment

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

MyPy disagrees :(

Copy link

codecov bot commented Dec 6, 2023

Codecov Report

Merging #14091 (b0351d9) into edge (a32571e) will decrease coverage by 0.01%.
The diff coverage is n/a.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             edge   #14091      +/-   ##
==========================================
- Coverage   70.40%   70.39%   -0.01%     
==========================================
  Files        2510     2510              
  Lines       71201    71188      -13     
  Branches     8971     8971              
==========================================
- Hits        50127    50113      -14     
  Misses      18880    18880              
- Partials     2194     2195       +1     
Flag Coverage Δ
app 67.63% <ø> (-0.01%) ⬇️
g-code-testing 96.44% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
api/src/opentrons/hardware_control/__init__.py 100.00% <ø> (ø)
api/src/opentrons/hardware_control/api.py 82.42% <ø> (ø)
api/src/opentrons/hardware_control/ot3api.py 79.58% <ø> (ø)
...c/opentrons/hardware_control/protocols/__init__.py 100.00% <ø> (ø)
...entrons/hardware_control/protocols/calibratable.py 55.55% <ø> (ø)
...entrons/hardware_control/protocols/configurable.py 44.44% <ø> (-24.31%) ⬇️
...ardware_control/protocols/instrument_configurer.py 63.15% <ø> (ø)
...trons/hardware_control/protocols/liquid_handler.py 60.00% <ø> (-4.71%) ⬇️
...ns/hardware_control/protocols/motion_controller.py 51.85% <ø> (+0.12%) ⬆️

... and 1 file with indirect coverage changes

@fsinapi fsinapi requested a review from sfoster1 December 6, 2023 21:16
Copy link
Member

@sfoster1 sfoster1 left a comment

Choose a reason for hiding this comment

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

Can we remove TYPE_CHECKING? otherwise looks good


if TYPE_CHECKING:
from opentrons.hardware_control.ot3api import OT3API
from opentrons.hardware_control import HardwareControlAPI
from opentrons.hardware_control import HardwareControlAPI, OT3HardwareControlAPI
Copy link
Member

Choose a reason for hiding this comment

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

here too - shouldn't need TYPE_CHECKING?

@@ -49,7 +49,7 @@
from robot_server.subsystems.router import status_route_for, update_route_for

if TYPE_CHECKING:
from opentrons.hardware_control.ot3api import OT3API
from opentrons.hardware_control import OT3HardwareControlAPI
Copy link
Member

Choose a reason for hiding this comment

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

no TYPE_CHECKING?

Copy link
Member

@sfoster1 sfoster1 left a comment

Choose a reason for hiding this comment

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

Awesome, let's do it!

# Conflicts:
#	api/tests/opentrons/protocol_engine/execution/test_labware_movement_handler.py
@fsinapi fsinapi merged commit 85dfebe into edge Dec 12, 2023
38 checks passed
@fsinapi fsinapi deleted the RET-1385-explicit-robot-type-protocol-method branch December 12, 2023 16:10
ncdiehl11 pushed a commit that referenced this pull request Dec 19, 2023
* pass around mypy protocol instead of OT3API
* Explicit parameterization of config types
* Fix some tests with the protocol engine related to protocol changes
* Added a `FlexInstrumentConfigurer` for flex-specific configuration extensions
* Update test mocks to differentiate ot2/ot3 hardware
* Modifed `get_robot_type` to be parameterized on some singleton class types for robot models
* remove unnecessary TYPE_CHECKING import
* Add tip presence protocol functions to satisfy edits to tip manager
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.

2 participants