# Buggy code
def when(self, matches, context):
    """Evaluate the rule.

    :param matches:
    :type matches: rebulk.match.Matches
    :param context:
    :type context: dict
    :return:
    """
    fileparts = matches.markers.named("path")
    for filepart in marker_sorted(fileparts, matches):
        formats = matches.range(
            filepart.start, filepart.end, predicate=lambda match: match.name == "format"
        )
        if len(formats) < 2:
            continue

        last_format = formats[-1]
        previous = matches.previous(
            last_format, predicate=lambda match: match.name == "screen_size"
        )
        next_range = matches.range(
            last_format.end,
            filepart.end,
            lambda match: match.name in ("video_codec", "release_group"),
        )
        # If we have at least 3 matches near by, then discard the other formats
        if len(previous) + len(next_range) > 2:
            invalid_formats = {f.value for f in formats[0:-1]}
            to_remove = matches.named(
                "format", predicate=lambda m: m.value in invalid_formats
            )
            return to_remove


# Traceback (KeyError)
FINDSUBTITLES :: [8ae9bc7] Unable to find subtitles for Mr. Robot S02E02. Error: 'type'
Traceback (most recent call last):
File "/volume1/@appstore/sickbeard-custom/var/SickBeard/sickbeard/subtitles.py", line 870, in run
episode_object.download_subtitles()
File "/volume1/@appstore/sickbeard-custom/var/SickBeard/sickbeard/tv.py", line 1759, in download_subtitles
new_subtitles = subtitles.download_subtitles(self)
File "/volume1/@appstore/sickbeard-custom/var/SickBeard/sickbeard/subtitles.py", line 327, in download_subtitles
logger.error(traceback.format_exc())
File "/usr/local/python/lib/python2.7/logging/__init__.py", line 1456, in error
self.logger.error(msg, *args, **kwargs)
File "/usr/local/python/lib/python2.7/logging/__init__.py", line 1185, in error
self._log(ERROR, msg, args, **kwargs)
File "/usr/local/python/lib/python2.7/logging/__init__.py", line 1278, in _log
self.handle(record)
File "/usr/local/python/lib/python2.7/logging/__init__.py", line 1288, in handle
self.callHandlers(record)
File "/usr/local/python/lib/python2.7/logging/__init__.py", line 1328, in callHandlers
hdlr.handle(record)
File "/usr/local/python/lib/python2.7/logging/__init__.py", line 751, in handle
self.emit(record)
File "/volume1/@appstore/sickbeard-custom/var/SickBeard/sickbeard/logger.py", line 135, in emit
message = self.format(record)
File "/usr/local/python/lib/python2.7/logging/__init__.py", line 726, in format
return fmt.format(record)
File "/volume1/@appstore/sickbeard-custom/var/SickBeard/sickbeard/logger.py", line 169, in format
msg = super(CensoredFormatter, self).format(record)
File "/usr/local/python/lib/python2.7/logging/__init__.py", line 465, in format
record.message = record.getMessage()
File "/usr/local/python/lib/python2.7/logging/__init__.py", line 325, in getMessage
msg = str(self.msg)
File "/volume1/@appstore/sickbeard-custom/var/SickBeard/sickbeard/logger.py", line 560, in __str__
return str(self.fmt).format(*self.args, **self.kwargs)
KeyError: "'type'"

# Fixed code
def when(self, matches, context):
    """Evaluate the rule.

    :param matches:
    :type matches: rebulk.match.Matches
    :param context:
    :type context: dict
    :return:
    """
    fileparts = matches.markers.named("path")
    for filepart in marker_sorted(fileparts, matches):
        formats = matches.range(
            filepart.start, filepart.end, predicate=lambda match: match.name == "format"
        )
        if len(formats) < 2:
            continue

        last_format = formats[-1]
        previous = matches.previous(
            last_format, predicate=lambda match: match.name == "screen_size"
        )
        next_range = matches.range(
            last_format.end,
            filepart.end,
            lambda match: match.name in ("audio_codec", "video_codec", "release_group"),
        )
        # If we have at least 3 matches near by, then discard the other formats
        if len(previous) + len(next_range) > 2:
            invalid_formats = {f.value for f in formats[0:-1]}
            to_remove = matches.named(
                "format", predicate=lambda m: m.value in invalid_formats
            )
            return to_remove