Skip to content

Commit

Permalink
upd
Browse files Browse the repository at this point in the history
  • Loading branch information
m5l14i11 committed Sep 10, 2024
1 parent 859f7a5 commit 2664e5d
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions core/actors/_base_actor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Union, get_args, get_origin

from core.commands.base import Command
from core.events.base import Event
from core.interfaces.abstract_actor import AbstractActor, Ask, Message
from core.queries.base import Query
from infrastructure.event_dispatcher.event_dispatcher import EventDispatcher
Expand Down Expand Up @@ -70,19 +71,27 @@ def _unregister_events(self):
self._mailbox.unregister(event, self.on_receive)

def _discover_events(self):
allowed_types = (Event, Query, Command)
sig = inspect.signature(self.on_receive)
params = list(sig.parameters.values())
params = sig.parameters.values()

if len(params) < 1:
if not params:
return []

event_type = params[0].annotation
event_type = next(iter(params)).annotation

events = []
events = (
get_args(event_type) if get_origin(event_type) is Union else [event_type]
)

if get_origin(event_type) is Union:
events = get_args(event_type)
else:
events = [event_type]
invalid_events = [
event for event in events if not issubclass(event, allowed_types)
]

if invalid_events:
raise RuntimeError(
f"Disallowed events: {', '.join(e.__name__ for e in invalid_events)}. "
f"Must be subclasses of {allowed_types}."
)

return list(set(events))

0 comments on commit 2664e5d

Please sign in to comment.