types
Types and serialization helper functions
| TYPE ALIAS | DESCRIPTION |
|---|---|
SerializableValueType |
Allowed parameterization types for the |
| CLASS | DESCRIPTION |
|---|---|
UnserializableValueTypeError |
Exception raised when parametrization invariants of |
Attributes
ORJSON_LEAVES
module-attribute
ORJSON_LEAVES: set[type] = {
str,
int,
float,
bool,
datetime,
date,
time,
UUID,
Fragment,
type(None),
}
Atomic serializable types for runtime typechecking.
Type Aliases
SerializableValueType
SerializableValueType = (
OrjsonLeaf
| tuple["SerializableValueType", ...]
| list["SerializableValueType"]
| dict[str, "SerializableValueType"]
)
Allowed parameterization types for the T in BaseMetric[T].
The SerializableValueType type contains all types that can be used as the value
type T in subclasses' implementations of the abstract
BaseMetric.evaluate(text: str) -> dict[str, T] method.
Supported types include
strintfloatbooldt.datetimedt.datedt.timeUUIDorjson.Fragment
optionally wrapped in tuple[...], list[...] or dict[str, ...].
Warning
While tuple is natively serialized, subclasses of tuple (e.g. namedtuple) are not.
See also
Classes
UnserializableValueTypeError
flowchart TD
evallm.metrics.types.UnserializableValueTypeError[UnserializableValueTypeError]
click evallm.metrics.types.UnserializableValueTypeError href "" "evallm.metrics.types.UnserializableValueTypeError"
Exception raised when parametrization invariants of BaseMetric[T] are violated.
Functions
check_evaluate_signature
Helper function to ensure that a metric class has an evaluate method with an
annotated signature that conforms to BaseMetric's invariants.
If metric_cls.evaluate(text: str) doesn't have a return annotation, it will be
added using the class'
metric_cls.value_type.
Expected signature
Every metric's evaluate(text: str) -> dict[str, T] method returns a
dictionary whose values must conform to the metric's declared T. For leaf
metrics, the expected convention is to return a single key-value pair using
the metric instance's
result_key (which defaults to
the metric's name). The generic type T is parametrized by each concrete
metric class such that T is bound to any JSON-serializable type (see
SerializableValueType).
| PARAMETER | DESCRIPTION |
|---|---|
metric_cls
|
The metric class to check.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
metric_cls
|
The same metric class (potentially with added return
annotation for
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
If |
TypeError
|
If...
- |
Source code in src/evallm/metrics/types.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 | |
is_serializable_type
Helper function to recursively verify a given type annotation.
| PARAMETER | DESCRIPTION |
|---|---|
tp
|
The object to check for JSON-serializability.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
TYPE:
|
Source code in src/evallm/metrics/types.py
is_serializable_value
Helper function to recursively verify a given runtime value.
| PARAMETER | DESCRIPTION |
|---|---|
v
|
The value to check for JSON-serializability.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
is_serializable
|
TYPE:
|
Source code in src/evallm/metrics/types.py
matches_annotation
Helper function to check whether a given runtime value matches a given type annotation.
| PARAMETER | DESCRIPTION |
|---|---|
val
|
The runtime value to check.
TYPE:
|
ann
|
The type annotation to check against.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
matches
|
TYPE:
|