@@ -26,7 +26,9 @@ class ScorerMetrics:
2626 """
2727 Base dataclass for storing scorer evaluation metrics.
2828
29- This class provides methods for serializing metrics to JSON and loading them from JSON files.
29+ This class provides methods for serializing metrics to JSON strings (see
30+ :meth:`to_json`) and loading them from JSON files on disk (see
31+ :meth:`from_json_file`).
3032
3133 Args:
3234 num_responses (int): Total number of responses evaluated.
@@ -48,23 +50,34 @@ class ScorerMetrics:
4850
4951 def to_json (self ) -> str :
5052 """
51- Convert the metrics to a JSON string.
53+ Serialize this metrics instance to a JSON string.
54+
55+ This is the canonical serialization entry point for ``ScorerMetrics`` and its
56+ subclasses. Pair it with :meth:`from_json_file` (which reads a JSON file written
57+ from this string, optionally wrapped in a ``"metrics"`` key) for round-trip
58+ (de)serialization.
5259
5360 Returns:
5461 str: The JSON string representation of the metrics.
5562 """
5663 return json .dumps (asdict (self ))
5764
5865 @classmethod
59- def from_json (cls : type [T ], file_path : Union [str , Path ]) -> T :
66+ def from_json_file (cls : type [T ], file_path : Union [str , Path ]) -> T :
6067 """
61- Load the metrics from a JSON file.
68+ Load a metrics instance from a JSON file on disk.
69+
70+ This is the canonical deserialization entry point for ``ScorerMetrics`` and its
71+ subclasses. It accepts a *file path* (string or ``Path``), not a JSON string —
72+ the loader opens the file, unwraps a top-level ``"metrics"`` key if present
73+ (as used by evaluation result files), and filters out internal underscore-prefixed
74+ fields (e.g., cached ``init=False`` attributes) before constructing the instance.
6275
6376 Args:
6477 file_path (Union[str, Path]): The path to the JSON file.
6578
6679 Returns:
67- ScorerMetrics: An instance of ScorerMetrics with the loaded data.
80+ ScorerMetrics: An instance of ScorerMetrics (or subclass) with the loaded data.
6881
6982 Raises:
7083 FileNotFoundError: If the specified file does not exist.
@@ -82,6 +95,29 @@ def from_json(cls: type[T], file_path: Union[str, Path]) -> T:
8295
8396 return cls (** filtered_data )
8497
98+ @classmethod
99+ def from_json (cls : type [T ], file_path : Union [str , Path ]) -> T :
100+ """
101+ Load a metrics instance from a JSON file (deprecated alias for :meth:`from_json_file`).
102+
103+ The name ``from_json`` is misleading because it accepts a *file path*, not a JSON
104+ string. Use :meth:`from_json_file` instead.
105+
106+ Args:
107+ file_path (Union[str, Path]): The path to the JSON file.
108+
109+ Returns:
110+ ScorerMetrics: An instance of ScorerMetrics (or subclass) with the loaded data.
111+ """
112+ from pyrit .common .deprecation import print_deprecation_message
113+
114+ print_deprecation_message (
115+ old_item = f"{ cls .__name__ } .from_json" ,
116+ new_item = f"{ cls .__name__ } .from_json_file" ,
117+ removed_in = "0.15.0" ,
118+ )
119+ return cls .from_json_file (file_path )
120+
85121
86122@dataclass
87123class HarmScorerMetrics (ScorerMetrics ):
0 commit comments