bpod_core.misc.ValidatedDict

class bpod_core.misc.ValidatedDict

Bases: RootModel[dict[TypeVar, TypeVar]], MutableMapping[K, V], Generic[K, V]

A dict-like container with runtime validation for keys and values.

This class wraps a standard dict and integrates with Pydantic’s RootModel to validate keys and values upon mutation. It behaves like a mutable mapping for all common operations (get, set, delete, iterate, len) and compares equal to regular dicts with the same contents.

Notes

Subclass ValidatedDict to create a custom type with validation:

>>> class TestDict(ValidatedDict[str, int]):
...     pass

You can then instantiate your class TestDict like a regular dict:

>>> test_dict = TestDict()
>>> test_dict['foo'] = 1
>>> test_dict[42] = 2
Traceback (most recent call last):
   ...
pydantic_core._pydantic_core.ValidationError: 1 validation error for TestDict
42.[key]
  Input should be a valid string [type=string_type, input_value=42, input_type=int]
    For further information visit https://errors.pydantic.dev/2.11/v/string_type

Alternatively, you can also instantiate a ValidatedDict directly:

>>> my_validated_dict = ValidatedDict[str, int]({'foo': 1, 'bar': 2})
model_config: ClassVar[ConfigDict] = {}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

root: dict[TypeVar(K), TypeVar(V)]