# Buggy code
def cmd_batch(
    self, tgt, fun, arg=(), tgt_type="glob", ret="", kwarg=None, batch="10%", **kwargs
):
    """
    Iteratively execute a command on subsets of minions at a time

    The function signature is the same as :py:meth:`cmd` with the
    following exceptions.

    :param batch: The batch identifier of systems to execute on

    :returns: A generator of minion returns

    .. code-block:: python

        >>> returns = local.cmd_batch('*', 'state.highstate', batch='10%')
        >>> for ret in returns:
        ...     print(ret)
        {'jerry': {...}}
        {'dave': {...}}
        {'stewart': {...}}
    """
    if "expr_form" in kwargs:
        salt.utils.warn_until(
            "Fluorine",
            "The target type should be passed using the 'tgt_type' "
            "argument instead of 'expr_form'. Support for using "
            "'expr_form' will be removed in Salt Fluorine.",
        )
        tgt_type = kwargs.pop("expr_form")

    import salt.cli.batch

    arg = salt.utils.args.condition_input(arg, kwarg)
    opts = {
        "tgt": tgt,
        "fun": fun,
        "arg": arg,
        "tgt_type": tgt_type,
        "ret": ret,
        "batch": batch,
        "raw": kwargs.get("raw", False),
    }

    if "timeout" in kwargs:
        opts["timeout"] = kwargs["timeout"]
    if "gather_job_timeout" in kwargs:
        opts["gather_job_timeout"] = kwargs["gather_job_timeout"]

    eauth = {}
    if "eauth" in kwargs:
        eauth["eauth"] = kwargs.pop("eauth")
    if "username" in kwargs:
        eauth["username"] = kwargs.pop("username")
    if "password" in kwargs:
        eauth["password"] = kwargs.pop("password")
    if "token" in kwargs:
        eauth["token"] = kwargs.pop("token")

    for key, val in six.iteritems(self.opts):
        if key not in opts:
            opts[key] = val
    batch = salt.cli.batch.Batch(opts, eauth=eauth, quiet=True)
    for ret in batch.run():
        yield ret


# Traceback (UnboundLocalError)
[DEBUG   ][19087] Error while processing request for: /
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/salt/netapi/rest_cherrypy/app.py", line 737, in hypermedia_handler
ret = cherrypy.serving.request._hypermedia_inner_handler(*args, **kwargs)
File "/usr/lib64/python2.7/site-packages/cherrypy/_cpdispatch.py", line 60, in __call__
return self.callable(*self.args, **self.kwargs)
File "/usr/lib/python2.7/site-packages/salt/netapi/rest_cherrypy/app.py", line 1172, in POST
token=cherrypy.session.get('token')))
File "/usr/lib/python2.7/site-packages/salt/netapi/rest_cherrypy/app.py", line 1068, in exec_lowstate
for i in ret:
File "/usr/lib/python2.7/site-packages/salt/client/__init__.py", line 547, in cmd_batch
salt.utils.warn_until(
UnboundLocalError: local variable 'salt' referenced before assignment

# Fixed code
def cmd_batch(
    self, tgt, fun, arg=(), tgt_type="glob", ret="", kwarg=None, batch="10%", **kwargs
):
    """
    Iteratively execute a command on subsets of minions at a time

    The function signature is the same as :py:meth:`cmd` with the
    following exceptions.

    :param batch: The batch identifier of systems to execute on

    :returns: A generator of minion returns

    .. code-block:: python

        >>> returns = local.cmd_batch('*', 'state.highstate', batch='10%')
        >>> for ret in returns:
        ...     print(ret)
        {'jerry': {...}}
        {'dave': {...}}
        {'stewart': {...}}
    """
    if "expr_form" in kwargs:
        import salt

        salt.utils.warn_until(
            "Fluorine",
            "The target type should be passed using the 'tgt_type' "
            "argument instead of 'expr_form'. Support for using "
            "'expr_form' will be removed in Salt Fluorine.",
        )
        tgt_type = kwargs.pop("expr_form")

    import salt.cli.batch

    arg = salt.utils.args.condition_input(arg, kwarg)
    opts = {
        "tgt": tgt,
        "fun": fun,
        "arg": arg,
        "tgt_type": tgt_type,
        "ret": ret,
        "batch": batch,
        "raw": kwargs.get("raw", False),
    }

    if "timeout" in kwargs:
        opts["timeout"] = kwargs["timeout"]
    if "gather_job_timeout" in kwargs:
        opts["gather_job_timeout"] = kwargs["gather_job_timeout"]

    eauth = {}
    if "eauth" in kwargs:
        eauth["eauth"] = kwargs.pop("eauth")
    if "username" in kwargs:
        eauth["username"] = kwargs.pop("username")
    if "password" in kwargs:
        eauth["password"] = kwargs.pop("password")
    if "token" in kwargs:
        eauth["token"] = kwargs.pop("token")

    for key, val in six.iteritems(self.opts):
        if key not in opts:
            opts[key] = val
    batch = salt.cli.batch.Batch(opts, eauth=eauth, quiet=True)
    for ret in batch.run():
        yield ret