From 3f3af8032cdaed1f66b920db2be84bfd4deb0d01 Mon Sep 17 00:00:00 2001 From: Rusheb Shah Date: Sun, 6 Oct 2024 10:15:03 +0100 Subject: [PATCH] Add as_dict utility method to Score (#653) * Add as_dict utility method to Score * Remove newline * Update CHANGELOG.md --------- Co-authored-by: jjallaire --- CHANGELOG.md | 4 ++++ src/inspect_ai/scorer/_metric.py | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a08747d3..95557e5d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- Add `as_dict()` utility method to `Score` + ## v0.3.40 (6 October 2024) - Add `interactive` option to `web_browser()` for disabling interactive tools (clicking, typing, and submitting forms). diff --git a/src/inspect_ai/scorer/_metric.py b/src/inspect_ai/scorer/_metric.py index 3240aa2d2..a1d26e52f 100644 --- a/src/inspect_ai/scorer/_metric.py +++ b/src/inspect_ai/scorer/_metric.py @@ -90,6 +90,13 @@ def as_bool(self) -> bool: """Read the score as a boolean.""" return bool(self._as_scalar()) + def as_dict(self) -> dict[str, str | int | float | bool | None]: + """Read the score as a dictionary.""" + if isinstance(self.value, dict): + return self.value + else: + raise ValueError("This score is not a dictionary") + def _as_scalar(self) -> str | int | float | bool: if isinstance(self.value, str | int | float | bool): return self.value