Skip to content

Add runtime type validation for Event class #6

@sidmitra

Description

  • Get better runtime type validation
    • Evaluate moving to pydantic model instead of dataclass for Event
  • What about avro, or protobuf?
In [9]: import datetime

In [10]: from dataclasses import dataclass, asdict

In [11]: @dataclass
   ...: class Event:
   ...:     data: str

In [12]: bus.register_event("airbase.event_store.event", Event)

In [13]: event = Event(data=datetime.datetime.now())

In [14]: bus.send(event)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

One side-effect of using dataclass in the eventbus is dataclass values aren't type validated during creation. As result the failure happens at the lowest layer while converting to JSON. This becomes painful to debug when there one of the field is nested dictionary and can't be converted to JSON by json.dumps.
If we use pydantic the type check will kick in during object creation and can prevent the error happening at lower level.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions