simplebench.validators.validate_sequence_of_type module🔗

Validator for sequence of specified type(s).

simplebench.validators.validate_sequence_of_type.validate_sequence_of_type(
value: Any,
types: type[T],
field_name: str,
type_tag: ErrorTag,
value_tag: ErrorTag,
*,
allow_empty: bool = True,
exact_type: bool = False,
) list[T][source]🔗
simplebench.validators.validate_sequence_of_type.validate_sequence_of_type(
value: Any,
types: tuple[type, ...],
field_name: str,
type_tag: ErrorTag,
value_tag: ErrorTag,
*,
allow_empty: bool = True,
exact_type: bool = False,
) list[Any]

Validate that a value is a sequence of specified type(s).

When a single type is provided, the return type is automatically inferred as list[T]. When multiple types are provided, the return type is list[Any], which allows the caller to narrow the type with an explicit annotation.

Parameters:
  • value (Sequence[Any]) – The sequence of values to validate.

  • types (type[T] | tuple[type, ...]) – A single type or tuple of allowed types.

  • field_name (str) – The name of the field being validated (for error messages).

  • type_tag (ErrorTag) – The error tag to use for type errors.

  • value_tag (ErrorTag) – The error tag to use for value errors.

  • allow_empty (bool) – If set to True, allow an empty sequence. Otherwise raise an error.

  • exact_type (bool) – If set to True, require that the types be exactly the types specified, not subclasses. Otherwise allow subclass matches.

Returns:

For single type, returns list[T]. For multiple types, returns list[Any] which can be narrowed with explicit type annotation.

Return type:

list[T] | list[Any]

Raises:

Examples

Single type (automatic inference):

names = validate_sequence_of_type(['Alice'], str, 'names', ...)
# Type: list[str]

Multiple types (manual narrowing):

mixed: list[str | int] = validate_sequence_of_type(
    ['a', 1], (str, int), 'items', ...
)
# Type: list[str | int]