Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add runtime type validation for Event class #6

Open
sidmitra opened this issue Apr 7, 2022 · 0 comments
Open

Add runtime type validation for Event class #6

sidmitra opened this issue Apr 7, 2022 · 0 comments

Comments

@sidmitra
Copy link
Contributor

sidmitra commented Apr 7, 2022

  • 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.

@sidmitra sidmitra changed the title Evaluate moving to pydantic model instead of dataclass for Event Add runtime type validation for Event class Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant