Model descriptions.

The model describes the components of a data structure, kind of like a struct in C, but there’s no limitation of what type of data structure the model is, or what it’s used for.

A record (faust.models.record) is a model type that serialize into dictionaries, so the model describe the fields, and their types:

>>> class Point(Record):
...    x: int
...    y: int

>>> p = Point(10, 3)
>>> assert p.x == 10
>>> assert p.y == 3
>>> p
<Point: x=10, y=3>
>>> payload = p.dumps(serializer='json')
'{"x": 10, "y": 3, "__faust": {"ns": "__main__.Point"}}'
>>> p2 = Record.loads(payload)
>>> p2
<Point: x=10, y=3>

Models are mainly used for describing the data in messages: both keys and values can be described as models.

faust.models.base.registry = {'@ClientAssignment': <class 'faust.assignor.client_assignment.ClientAssignment'>, '@ClientMetadata': <class 'faust.assignor.client_assignment.ClientMetadata'>, '@ClusterAssignment': <class 'faust.assignor.cluster_assignment.ClusterAssignment'>, '@ReqRepRequest': <class 'faust.agents.models.ReqRepRequest'>, '@ReqRepResponse': <class 'faust.agents.models.ReqRepResponse'>, 'faust.agents.models.ModelReqRepRequest': <class 'faust.agents.models.ModelReqRepRequest'>, 'faust.agents.models.ModelReqRepResponse': <class 'faust.agents.models.ModelReqRepResponse'>}

Global map of namespace -> Model, used to find model classes by name. Every single model defined is added here automatically when a model class is defined.

class faust.models.base.Model(*args, **kwargs) → None[source]

Meta description model for serialization.

classmethod loads(s: bytes, *, default_serializer: Union[faust.types.codecs.CodecT, str, None] = None, serializer: Union[faust.types.codecs.CodecT, str, None] = None) → faust.types.models.ModelT[source]

Deserialize model object from bytes.

Keyword Arguments:
 serializer (CodecArg) – Default serializer to use if no custom serializer was set for this model subclass.
Return type:ModelT
to_representation() → Any[source]

Convert object to JSON serializable object.

Return type:Any
derive(*objects, **fields) → faust.types.models.ModelT[source]
Return type:ModelT
dumps(*, serializer: Union[faust.types.codecs.CodecT, str, None] = None) → bytes[source]

Serialize object to the target serialization format.

Return type:bytes
class faust.models.base.FieldDescriptor(field: str, type: Type, model: Type[faust.types.models.ModelT], required: bool = True, default: Any = None, parent: faust.types.models.FieldDescriptorT = None) → None[source]

Describes a field.

Used for every field in Record so that they can be used in join’s /group_by etc.


>>> class Withdrawal(Record):
...    account_id: str
...    amount: float = 0.0
>>> Withdrawal.account_id
<FieldDescriptor: Withdrawal.account_id: str>
>>> Withdrawal.amount
<FieldDescriptor: Withdrawal.amount: float = 0.0>
  • field (str) – Name of field.
  • type (Type) – Field value type.
  • model (Type) – Model class the field belongs to.
  • required (bool) – Set to false if field is optional.
  • default (Any) – Default value when required=False.
field = None

Name of attribute on Model.

type = None

Type of value (e.g. int, or Optional[int])).

model = None

The model class this field is associated with.

required = True

Set if a value for this field is required (cannot be None).

default = None

Default value for non-required field.

getattr(obj: faust.types.models.ModelT) → Any[source]
Return type:Any
Return type:str