Utilities

class stereotype.Role(name: str, empty_by_default: bool = False)[source]

Declares a serialization role used to exclude some fields. Role objects should usually be global variables. Used in stereotype.Model class method stereotype.Model.declare_roles() to either blacklist or whitelist that Model’s fields.

Parameters:
  • name (str) – A string representation of this role. Useful for custom field serialization.

  • empty_by_default (bool) – If true, this role excludes all fields by default.

blacklist(*fields, override_parents: bool = False)[source]

This role should omit the specified fields of the stereotype.Model.

Parameters:
  • fields – The field descriptors (cls.field_name) to omit for this role.

  • override_parents (bool) – If true, even fields inherited from superclasses are included unless specified.

whitelist(*fields, override_parents: bool = False)[source]

This role should include only the specified fields of the stereotype.Model

Parameters:
  • fields – The field descriptors (cls.field_name) to include for this role.

  • override_parents (bool) – If true, even fields inherited from superclasses are hidden unless specified.

class stereotype.DataError(errors: List[Tuple[Tuple[str, ...], str]])[source]

Bases: StereotypeError

Encapsulates one or multiple errors that happened during conversion or validation, mapped by field paths.

property errors: Dict[str, List[str] | dict]

Generates a potentially deeply nested dictionary with errors and their paths.

Keys in the dictionaries are field (primitive) names. Values are either lists of error messages from simple fields, or recursive dictionaries for compound fields.

class stereotype.ConversionError(errors: List[Tuple[Tuple[str, ...], str]])[source]

Bases: DataError, TypeError

Single error created when converting raw data to a Model, caused mostly by failed type coercions.

class stereotype.ValidationError(errors: List[Tuple[Tuple[str, ...], str]])[source]

Bases: DataError, ValueError

Potentially multiple errors created when validating already converted models.

stereotype.utils.Missing = Missing

Placeholder value for required fields with missing value (i.e., the field doesn’t specify a default, no relation to Optional). Validation will always fail if this value is present, i.e. it’s never present in valid models.

In the unlikely event of needing to check for this value in code, use model.field is Missing.