HPInc / HP-Digital-Microfluidics

HP Digital Microfluidics Software Platform and Libraries
MIT License
3 stars 1 forks source link

Exception thrown on nonexistent pad #249

Open EvanKirshenbaum opened 8 months ago

EvanKirshenbaum commented 8 months ago

While commenting on #243 re converting between a putative coord type to pad, it dawned on me that the current implementation of referring to pads by coordinates is buggy.

The result of (x,y) is defined by

        fn = Functions["COORD"]
        fn.format_type_expr_using(2, lambda x,y: f"({x}, {y})")
        def find_pad(env: Environment, x: int, y: int) -> Pad:
            board = env.board
            return board.pad_at(x, y)
        fn.register((Type.INT, Type.INT), Type.PAD, WithEnv(find_pad))

where Board.pad_at() is simply

    def pad_at(self, x: int, y: int) -> Pad:
        return self.pads[XYCoord(x,y)]

which will throw an (uncaught) KeyError if you refer to something off the board.

At the very least find_pad() should catch KeyError and transform it to an EvaluationError.

Note that there's a distinction that will be made between the situation in which the coordinate refers off the board (which will throw an exception) and the situation in which it refers to a dead pad, which will return a pad whose (Python) exists attribute is False. This implies that it should be possible to ask

if p is dead { ... }
Migrated from internal repository. Originally created by @EvanKirshenbaum on Feb 16, 2023 at 10:51 AM PST.