# Buggy code
def validate_name(
    pattern,
    multi=None,
    anime_type=None,  # pylint: disable=too-many-arguments, too-many-return-statements
    file_only=False,
    abd=False,
    sports=False,
):
    """
    See if we understand a name

    :param pattern: Name to analyse
    :param multi: Is this a multi-episode name
    :param anime_type: Is this anime
    :param file_only: Is this just a file or a dir
    :param abd: Is air-by-date enabled
    :param sports: Is this sports
    :return: True if valid name, False if not
    """
    ep = generate_sample_ep(multi, abd, sports, anime_type)

    new_name = ep.formatted_filename(pattern, multi, anime_type) + ".ext"
    new_path = ep.formatted_dir(pattern, multi)
    if not file_only:
        new_name = os.path.join(new_path, new_name)

    if not new_name:
        logger.log("Unable to create a name out of " + pattern, logger.DEBUG)
        return False

    logger.log("Trying to parse " + new_name, logger.DEBUG)

    try:
        parse_result = NameParser(series=ep.series, naming_pattern=True).parse(new_name)
    except (InvalidNameException, InvalidShowException) as error:
        logger.log("{}".format(error), logger.DEBUG)
        return False

    logger.log(
        "The name " + new_name + " parsed into " + str(parse_result), logger.DEBUG
    )

    if abd or sports:
        if parse_result.air_date != ep.airdate:
            logger.log(
                "Air date incorrect in parsed episode, pattern isn't valid",
                logger.DEBUG,
            )
            return False
    elif anime_type != 3:
        if parse_result.ab_episode_numbers and parse_result.ab_episode_numbers != [
            x.absolute_number for x in [ep] + ep.related_episodes
        ]:
            logger.log(
                "Absolute numbering incorrect in parsed episode, pattern isn't valid",
                logger.DEBUG,
            )
            return False
    else:
        if parse_result.season_number != ep.season:
            logger.log(
                "Season number incorrect in parsed episode, pattern isn't valid",
                logger.DEBUG,
            )
            return False
        if parse_result.episode_numbers != [
            x.episode for x in [ep] + ep.related_episodes
        ]:
            logger.log(
                "Episode numbering incorrect in parsed episode, pattern isn't valid",
                logger.DEBUG,
            )
            return False

    return True


# Traceback (AttributeError)
2019-02-22 03:02:57 DEBUG    Thread_6 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_16 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.018264055252075195, season: None, quality: Unknown, total_time: 0.018332958221435547
2019-02-22 03:02:57 DEBUG    Thread_6 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.027241230010986328, season: None, quality: Unknown, total_time: 0.02730083465576172
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.024758100509643555, season: None, quality: Unknown, total_time: 0.02481818199157715
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.04372978210449219, season: None, quality: Unknown, total_time: 0.04378795623779297
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.024518966674804688, season: None, quality: Unknown, total_time: 0.024566173553466797
2019-02-22 03:02:57 DEBUG    Thread_16 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 ERROR    Thread_6 :: [ee861d0] Exception generated: 'TVShow' object has no attribute 'identifier'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 262, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/config/post_processing.py", line 130, in testNaming
    result = naming.test_name(pattern, multi, abd, sports, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 298, in test_name
    return {'name': ep.formatted_filename(pattern, multi, anime_type), 'dir': ep.formatted_dir(pattern, multi)}
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1830, in formatted_filename
    return sanitize_filename(self._format_pattern(name_groups[-1], multi, anime_type))
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1682, in _format_pattern
    other_ep != self.related_episodes[-1]:
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2066, in __eq__
    return all([self.series.identifier == other.series.identifier,
AttributeError: 'TVShow' object has no attribute 'identifier'
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_16 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] 1: Formatting pattern: %SN S%0SE03 -> Show Name S02E03
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.02576303482055664, absolute_episode: [], quality: Unknown, total_time: 0.02585601806640625
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.036277055740356445, absolute_episode: [], quality: Unknown, total_time: 0.03634905815124512
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.030246973037719727, absolute_episode: [], quality: Unknown, total_time: 0.030331134796142578
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.02576303482055664, absolute_episode: [], quality: Unknown, total_time: 0.02585601806640625
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.036277055740356445, absolute_episode: [], quality: Unknown, total_time: 0.03634905815124512
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.016936779022216797, season: None, quality: Unknown, total_time: 0.01699090003967285
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.030246973037719727, absolute_episode: [], quality: Unknown, total_time: 0.030331134796142578
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Checking whether the pattern Season %S/%SN S%0SE%0E is valid for a multi episode
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.013588905334472656, season: None, quality: Unknown, total_time: 0.013649940490722656
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Matched release Show.Name.003-004.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.027174949645996094, season: None, quality: Unknown, total_time: 0.027235031127929688
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Trying to parse Season 2/Show Name S02E03.ext
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Parsed Show.Name.003-004.HDTV.x264-RLSGROUP into title: Show Name, absolute_episode: [3, 4], episode: [], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.016008853912353516, season: None, quality: Unknown, total_time: 0.016068220138549805
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Season 2/Show Name S02E03.ext to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Season 2/Show Name S02E03.ext into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.015719890594482422, absolute_episode: [], quality: Unknown, total_time: 0.0158078670501709
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] 1: Formatting pattern: %SN S%0SE03 -> Show Name S02E03
2019-02-22 03:02:57 DEBUG    Thread_1 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] 1: Formatting pattern: %SN S%0SE03 -> Show Name S02E03
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] The name Season 2/Show Name S02E03.ext parsed into season: 2, title: Show Name, episode: [3], episode_title: ext, mimetype: application/vnd.novadigm.ext, type: episode, parsing_time: 0.015719890594482422, absolute_episode: [], quality: Unknown, total_time: 0.0158078670501709
2019-02-22 03:02:57 ERROR    Thread_1 :: [ee861d0] Exception generated: 'TVShow' object has no attribute 'identifier'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 262, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/config/post_processing.py", line 160, in isNamingValid
    is_valid = naming.check_valid_naming(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 151, in check_valid_naming
    valid = valid and validate_name(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 201, in validate_name
    new_name = ep.formatted_filename(pattern, multi, anime_type) + '.ext'
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1830, in formatted_filename
    return sanitize_filename(self._format_pattern(name_groups[-1], multi, anime_type))
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1682, in _format_pattern
    other_ep != self.related_episodes[-1]:
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2066, in __eq__
    return all([self.series.identifier == other.series.identifier,
AttributeError: 'TVShow' object has no attribute 'identifier'
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Checking whether the pattern Season %S/%SN S%0SE%0E is valid for a multi episode
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Matched release Show.Name.003.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.014174938201904297, season: None, quality: Unknown, total_time: 0.014236927032470703
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Matched release Show.Name.003-004.HDTV.x264-RLSGROUP to a series in your database: Show Name
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Parsed Show.Name.003.HDTV.x264-RLSGROUP into title: Show Name, episode: [], absolute_episode: [3], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.013724088668823242, season: None, quality: Unknown, total_time: 0.013782978057861328
2019-02-22 03:02:57 DEBUG    Thread_39 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:57 DEBUG    Thread_3 :: [ee861d0] Parsed Show.Name.003-004.HDTV.x264-RLSGROUP into title: Show Name, absolute_episode: [3, 4], episode: [], source: HDTV, video_codec: H.264, video_encoder: x264, release_group: RLSGROUP, type: episode, parsing_time: 0.015986919403076172, season: None, quality: Unknown, total_time: 0.016047000885009766
2019-02-22 03:02:57 DEBUG    Thread_0 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:58 DEBUG    Thread_39 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:58 DEBUG    Thread_3 :: [ee861d0] Found codec for Show Name Ep Name
2019-02-22 03:02:58 DEBUG    Thread_0 :: [ee861d0] 1: Formatting pattern: Season %S -> Season 2
2019-02-22 03:02:58 DEBUG    Thread_39 :: [ee861d0] Trying to parse Show Name S02E03.ext
2019-02-22 03:02:58 DEBUG    Thread_3 :: [ee861d0] 1: Episode has no release group, replacing it with RLSGROUP
2019-02-22 03:02:58 DEBUG    Thread_0 :: [ee861d0] Trying to parse Show Name S02E03.ext
2019-02-22 03:02:58 ERROR    Thread_3 :: [ee861d0] Exception generated: 'TVShow' object has no attribute 'identifier'
Traceback (most recent call last):
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/core/base.py", line 262, in async_call
    result = function(**kwargs)
  File "/Users/ovidijus/Apps/Medusa/medusa/server/web/config/post_processing.py", line 160, in isNamingValid
    is_valid = naming.check_valid_naming(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 151, in check_valid_naming
    valid = valid and validate_name(pattern, multi, anime_type)
  File "/Users/ovidijus/Apps/Medusa/medusa/naming.py", line 201, in validate_name
    new_name = ep.formatted_filename(pattern, multi, anime_type) + '.ext'
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1830, in formatted_filename
    return sanitize_filename(self._format_pattern(name_groups[-1], multi, anime_type))
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 1682, in _format_pattern
    other_ep != self.related_episodes[-1]:
  File "/Users/ovidijus/Apps/Medusa/medusa/tv/episode.py", line 2066, in __eq__
    return all([self.series.identifier == other.series.identifier,
AttributeError: 'TVShow' object has no attribute 'identifier'

# Fixed code
def validate_name(
    pattern, multi=None, anime_type=None, file_only=False, abd=False, sports=False
):
    """
    See if we understand a name

    :param pattern: Name to analyse
    :param multi: Is this a multi-episode name
    :param anime_type: Is this anime
    :param file_only: Is this just a file or a dir
    :param abd: Is air-by-date enabled
    :param sports: Is this sports
    :return: True if valid name, False if not
    """
    ep = generate_sample_ep(multi, abd, sports, anime_type)

    new_name = ep.formatted_filename(pattern, multi, anime_type) + ".ext"
    new_path = ep.formatted_dir(pattern, multi)
    if not file_only:
        new_name = os.path.join(new_path, new_name)

    if not new_name:
        logger.log("Unable to create a name out of " + pattern, logger.DEBUG)
        return False

    logger.log("Trying to parse " + new_name, logger.DEBUG)

    try:
        parse_result = NameParser(series=ep.series, naming_pattern=True).parse(new_name)
    except (InvalidNameException, InvalidShowException) as error:
        logger.log("{}".format(error), logger.DEBUG)
        return False

    logger.log(
        "The name " + new_name + " parsed into " + str(parse_result), logger.DEBUG
    )

    if abd or sports:
        if parse_result.air_date != ep.airdate:
            logger.log(
                "Air date incorrect in parsed episode, pattern isn't valid",
                logger.DEBUG,
            )
            return False
    elif anime_type != 3:
        if parse_result.ab_episode_numbers and parse_result.ab_episode_numbers != [
            x.absolute_number for x in [ep] + ep.related_episodes
        ]:
            logger.log(
                "Absolute numbering incorrect in parsed episode, pattern isn't valid",
                logger.DEBUG,
            )
            return False
    else:
        if parse_result.season_number != ep.season:
            logger.log(
                "Season number incorrect in parsed episode, pattern isn't valid",
                logger.DEBUG,
            )
            return False
        if parse_result.episode_numbers != [
            x.episode for x in [ep] + ep.related_episodes
        ]:
            logger.log(
                "Episode numbering incorrect in parsed episode, pattern isn't valid",
                logger.DEBUG,
            )
            return False

    return True