Skip to content

Commit

Permalink
more validation stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
sfoster1 committed Dec 5, 2024
1 parent 2170735 commit dcd9257
Show file tree
Hide file tree
Showing 37 changed files with 66 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class InitializeParams(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


ReadAbsorbanceCommandType = Literal["absorbanceReader/read"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class CalibrateGripperParamsJaw(Enum): # noqa: D101
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_engine/commands/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CommandIntent(str, enum.Enum):


def _pop_default(s: Dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class BaseCommandCreate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class ConfigureForVolumeParams(PipetteIdMixin):
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_engine/commands/dispense.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class DispenseParams(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class DispenseInPlaceParams(PipetteIdMixin, DispenseVolumeMixin, FlowRateMixin):
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_engine/commands/drop_tip.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class DropTipParams(PipetteIdMixin):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class DropTipInPlaceParams(PipetteIdMixin):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class WaitForTemperatureParams(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_engine/commands/home.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class HomeParams(BaseModel):
Expand Down
6 changes: 3 additions & 3 deletions api/src/opentrons/protocol_engine/commands/liquid_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


LiquidProbeCommandType = Literal["liquidProbe"]
Expand Down Expand Up @@ -375,9 +375,9 @@ class TryLiquidProbe(
params: TryLiquidProbeParams
result: Optional[TryLiquidProbeResult]

_ImplementationCls: Type[
_ImplementationCls: Type[TryLiquidProbeImplementation] = (
TryLiquidProbeImplementation
] = TryLiquidProbeImplementation
)


class LiquidProbeCreate(BaseCommandCreate[LiquidProbeParams]):
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_engine/commands/load_labware.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class LoadLabwareParams(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class LoadLiquidClassParams(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_engine/commands/load_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class LoadModuleParams(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_engine/commands/load_pipette.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class LoadPipetteParams(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_engine/commands/move_labware.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


# Extra buffer on top of minimum distance to move to the right
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class MoveToAddressableAreaForDropTipParams(PipetteIdMixin, MovementMixin):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class WellLocationMixin(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class closeGripperJawParams(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class MoveAxesRelativeParams(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class MoveAxesToParams(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class MoveToParams(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class SavePositionParams(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class WaitForTemperatureParams(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class ProfileStep(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class RunProfileStepParams(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class SetTargetBlockTemperatureParams(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_engine/commands/touch_tip.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class TouchTipParams(PipetteIdMixin, WellLocationMixin):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class UnsafeDropTipInPlaceParams(PipetteIdMixin):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class VerifyTipPresenceParams(PipetteIdMixin):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class WaitForDurationParams(BaseModel):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@


def _remove_default(s: dict[str, Any]) -> None:
s.pop("default")
s.pop("default", None)


class WaitForResumeParams(BaseModel):
Expand Down
22 changes: 15 additions & 7 deletions api/src/opentrons/protocols/labware.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import logging
import json

from pathlib import Path
from typing import Any, AnyStr, Dict, Optional, Union

from pydantic_core import from_json
import jsonschema # type: ignore

from opentrons_shared_data import load_shared_data, get_shared_data_root
Expand Down Expand Up @@ -103,7 +103,7 @@ def save_definition(


def verify_definition(
contents: Union[AnyStr, LabwareDefinition, Dict[str, Any]]
contents: Union[AnyStr, LabwareDefinition, Dict[str, Any]],
) -> LabwareDefinition:
"""Verify that an input string is a labware definition and return it.
Expand All @@ -114,14 +114,23 @@ def verify_definition(
:raises jsonschema.ValidationError: If the definition is not valid.
:returns: The parsed definition
"""
schema_body = load_shared_data("labware/schemas/2.json").decode("utf-8")
labware_schema_v2 = json.loads(schema_body)
schemata_by_version = {
2: from_json(load_shared_data("labware/schemas/2.json").decode("utf-8")),
3: from_json(load_shared_data("labware/schemas/3.json").decode("utf-8")),
}

if isinstance(contents, dict):
to_return = contents
else:
to_return = json.loads(contents)
jsonschema.validate(to_return, labware_schema_v2)
to_return = from_json(contents)
try:
schema = schemata_by_version[to_return.get("schemaVersion", None)]
except KeyError:
raise RuntimeError(
f'Invalid or unknown labware schema version {to_return.get("schemaVersion", None)}'
)
jsonschema.validate(to_return, schema)

# we can type ignore this because if it passes the jsonschema it has
# the correct structure
return to_return # type: ignore[return-value]
Expand Down Expand Up @@ -176,7 +185,6 @@ def _get_labware_definition_from_bundle(
def _get_standard_labware_definition(
load_name: str, namespace: Optional[str] = None, version: Optional[int] = None
) -> LabwareDefinition:

if version is None:
checked_version = 1
else:
Expand Down
Loading

0 comments on commit dcd9257

Please sign in to comment.