Skip to content

EventHandler should normalize entities into iterable type #829

@huweiATgithub

Description

@huweiATgithub

The problem

class EventHandler(BaseEventHandler):
def __init__(
self,
*,
matcher: Callable[[Event], bool],
entities: Optional[SomeEntitiesType] = None,
handle_once: bool = False
) -> None:
"""
Create an EventHandler.
:param: matcher is a callable that takes an event and returns True if
the event should be handled by this event handler, False otherwise.
:param: entities is an LaunchDescriptionEntity or list of them, and is
returned by handle() unconditionally if matcher returns True.
:param: handle_once is a flag that, if True, unregisters this EventHandler
after being handled once.
"""
super().__init__(matcher=matcher, handle_once=handle_once)
self.__entities = entities
@property
def entities(self):
"""Getter for entities."""
return self.__entities
def describe(self) -> Tuple[Text, List[SomeEntitiesType]]:
"""Return the description list with 0 as a string, and then LaunchDescriptionEntity's."""
text, actions = super().describe()
if self.entities:
actions.extend(self.entities)
return (text, actions)

In above code, entities: Optional[SomeEntitiesType] = None, entities is marked as SomeEntitiesType. It is not normalized in the __init__ function. In the describe function below, it is used as an iterable actions.extend(self.entities).

Besides, noting that in ExecuteLocal, entities is set to a single LaunchDescriptionEntity many times:

            EventHandler(
                matcher=lambda event: is_a_subclass(event, SignalProcess),
                entities=OpaqueFunction(function=self.__on_signal_process_event),
            )

Proposed fix

I plan to fix this by always making the __entities member an iterable (if not None):

        if isinstance(entities, LaunchDescriptionEntity):
            self.__entities = (entities,)
        else:
            self.__entities = entities

Metadata

Metadata

Assignees

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