# Buggy code
def fromVEvents(cls, events_list, ref=None, **kwargs):
    """
    :type events: list
    """
    assert isinstance(events_list, list)

    vevents = dict()
    if len(events_list) == 1:
        vevents["PROTO"] = events_list[0]  # TODO set mutable = False
    else:
        for event in events_list:
            if "RECURRENCE-ID" in event:
                if invalid_timezone(event["RECURRENCE-ID"]):
                    default_timezone = kwargs["locale"]["default_timezone"]
                    recur_id = default_timezone.localize(event["RECURRENCE-ID"].dt)
                    ident = str(to_unix_time(recur_id))
                else:
                    ident = str(to_unix_time(event["RECURRENCE-ID"].dt))
                vevents[ident] = event
            else:
                vevents["PROTO"] = event
    if ref is None:
        ref = "PROTO"

    try:
        if type(vevents[ref]["DTSTART"].dt) != type(vevents[ref]["DTEND"].dt):  # flake8: noqa
            raise ValueError(
                "DTSTART and DTEND should be of the same type (datetime or date)"
            )
    except KeyError:
        pass

    if kwargs.get("start"):
        instcls = cls._get_type_from_date(kwargs.get("start"))
    else:
        instcls = cls._get_type_from_vDDD(vevents[ref]["DTSTART"])
    return instcls(vevents, ref=ref, **kwargs)


# Traceback (KeyError)
$ khal
critical: Cannot understand event 2233f40b-9d80-49ff-aac0-01c2f957fc9f.ics from calendar shiftgig,
critical:  you might want tofile an issue at https://github.com/pimutils/khal/issues
Traceback (most recent call last):
File "/usr/bin/khal", line 11, in <module>
load_entry_point('khal==0.9.4.dev26+ng93e6861', 'console_scripts', 'khal')()
File "/usr/lib/python3.6/site-packages/click/core.py", line 722, in __call__
return self.main(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/click/core.py", line 697, in main
rv = self.invoke(ctx)
File "/usr/lib/python3.6/site-packages/click/core.py", line 1043, in invoke
return Command.invoke(self, ctx)
File "/usr/lib/python3.6/site-packages/click/core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "/usr/lib/python3.6/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "/usr/lib/python3.6/site-packages/khal/cli.py", line 250, in cli
ctx.invoke(cli.commands[command])
File "/usr/lib/python3.6/site-packages/click/core.py", line 535, in invoke
return callback(*args, **kwargs)
File "/usr/lib/python3.6/site-packages/click/decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "/usr/lib/python3.6/site-packages/khal/cli.py", line 289, in calendar
env={"calendars": ctx.obj['conf']['calendars']}
File "/usr/lib/python3.6/site-packages/khal/controllers.py", line 105, in calendar
env=env,
File "/usr/lib/python3.6/site-packages/khal/controllers.py", line 264, in khal_list
width=width,
File "/usr/lib/python3.6/site-packages/khal/controllers.py", line 182, in get_events_between
events = sorted(collection.get_localized(start_local, end_local))
File "/usr/lib/python3.6/site-packages/khal/khalendar/khalendar.py", line 148, in <genexpr>
return (self._cover_event(event) for event in events)
File "/usr/lib/python3.6/site-packages/khal/khalendar/backend.py", line 485, in get_localized
yield self.construct_event(item, href, start, end, ref, etag, calendar, dtype)
File "/usr/lib/python3.6/site-packages/khal/khalendar/backend.py", line 564, in construct_event
ref=ref,
File "/usr/lib/python3.6/site-packages/khal/khalendar/event.py", line 155, in fromString
return cls.fromVEvents(events, ref, **kwargs)
File "/usr/lib/python3.6/site-packages/khal/khalendar/event.py", line 149, in fromVEvents
return instcls(vevents, ref=ref, **kwargs)
File "/usr/lib/python3.6/site-packages/khal/khalendar/event.py", line 634, in __init__
starttz = getattr(self._vevents[self.ref]['DTSTART'].dt, 'tzinfo', None)
KeyError: '1490803200'

# Fixed code
def fromVEvents(cls, events_list, ref=None, **kwargs):
    """
    :type events: list
    """
    assert isinstance(events_list, list)

    vevents = dict()
    for event in events_list:
        if "RECURRENCE-ID" in event:
            if invalid_timezone(event["RECURRENCE-ID"]):
                default_timezone = kwargs["locale"]["default_timezone"]
                recur_id = default_timezone.localize(event["RECURRENCE-ID"].dt)
                ident = str(to_unix_time(recur_id))
            else:
                ident = str(to_unix_time(event["RECURRENCE-ID"].dt))
            vevents[ident] = event
        else:
            vevents["PROTO"] = event

    if ref is None:
        ref = "PROTO"

    try:
        if type(vevents[ref]["DTSTART"].dt) != type(vevents[ref]["DTEND"].dt):  # flake8: noqa
            raise ValueError(
                "DTSTART and DTEND should be of the same type (datetime or date)"
            )
    except KeyError:
        pass

    if kwargs.get("start"):
        instcls = cls._get_type_from_date(kwargs.get("start"))
    else:
        instcls = cls._get_type_from_vDDD(vevents[ref]["DTSTART"])
    return instcls(vevents, ref=ref, **kwargs)