# Buggy code
def commands(self):
    cmd = ui.Subcommand("lyrics", help="fetch song lyrics")
    cmd.parser.add_option(
        "-p",
        "--print",
        dest="printlyr",
        action="store_true",
        default=False,
        help="print lyrics to console",
    )
    cmd.parser.add_option(
        "-r",
        "--write-rest",
        dest="writerest",
        action="store",
        default=None,
        metavar="dir",
        help="write lyrics to given directory as ReST files",
    )
    cmd.parser.add_option(
        "-f",
        "--force",
        dest="force_refetch",
        action="store_true",
        default=False,
        help="always re-download lyrics",
    )
    cmd.parser.add_option(
        "-l",
        "--local",
        dest="local_only",
        action="store_true",
        default=False,
        help="do not fetch missing lyrics",
    )

    def func(lib, opts, args):
        # The "write to files" option corresponds to the
        # import_write config value.
        write = ui.should_write()
        if opts.writerest:
            self.writerest_indexes(opts.writerest)
        for item in lib.items(ui.decargs(args)):
            if not opts.local_only and not self.config["local"]:
                self.fetch_item_lyrics(
                    lib,
                    item,
                    write,
                    opts.force_refetch or self.config["force"],
                )
            if item.lyrics:
                if opts.printlyr:
                    ui.print_(item.lyrics)
                if opts.writerest:
                    self.writerest(opts.writerest, item)
        if opts.writerest:
            # flush last artist
            self.writerest(opts.writerest, None)
            ui.print_("ReST files generated. to build, use one of:")
            ui.print_("  sphinx-build -b html %s _build/html" % opts.writerest)
            ui.print_("  sphinx-build -b epub %s _build/epub" % opts.writerest)
            ui.print_(
                (
                    "  sphinx-build -b latex %s _build/latex "
                    "&& make -C _build/latex all-pdf"
                )
                % opts.writerest
            )

    cmd.func = func
    return [cmd]


# Traceback (AttributeError)
$ beet -vv lyrics artie shaw carioca
user configuration: /home/doron/.config/beets/config.yaml
data directory: /home/doron/.config/beets
plugin paths:
Sending event: pluginload
inline: adding item field isMultiDisc
library database: /var/lib/beets/db
library directory: /var/lib/mpd/music
Sending event: library_opened
lyrics: failed to fetch: https://www.musixmatch.com/lyrics/Artie-Shaw-and-His-Orchestra/Carioca (404)
lyrics: failed to fetch: https://www.musixmatch.com/lyrics/Artie-Shaw/Carioca (404)
lyrics: failed to fetch: https://www.musixmatch.com/es/letras/Buddy-Rich-Artie-Shaw/The-Carioca (404)
lyrics: failed to fetch: https://www.musixmatch.com/lyrics/Artie-Shaw/Carioca (404)
lyrics: lyrics not found: FLAC % Artie Shaw - Begin the Beguine | CD-04 | 25 Carioca
Traceback (most recent call last):
File "/usr/bin/beet", line 11, in <module>
load_entry_point('beets==1.4.6', 'console_scripts', 'beet')()
File "/usr/lib/python3.6/site-packages/beets/ui/__init__.py", line 1256, in main
_raw_main(args)
File "/usr/lib/python3.6/site-packages/beets/ui/__init__.py", line 1243, in _raw_main
subcommand.func(lib, suboptions, subargs)
File "/usr/lib/python3.6/site-packages/beetsplug/lyrics.py", line 734, in func
self.writerest(opts.writerest, None)
File "/usr/lib/python3.6/site-packages/beetsplug/lyrics.py", line 762, in writerest
self.artist = item.artist.strip()
AttributeError: 'NoneType' object has no attribute 'artist'

# Fixed code
def commands(self):
    cmd = ui.Subcommand("lyrics", help="fetch song lyrics")
    cmd.parser.add_option(
        "-p",
        "--print",
        dest="printlyr",
        action="store_true",
        default=False,
        help="print lyrics to console",
    )
    cmd.parser.add_option(
        "-r",
        "--write-rest",
        dest="writerest",
        action="store",
        default=None,
        metavar="dir",
        help="write lyrics to given directory as ReST files",
    )
    cmd.parser.add_option(
        "-f",
        "--force",
        dest="force_refetch",
        action="store_true",
        default=False,
        help="always re-download lyrics",
    )
    cmd.parser.add_option(
        "-l",
        "--local",
        dest="local_only",
        action="store_true",
        default=False,
        help="do not fetch missing lyrics",
    )

    def func(lib, opts, args):
        # The "write to files" option corresponds to the
        # import_write config value.
        write = ui.should_write()
        if opts.writerest:
            self.writerest_indexes(opts.writerest)
        items = lib.items(ui.decargs(args))
        for item in items:
            if not opts.local_only and not self.config["local"]:
                self.fetch_item_lyrics(
                    lib,
                    item,
                    write,
                    opts.force_refetch or self.config["force"],
                )
            if item.lyrics:
                if opts.printlyr:
                    ui.print_(item.lyrics)
                if opts.writerest:
                    self.appendrest(opts.writerest, item)
        if opts.writerest and items:
            # flush last artist & write to ReST
            self.writerest(opts.writerest)
            ui.print_("ReST files generated. to build, use one of:")
            ui.print_("  sphinx-build -b html %s _build/html" % opts.writerest)
            ui.print_("  sphinx-build -b epub %s _build/epub" % opts.writerest)
            ui.print_(
                (
                    "  sphinx-build -b latex %s _build/latex "
                    "&& make -C _build/latex all-pdf"
                )
                % opts.writerest
            )

    cmd.func = func
    return [cmd]