Skip to content

Commit

Permalink
fix(api): Fix Pydantic error when parsing commands that did not succe…
Browse files Browse the repository at this point in the history
…ed (#17264)

robot-server sometimes stores, in its database, run commands that do not
have a `result`. For example, if a command failed, it has an `error`
instead of a `result`.

After the recent Pydantic v1->v2 upgrade, parsing these commands was
broken for about 1/3rd of our command types. The server would raise a
500 error. Their Pydantic models were defined like:

```
result: Optional[FooResult]
```

In Pydantic v1, that parses JSON where `result` is `null` or omitted,
but in Pydantic v2, it only parses JSON where `result` is `null`. We
need to change it to:

```
result: Optional[FooResult] = None
```
  • Loading branch information
SyntaxColoring authored Jan 14, 2025
1 parent 871aa94 commit 271ad58
Show file tree
Hide file tree
Showing 22 changed files with 23 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class CloseLid(BaseCommand[CloseLidParams, CloseLidResult, ErrorOccurrence]):

commandType: CloseLidCommandType = "absorbanceReader/closeLid"
params: CloseLidParams
result: Optional[CloseLidResult]
result: Optional[CloseLidResult] = None

_ImplementationCls: Type[CloseLidImpl] = CloseLidImpl

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Initialize(BaseCommand[InitializeParams, InitializeResult, ErrorOccurrence

commandType: InitializeCommandType = "absorbanceReader/initialize"
params: InitializeParams
result: Optional[InitializeResult]
result: Optional[InitializeResult] = None

_ImplementationCls: Type[InitializeImpl] = InitializeImpl

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class OpenLid(BaseCommand[OpenLidParams, OpenLidResult, ErrorOccurrence]):

commandType: OpenLidCommandType = "absorbanceReader/openLid"
params: OpenLidParams
result: Optional[OpenLidResult]
result: Optional[OpenLidResult] = None

_ImplementationCls: Type[OpenLidImpl] = OpenLidImpl

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

commandType: ReadAbsorbanceCommandType = "absorbanceReader/read"
params: ReadAbsorbanceParams
result: Optional[ReadAbsorbanceResult]
result: Optional[ReadAbsorbanceResult] = None

_ImplementationCls: Type[ReadAbsorbanceImpl] = ReadAbsorbanceImpl

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

commandType: AirGapInPlaceCommandType = "airGapInPlace"
params: AirGapInPlaceParams
result: Optional[AirGapInPlaceResult]
result: Optional[AirGapInPlaceResult] = None

_ImplementationCls: Type[AirGapInPlaceImplementation] = AirGapInPlaceImplementation

Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_engine/commands/get_next_tip.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class GetNextTip(BaseCommand[GetNextTipParams, GetNextTipResult, ErrorOccurrence

commandType: GetNextTipCommandType = "getNextTip"
params: GetNextTipParams
result: Optional[GetNextTipResult]
result: Optional[GetNextTipResult] = None

_ImplementationCls: Type[GetNextTipImplementation] = GetNextTipImplementation

Expand Down
4 changes: 2 additions & 2 deletions api/src/opentrons/protocol_engine/commands/liquid_probe.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ class LiquidProbe(

commandType: LiquidProbeCommandType = "liquidProbe"
params: LiquidProbeParams
result: Optional[LiquidProbeResult]
result: Optional[LiquidProbeResult] = None

_ImplementationCls: Type[LiquidProbeImplementation] = LiquidProbeImplementation

Expand All @@ -373,7 +373,7 @@ class TryLiquidProbe(

commandType: TryLiquidProbeCommandType = "tryLiquidProbe"
params: TryLiquidProbeParams
result: Optional[TryLiquidProbeResult]
result: Optional[TryLiquidProbeResult] = None

_ImplementationCls: Type[
TryLiquidProbeImplementation
Expand Down
2 changes: 1 addition & 1 deletion api/src/opentrons/protocol_engine/commands/load_lid.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class LoadLid(BaseCommand[LoadLidParams, LoadLidResult, ErrorOccurrence]):

commandType: LoadLidCommandType = "loadLid"
params: LoadLidParams
result: Optional[LoadLidResult]
result: Optional[LoadLidResult] = None

_ImplementationCls: Type[LoadLidImplementation] = LoadLidImplementation

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

commandType: LoadLidStackCommandType = "loadLidStack"
params: LoadLidStackParams
result: Optional[LoadLidStackResult]
result: Optional[LoadLidStackResult] = None

_ImplementationCls: Type[LoadLidStackImplementation] = LoadLidStackImplementation

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

commandType: LoadLiquidClassCommandType = "loadLiquidClass"
params: LoadLiquidClassParams
result: Optional[LoadLiquidClassResult]
result: Optional[LoadLiquidClassResult] = None

_ImplementationCls: Type[
LoadLiquidClassImplementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class closeGripperJaw(

commandType: closeGripperJawCommandType = "robot/closeGripperJaw"
params: closeGripperJawParams
result: Optional[closeGripperJawResult]
result: Optional[closeGripperJawResult] = None

_ImplementationCls: Type[
closeGripperJawImplementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class MoveAxesRelative(

commandType: MoveAxesRelativeCommandType = "robot/moveAxesRelative"
params: MoveAxesRelativeParams
result: Optional[MoveAxesRelativeResult]
result: Optional[MoveAxesRelativeResult] = None

_ImplementationCls: Type[
MoveAxesRelativeImplementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class MoveAxesTo(BaseCommand[MoveAxesToParams, MoveAxesToResult, ErrorOccurrence

commandType: MoveAxesToCommandType = "robot/moveAxesTo"
params: MoveAxesToParams
result: Optional[MoveAxesToResult]
result: Optional[MoveAxesToResult] = None

_ImplementationCls: Type[MoveAxesToImplementation] = MoveAxesToImplementation

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class MoveTo(BaseCommand[MoveToParams, MoveToResult, ErrorOccurrence]):

commandType: MoveToCommandType = "robot/moveTo"
params: MoveToParams
result: Optional[MoveToResult]
result: Optional[MoveToResult] = None

_ImplementationCls: Type[MoveToImplementation] = MoveToImplementation

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

commandType: openGripperJawCommandType = "robot/openGripperJaw"
params: openGripperJawParams
result: Optional[openGripperJawResult]
result: Optional[openGripperJawResult] = None

_ImplementationCls: Type[
openGripperJawImplementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class RunExtendedProfile(

commandType: RunExtendedProfileCommandType = "thermocycler/runExtendedProfile"
params: RunExtendedProfileParams
result: Optional[RunExtendedProfileResult]
result: Optional[RunExtendedProfileResult] = None

_ImplementationCls: Type[RunExtendedProfileImpl] = RunExtendedProfileImpl

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

commandType: UnsafeBlowOutInPlaceCommandType = "unsafe/blowOutInPlace"
params: UnsafeBlowOutInPlaceParams
result: Optional[UnsafeBlowOutInPlaceResult]
result: Optional[UnsafeBlowOutInPlaceResult] = None

_ImplementationCls: Type[
UnsafeBlowOutInPlaceImplementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class UnsafeDropTipInPlace(

commandType: UnsafeDropTipInPlaceCommandType = "unsafe/dropTipInPlace"
params: UnsafeDropTipInPlaceParams
result: Optional[UnsafeDropTipInPlaceResult]
result: Optional[UnsafeDropTipInPlaceResult] = None

_ImplementationCls: Type[
UnsafeDropTipInPlaceImplementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class UnsafeEngageAxes(

commandType: UnsafeEngageAxesCommandType = "unsafe/engageAxes"
params: UnsafeEngageAxesParams
result: Optional[UnsafeEngageAxesResult]
result: Optional[UnsafeEngageAxesResult] = None

_ImplementationCls: Type[
UnsafeEngageAxesImplementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class UnsafePlaceLabware(

commandType: UnsafePlaceLabwareCommandType = "unsafe/placeLabware"
params: UnsafePlaceLabwareParams
result: Optional[UnsafePlaceLabwareResult]
result: Optional[UnsafePlaceLabwareResult] = None

_ImplementationCls: Type[
UnsafePlaceLabwareImplementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class UnsafeUngripLabware(

commandType: UnsafeUngripLabwareCommandType = "unsafe/ungripLabware"
params: UnsafeUngripLabwareParams
result: Optional[UnsafeUngripLabwareResult]
result: Optional[UnsafeUngripLabwareResult] = None

_ImplementationCls: Type[
UnsafeUngripLabwareImplementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class UpdatePositionEstimators(

commandType: UpdatePositionEstimatorsCommandType = "unsafe/updatePositionEstimators"
params: UpdatePositionEstimatorsParams
result: Optional[UpdatePositionEstimatorsResult]
result: Optional[UpdatePositionEstimatorsResult] = None

_ImplementationCls: Type[
UpdatePositionEstimatorsImplementation
Expand Down

0 comments on commit 271ad58

Please sign in to comment.