ethereum / lahja

Lahja is a generic multi process event bus implementation written in Python 3.6+ to enable lightweight inter-process communication, based on non-blocking asyncio
MIT License
394 stars 19 forks source link

Metaclass API for defining events #63

Open pipermerriam opened 5 years ago

pipermerriam commented 5 years ago

What is wrong

Defining new events requires un-necessary boilerplate. Events are typically purely for data transfer and rarely have extra methods or properties.

How can it be fixed?

It'd be really nice to be able to do this:

class NewUser(DataEvent):
    username: str
    created_at: datetime.datetime
    age: int

event = NewUser('piper', datatime.datetime.now(), 99)

Doing this with metaclasses is quite easy and it would reduce the boilerplate in defining new events significantly.

cburgdorf commented 5 years ago

We could also just force it on the user to add a @dataclass decorator on the events. I opened an PR to demonstrate this with the examples: https://github.com/ethereum/lahja/pull/139

pipermerriam commented 5 years ago

dataclasses could be a nice way to do this but I'm not inclined to force it.

cburgdorf commented 5 years ago

but I'm not inclined to force it.

It would be totally up to the consumer to decide to use them or not. Here's a PR to show case that for Trinity https://github.com/ethereum/trinity/pull/726 I only converted a few places there but I happily convert all of them if you are :+1: on that change.