Skip to content

Commit

Permalink
Merge 6bc3ce6 into 4eba702
Browse files Browse the repository at this point in the history
  • Loading branch information
federicotdn committed Nov 26, 2019
2 parents 4eba702 + 6bc3ce6 commit 6e39852
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 27 deletions.
47 changes: 29 additions & 18 deletions rasa/core/events/__init__.py
Expand Up @@ -7,6 +7,7 @@
import logging
import uuid
from dateutil import parser
from datetime import datetime
from typing import List, Dict, Text, Any, Type, Optional

from rasa.core import utils
Expand Down Expand Up @@ -91,14 +92,14 @@ def __init__(

@property
def metadata(self) -> Dict[Text, Any]:
# Needed for compatibility for Rasa versions <1.4.0.
# Previous versions of Rasa serialized trackers using the pickle
# module. For the moment, Rasa still supports loading these serialized
# trackers with pickle, but will use JSON in any subsequent save
# operations. Versions of trackers serialized with pickle won't
# include the `_metadata` attribute in their events, so it is necessary
# to define this getter in case the attribute does not exist.
# For more information see CHANGELOG.rst.
# Needed for compatibility with Rasa versions <1.4.0. Previous versions
# of Rasa serialized trackers using the pickle module. For the moment,
# Rasa still supports loading these serialized trackers with pickle,
# but will use JSON in any subsequent save operations. Versions of
# trackers serialized with pickle won't include the `_metadata`
# attribute in their events, so it is necessary to define this getter
# in case the attribute does not exist. For more information see
# CHANGELOG.rst.
return getattr(self, "_metadata", {})

def __ne__(self, other: Any) -> bool:
Expand Down Expand Up @@ -131,7 +132,7 @@ def from_parameters(
if event_name is None:
return None

event_class = Event.resolve_by_type(event_name, default)
event_class: Type[Event] = Event.resolve_by_type(event_name, default)
if not event_class:
return None

Expand All @@ -142,7 +143,7 @@ def _from_story_string(cls, parameters: Dict[Text, Any]) -> Optional[List["Event
"""Called to convert a parsed story line into an event."""
return [cls(parameters.get("timestamp"), parameters.get("metadata"))]

def as_dict(self):
def as_dict(self) -> Dict[Text, Any]:
d = {
"event": self.type_name,
"timestamp": self.timestamp,
Expand Down Expand Up @@ -590,12 +591,12 @@ class ReminderScheduled(Event):

def __init__(
self,
action_name,
trigger_date_time,
name=None,
kill_on_user_message=True,
timestamp=None,
metadata=None,
action_name: Text,
trigger_date_time: datetime,
name: Optional[Text] = None,
kill_on_user_message: bool = True,
timestamp: Optional[int] = None,
metadata: Optional[Dict[Text, Any]] = None,
):
"""Creates the reminder
Expand Down Expand Up @@ -679,7 +680,12 @@ class ReminderCancelled(Event):

type_name = "cancel_reminder"

def __init__(self, action_name, timestamp=None, metadata=None):
def __init__(
self,
action_name: Text,
timestamp: Optional[int] = None,
metadata: Optional[Dict[Text, Any]] = None,
):
"""
Args:
action_name: name of the scheduled action to be cancelled
Expand Down Expand Up @@ -748,7 +754,12 @@ class StoryExported(Event):

type_name = "export"

def __init__(self, path=None, timestamp=None, metadata=None):
def __init__(
self,
path: Optional[Text] = None,
timestamp: Optional[int] = None,
metadata: Optional[Dict[Text, Any]] = None,
):
self.path = path
super().__init__(timestamp, metadata)

Expand Down
19 changes: 10 additions & 9 deletions tests/core/test_events.py
@@ -1,4 +1,5 @@
import time
from typing import Type

import pytz
from datetime import datetime
Expand Down Expand Up @@ -270,11 +271,12 @@ def test_correct_timestamp_setting(event_class):


@pytest.mark.parametrize("event_class", utils.all_subclasses(Event))
def test_event_metadata_dict(event_class):
def test_event_metadata_dict(event_class: Type[Event]):
metadata = {"foo": "bar", "quux": 42}

# Create the event from a dict that will be accepted by the _from_parameters
# method of any Event subclass (the values themselves are not important).
# Create the event from a `dict` that will be accepted by the
# `_from_parameters` method of any `Event` subclass (the values themselves
# are not important).
event = Event.from_parameters(
{
"metadata": metadata,
Expand All @@ -287,13 +289,12 @@ def test_event_metadata_dict(event_class):


@pytest.mark.parametrize("event_class", utils.all_subclasses(Event))
def test_event_default_metadata(event_class):
def test_event_default_metadata(event_class: Type[Event]):

# Create an event without metadata.
# When converting the Event to a dict, it should not include a `metadata`
# property - unless it's a UserUttered or a BotUttered event (or subclasses
# of them), in which case the metadata should be included with a default
# value of {}.
# Create an event without metadata. When converting the `Event` to a
# `dict`, it should not include a `metadata` property - unless it's a
# `UserUttered` or a `BotUttered` event (or subclasses of them), in which
# case the metadata should be included with a default value of {}.
event = Event.from_parameters(
{
"event": event_class.type_name,
Expand Down

0 comments on commit 6e39852

Please sign in to comment.