diff --git a/pylabrobot/resources/hamilton/hamilton_deck_tests.py b/pylabrobot/resources/hamilton/hamilton_deck_tests.py index d41199de305..c2d179421be 100644 --- a/pylabrobot/resources/hamilton/hamilton_deck_tests.py +++ b/pylabrobot/resources/hamilton/hamilton_deck_tests.py @@ -1,7 +1,7 @@ import textwrap import unittest -from pylabrobot.resources import TipRack +from pylabrobot.resources import Deck, TipRack from pylabrobot.resources.corning import ( cor_96_wellplate_360uL_Fb, ) @@ -94,6 +94,30 @@ def test_teaching_rack_excluded_by_nominal_volume_search(self): self.assertEqual(len(matches), 5) self.assertNotIn("teaching_tip_rack", {tr.name for tr in matches}) + def test_get_trash_area96_survives_serialization_round_trip(self): + """`serialize()` encodes the 96 trash as a child and sets `with_trash96=False`, so the + rebuilt deck never runs the `with_trash96` branch in `__init__`. `get_trash_area96()` must + still resolve it from the child tree rather than raising.""" + deck = self.build_layout() + original_location = deck.get_trash_area96().get_location_wrt(deck) + + deck2 = Deck.deserialize(deck.serialize()) + + self.assertIn("trash_core96", [child.name for child in deck2.children]) + self.assertEqual(deck2.get_trash_area96().get_location_wrt(deck2), original_location) + + def test_get_trash_area96_raises_after_clear_include_trash(self): + """`clear(include_trash=True)` unassigns the 96 trash, so `get_trash_area96()` must raise + rather than hand back the now-orphaned resource.""" + deck = self.build_layout() + deck.get_trash_area96() # resolves while still assigned + + deck.clear(include_trash=True) + + self.assertNotIn("trash_core96", [child.name for child in deck.children]) + with self.assertRaises(RuntimeError): + deck.get_trash_area96() + def test_assign_gigantic_resource(self): stanley_cup = StanleyCup_QUENCHER_FLOWSTATE_TUMBLER(name="HUGE") deck = STARLetDeck() diff --git a/pylabrobot/resources/hamilton/hamilton_decks.py b/pylabrobot/resources/hamilton/hamilton_decks.py index 8c0b9d3fff9..b0357a5cea8 100644 --- a/pylabrobot/resources/hamilton/hamilton_decks.py +++ b/pylabrobot/resources/hamilton/hamilton_decks.py @@ -458,12 +458,11 @@ def __init__( origin=origin, ) - self._trash96: Optional[Trash] = None if with_trash96: # got this location from a .lay file, but will probably need to be adjusted by the user. - self._trash96 = Trash("trash_core96", size_x=122.4, size_y=82.6, size_z=0) # size of tiprack + trash96 = Trash("trash_core96", size_x=122.4, size_y=82.6, size_z=0) # size of tiprack self.assign_child_resource( - resource=self._trash96, + resource=trash96, location=Coordinate(x=-42.0 - 16.2, y=120.3 - 14.3, z=216.4), ) @@ -548,11 +547,11 @@ def rails_to_location(self, rails: int) -> Coordinate: return Coordinate(x=x, y=63, z=100) def get_trash_area96(self) -> Trash: - if self._trash96 is None: + if not self.has_resource("trash_core96"): raise RuntimeError( "Trash area for 96-well plates was not created. Initialize with `with_trash96=True`." ) - return self._trash96 + return cast(Trash, self.get_resource("trash_core96")) def clear(self, include_trash: bool = False): """Clear the deck, removing all resources except the trash areas and the waste block."""