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

    :param matches:
    :type matches: rebulk.match.Matches
    :param context:
    :type context: dict
    :return:
    """
    to_remove = []
    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:
            to_remove.extend(formats[0:-1])

    return to_remove


# Traceback (AttributeError)
FINDSUBTITLES :: [42fd472] Traceback (most recent call last):
File "/home/osmc/SickRage/sickbeard/subtitles.py", line 326, in download_best_subs
for s in subtitles_list], key=operator.itemgetter(1), reverse=True)
File "/home/osmc/SickRage/lib/subliminal/score.py", line 88, in compute_score
matches = subtitle.get_matches(video)
File "/home/osmc/SickRage/lib/subliminal/providers/legendastv.py", line 147, in get_matches
matches |= guess_matches(video, guessit(self.archive.name, {'type': self.type}))
File "/home/osmc/SickRage/lib/subliminal/subtitle.py", line 233, in guess_matches
if video.format and 'format' in guess and guess['format'].lower() == video.format.lower():
AttributeError: 'list' object has no attribute 'lower'
Traceback (most recent call last):
File "/home/osmc/SickRage/sickbeard/subtitles.py", line 326, in download_best_subs
for s in subtitles_list], key=operator.itemgetter(1), reverse=True)
File "/home/osmc/SickRage/lib/subliminal/score.py", line 88, in compute_score
matches = subtitle.get_matches(video)
File "/home/osmc/SickRage/lib/subliminal/providers/legendastv.py", line 147, in get_matches
matches |= guess_matches(video, guessit(self.archive.name, {'type': self.type}))
File "/home/osmc/SickRage/lib/subliminal/subtitle.py", line 233, in guess_matches
if video.format and 'format' in guess and guess['format'].lower() == video.format.lower():
AttributeError: 'list' object has no attribute 'lower'

# 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 ("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