gurudesh/pytracebugs huggingface dataset:
# Buggy code
def append(self, pane):
    from .pane import panel

    name = None
    if isinstance(pane, tuple):
        name, pane = pane
    new_objects = list(self)
    new_objects.append(panel(pane, name=name))
    name = param_name(new_objects[-1].name) if name is None else name
    self._names[-1] = name
    self.objects = new_objects


# Traceback (ModuleNotFoundError)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-100-8bd431d8f26d> in <module>
----> 1 pn.Column([])

~/panel/panel/layout.py in __init__(self, *objects, **params)
33     def __init__(self, *objects, **params):
34         from .pane import panel
---> 35         objects = [panel(pane) for pane in objects]
36         super(Panel, self).__init__(objects=objects, **params)
37

~/panel/panel/layout.py in <listcomp>(.0)
33     def __init__(self, *objects, **params):
34         from .pane import panel
---> 35         objects = [panel(pane) for pane in objects]
36         super(Panel, self).__init__(objects=objects, **params)
37

~/panel/panel/pane.py in panel(obj, **kwargs)
45     if kwargs.get('name', False) is None:
46         kwargs.pop('name')
---> 47     pane = PaneBase.get_pane_type(obj)(obj, **kwargs)
48     if len(pane.layout) == 1 and pane._unpack:
49         return pane.layout[0]

~/panel/panel/plotly.py in __init__(self, object, layout, **params)
41
42     def __init__(self, object, layout=None, **params):
---> 43         super(Plotly, self).__init__(self._to_figure(object, layout),
44                                      plotly_layout=layout, **params)
45

~/panel/panel/plotly.py in _to_figure(self, obj, layout)
50
51     def _to_figure(self, obj, layout={}):
---> 52         import plotly.graph_objs as go
53         if isinstance(obj, go.Figure):
54             fig = obj

ModuleNotFoundError: No module named 'plotly'

# Fixed code
def append(self, pane):
    from .pane import panel

    name = None
    if isinstance(pane, tuple):
        name, pane = pane
    new_objects = list(self)
    new_objects.append(panel(pane, name=name))
    name = param_name(new_objects[-1].name) if name is None else name
    self._names.append(name)
    self.objects = new_objects
# Buggy code
def applies(cls, obj):
    return (isinstance(obj, list) and all(cls.applies(o) for o in obj)) or hasattr(
        obj, "to_plotly_json"
    )


# Traceback (ModuleNotFoundError)
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-100-8bd431d8f26d> in <module>
----> 1 pn.Column([])

~/panel/panel/layout.py in __init__(self, *objects, **params)
33     def __init__(self, *objects, **params):
34         from .pane import panel
---> 35         objects = [panel(pane) for pane in objects]
36         super(Panel, self).__init__(objects=objects, **params)
37

~/panel/panel/layout.py in <listcomp>(.0)
33     def __init__(self, *objects, **params):
34         from .pane import panel
---> 35         objects = [panel(pane) for pane in objects]
36         super(Panel, self).__init__(objects=objects, **params)
37

~/panel/panel/pane.py in panel(obj, **kwargs)
45     if kwargs.get('name', False) is None:
46         kwargs.pop('name')
---> 47     pane = PaneBase.get_pane_type(obj)(obj, **kwargs)
48     if len(pane.layout) == 1 and pane._unpack:
49         return pane.layout[0]

~/panel/panel/plotly.py in __init__(self, object, layout, **params)
41
42     def __init__(self, object, layout=None, **params):
---> 43         super(Plotly, self).__init__(self._to_figure(object, layout),
44                                      plotly_layout=layout, **params)
45

~/panel/panel/plotly.py in _to_figure(self, obj, layout)
50
51     def _to_figure(self, obj, layout={}):
---> 52         import plotly.graph_objs as go
53         if isinstance(obj, go.Figure):
54             fig = obj

ModuleNotFoundError: No module named 'plotly'

# Fixed code
def applies(cls, obj):
    return (
        isinstance(obj, list) and obj and all(cls.applies(o) for o in obj)
    ) or hasattr(obj, "to_plotly_json")
# Buggy code
def body_fn(val):
    i, loss, opt_state_, rng_ = val
    loss, opt_state_, rng_ = svi_update(i, rng_, opt_state_)
    return i + 1, loss, opt_state_, rng_


# Traceback (RuntimeError)
test/test_examples.py::test_cpu[hmm.py --num-samples 100 --num-warmup 100 --num-chains 2] Running:
python examples/hmm.py --num-samples 100 --num-warmup 100 --num-chains 2
Simulating data...
Starting inference...
Traceback (most recent call last):
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 180, in <module>
main(args)
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 159, in main
supervised_words, unsupervised_words,
File "/home/travis/build/pyro-ppl/numpyro/numpyro/hmc_util.py", line 775, in initialize_model
raise RuntimeError("Cannot find valid initial parameters. Please check your model again.")
RuntimeError: Cannot find valid initial parameters. Please check your model again.
FAILED

# Fixed code
def body_fn(val, i):
    opt_state_, rng_ = val
    loss, opt_state_, rng_ = svi_update(i, rng_, opt_state_)
    return (opt_state_, rng_), loss
# Buggy code
def decoder(hidden_dim, out_dim):
    return stax.serial(
        stax.Dense(hidden_dim, W_init=stax.randn()),
        stax.Softplus,
        stax.Dense(out_dim, W_init=stax.randn()),
        Sigmoid,
    )


# Traceback (RuntimeError)
test/test_examples.py::test_cpu[hmm.py --num-samples 100 --num-warmup 100 --num-chains 2] Running:
python examples/hmm.py --num-samples 100 --num-warmup 100 --num-chains 2
Simulating data...
Starting inference...
Traceback (most recent call last):
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 180, in <module>
main(args)
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 159, in main
supervised_words, unsupervised_words,
File "/home/travis/build/pyro-ppl/numpyro/numpyro/hmc_util.py", line 775, in initialize_model
raise RuntimeError("Cannot find valid initial parameters. Please check your model again.")
RuntimeError: Cannot find valid initial parameters. Please check your model again.
FAILED

# Fixed code
def decoder(hidden_dim, out_dim):
    return stax.serial(
        stax.Dense(hidden_dim, W_init=stax.randn()),
        stax.Softplus,
        stax.Dense(out_dim, W_init=stax.randn()),
        stax.Sigmoid,
    )
# Buggy code
def epoch_train(opt_state, rng):
    def body_fn(i, val):
        loss_sum, opt_state, rng = val
        rng, rng_binarize = random.split(rng)
        batch = binarize(rng_binarize, train_fetch(i, train_idx)[0])
        loss, opt_state, rng = svi_update(
            i,
            rng,
            opt_state,
            (batch,),
            (batch,),
        )
        loss_sum += loss
        return loss_sum, opt_state, rng

    return lax.fori_loop(0, num_train, body_fn, (0.0, opt_state, rng))


# Traceback (RuntimeError)
test/test_examples.py::test_cpu[hmm.py --num-samples 100 --num-warmup 100 --num-chains 2] Running:
python examples/hmm.py --num-samples 100 --num-warmup 100 --num-chains 2
Simulating data...
Starting inference...
Traceback (most recent call last):
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 180, in <module>
main(args)
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 159, in main
supervised_words, unsupervised_words,
File "/home/travis/build/pyro-ppl/numpyro/numpyro/hmc_util.py", line 775, in initialize_model
raise RuntimeError("Cannot find valid initial parameters. Please check your model again.")
RuntimeError: Cannot find valid initial parameters. Please check your model again.
FAILED

# Fixed code
def epoch_train(opt_state, rng):
    def body_fn(i, val):
        loss_sum, opt_state, rng = val
        rng, rng_binarize = random.split(rng)
        batch = binarize(rng_binarize, train_fetch(i, train_idx)[0])
        # TODO: we will want to merge (i, rng, opt_state) into `svi_state`
        # Here the index `i` is reseted after each epoch, which causes no
        # problem for static learning rate, but it is not a right way for
        # scheduled learning rate.
        loss, opt_state, rng = svi_update(
            i,
            rng,
            opt_state,
            (batch,),
            (batch,),
        )
        loss_sum += loss
        return loss_sum, opt_state, rng

    return lax.fori_loop(0, num_train, body_fn, (0.0, opt_state, rng))
# Buggy code
def body_fn(i, val):
    loss_sum, opt_state, rng = val
    rng, rng_binarize = random.split(rng)
    batch = binarize(rng_binarize, train_fetch(i, train_idx)[0])
    loss, opt_state, rng = svi_update(
        i,
        rng,
        opt_state,
        (batch,),
        (batch,),
    )
    loss_sum += loss
    return loss_sum, opt_state, rng


# Traceback (RuntimeError)
test/test_examples.py::test_cpu[hmm.py --num-samples 100 --num-warmup 100 --num-chains 2] Running:
python examples/hmm.py --num-samples 100 --num-warmup 100 --num-chains 2
Simulating data...
Starting inference...
Traceback (most recent call last):
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 180, in <module>
main(args)
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 159, in main
supervised_words, unsupervised_words,
File "/home/travis/build/pyro-ppl/numpyro/numpyro/hmc_util.py", line 775, in initialize_model
raise RuntimeError("Cannot find valid initial parameters. Please check your model again.")
RuntimeError: Cannot find valid initial parameters. Please check your model again.
FAILED

# Fixed code
def body_fn(i, val):
    loss_sum, opt_state, rng = val
    rng, rng_binarize = random.split(rng)
    batch = binarize(rng_binarize, train_fetch(i, train_idx)[0])
    # TODO: we will want to merge (i, rng, opt_state) into `svi_state`
    # Here the index `i` is reseted after each epoch, which causes no
    # problem for static learning rate, but it is not a right way for
    # scheduled learning rate.
    loss, opt_state, rng = svi_update(
        i,
        rng,
        opt_state,
        (batch,),
        (batch,),
    )
    loss_sum += loss
    return loss_sum, opt_state, rng
# Buggy code
def get_dtypes(*args):
    return [canonicalize_dtype(onp.result_type(arg)) for arg in args]


# Traceback (RuntimeError)
test/test_examples.py::test_cpu[hmm.py --num-samples 100 --num-warmup 100 --num-chains 2] Running:
python examples/hmm.py --num-samples 100 --num-warmup 100 --num-chains 2
Simulating data...
Starting inference...
Traceback (most recent call last):
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 180, in <module>
main(args)
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 159, in main
supervised_words, unsupervised_words,
File "/home/travis/build/pyro-ppl/numpyro/numpyro/hmc_util.py", line 775, in initialize_model
raise RuntimeError("Cannot find valid initial parameters. Please check your model again.")
RuntimeError: Cannot find valid initial parameters. Please check your model again.
FAILED

# Fixed code
def get_dtypes(*args):
    return [canonicalize_dtype(lax.dtype(arg)) for arg in args]
# Buggy code
def matrix_to_tril_vec(x, diagonal=0):
    idxs = onp.tril_indices(x.shape[-1], diagonal)
    return x[..., idxs[0], idxs[1]]


# Traceback (RuntimeError)
test/test_examples.py::test_cpu[hmm.py --num-samples 100 --num-warmup 100 --num-chains 2] Running:
python examples/hmm.py --num-samples 100 --num-warmup 100 --num-chains 2
Simulating data...
Starting inference...
Traceback (most recent call last):
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 180, in <module>
main(args)
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 159, in main
supervised_words, unsupervised_words,
File "/home/travis/build/pyro-ppl/numpyro/numpyro/hmc_util.py", line 775, in initialize_model
raise RuntimeError("Cannot find valid initial parameters. Please check your model again.")
RuntimeError: Cannot find valid initial parameters. Please check your model again.
FAILED

# Fixed code
def matrix_to_tril_vec(x, diagonal=0):
    idxs = np.tril_indices(x.shape[-1], diagonal)
    return x[..., idxs[0], idxs[1]]
# Buggy code
def vec_to_tril_matrix(t, diagonal=0):
    # NB: the following formula only works for diagonal <= 0
    n = round((math.sqrt(1 + 8 * t.shape[-1]) - 1) / 2) - diagonal
    n2 = n * n
    idx = np.reshape(np.arange(n2), (n, n))[onp.tril_indices(n, diagonal)]
    x = lax.scatter_add(
        np.zeros(t.shape[:-1] + (n2,)),
        np.expand_dims(idx, axis=-1),
        t,
        lax.ScatterDimensionNumbers(
            update_window_dims=range(t.ndim - 1),
            inserted_window_dims=(t.ndim - 1,),
            scatter_dims_to_operand_dims=(t.ndim - 1,),
        ),
    )
    return np.reshape(x, x.shape[:-1] + (n, n))


# Traceback (RuntimeError)
test/test_examples.py::test_cpu[hmm.py --num-samples 100 --num-warmup 100 --num-chains 2] Running:
python examples/hmm.py --num-samples 100 --num-warmup 100 --num-chains 2
Simulating data...
Starting inference...
Traceback (most recent call last):
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 180, in <module>
main(args)
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 159, in main
supervised_words, unsupervised_words,
File "/home/travis/build/pyro-ppl/numpyro/numpyro/hmc_util.py", line 775, in initialize_model
raise RuntimeError("Cannot find valid initial parameters. Please check your model again.")
RuntimeError: Cannot find valid initial parameters. Please check your model again.
FAILED

# Fixed code
def vec_to_tril_matrix(t, diagonal=0):
    # NB: the following formula only works for diagonal <= 0
    n = round((math.sqrt(1 + 8 * t.shape[-1]) - 1) / 2) - diagonal
    n2 = n * n
    idx = np.reshape(np.arange(n2), (n, n))[np.tril_indices(n, diagonal)]
    x = lax.scatter_add(
        np.zeros(t.shape[:-1] + (n2,)),
        np.expand_dims(idx, axis=-1),
        t,
        lax.ScatterDimensionNumbers(
            update_window_dims=range(t.ndim - 1),
            inserted_window_dims=(t.ndim - 1,),
            scatter_dims_to_operand_dims=(t.ndim - 1,),
        ),
    )
    return np.reshape(x, x.shape[:-1] + (n, n))
# Buggy code
def init_to_median(site, num_samples=15, skip_param=False):
    """
    Initialize to the prior median.
    """
    if site["type"] == "sample" and not site["is_observed"]:
        if isinstance(site["fn"], dist.TransformedDistribution):
            fn = site["fn"].base_dist
        else:
            fn = site["fn"]
        samples = sample("_init", fn, sample_shape=(num_samples,))
        # TODO: use np.median when it is available upstream
        return np.mean(samples, axis=0)

    if site["type"] == "param" and not skip_param:
        # return base value of param site
        constraint = site["kwargs"].pop("constraint", real)
        transform = biject_to(constraint)
        value = site["args"][0]
        if isinstance(transform, ComposeTransform):
            base_transform = transform.parts[0]
            value = base_transform(transform.inv(value))
        return value


# Traceback (RuntimeError)
test/test_examples.py::test_cpu[hmm.py --num-samples 100 --num-warmup 100 --num-chains 2] Running:
python examples/hmm.py --num-samples 100 --num-warmup 100 --num-chains 2
Simulating data...
Starting inference...
Traceback (most recent call last):
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 180, in <module>
main(args)
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 159, in main
supervised_words, unsupervised_words,
File "/home/travis/build/pyro-ppl/numpyro/numpyro/hmc_util.py", line 775, in initialize_model
raise RuntimeError("Cannot find valid initial parameters. Please check your model again.")
RuntimeError: Cannot find valid initial parameters. Please check your model again.
FAILED

# Fixed code
def init_to_median(site, num_samples=15, skip_param=False):
    """
    Initialize to the prior median.
    """
    if site["type"] == "sample" and not site["is_observed"]:
        if isinstance(site["fn"], dist.TransformedDistribution):
            fn = site["fn"].base_dist
        else:
            fn = site["fn"]
        samples = sample("_init", fn, sample_shape=(num_samples,))
        return np.median(samples, axis=0)

    if site["type"] == "param" and not skip_param:
        # return base value of param site
        constraint = site["kwargs"].pop("constraint", real)
        transform = biject_to(constraint)
        value = site["args"][0]
        if isinstance(transform, ComposeTransform):
            base_transform = transform.parts[0]
            value = base_transform(transform.inv(value))
        return value
# Buggy code
def while_loop(cond_fun, body_fun, init_val):
    if _DISABLE_CONTROL_FLOW_PRIM:
        val = init_val
        while cond_fun(val):
            val = body_fun(val)
        return val
    else:
        # TODO: consider jitting while_loop similar to fori_loop
        return lax.while_loop(cond_fun, body_fun, init_val)


# Traceback (RuntimeError)
test/test_examples.py::test_cpu[hmm.py --num-samples 100 --num-warmup 100 --num-chains 2] Running:
python examples/hmm.py --num-samples 100 --num-warmup 100 --num-chains 2
Simulating data...
Starting inference...
Traceback (most recent call last):
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 180, in <module>
main(args)
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 159, in main
supervised_words, unsupervised_words,
File "/home/travis/build/pyro-ppl/numpyro/numpyro/hmc_util.py", line 775, in initialize_model
raise RuntimeError("Cannot find valid initial parameters. Please check your model again.")
RuntimeError: Cannot find valid initial parameters. Please check your model again.
FAILED

# Fixed code
def while_loop(cond_fun, body_fun, init_val):
    if _DISABLE_CONTROL_FLOW_PRIM:
        val = init_val
        while cond_fun(val):
            val = body_fun(val)
        return val
    else:
        return lax.while_loop(cond_fun, body_fun, init_val)
# Buggy code
def fori_loop(lower, upper, body_fun, init_val):
    if _DISABLE_CONTROL_FLOW_PRIM:
        val = init_val
        for i in range(int(lower), int(upper)):
            val = body_fun(i, val)
        return val
    else:
        return jit(lax.fori_loop, static_argnums=(2,))(lower, upper, body_fun, init_val)


# Traceback (RuntimeError)
test/test_examples.py::test_cpu[hmm.py --num-samples 100 --num-warmup 100 --num-chains 2] Running:
python examples/hmm.py --num-samples 100 --num-warmup 100 --num-chains 2
Simulating data...
Starting inference...
Traceback (most recent call last):
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 180, in <module>
main(args)
File "/home/travis/build/pyro-ppl/numpyro/examples/hmm.py", line 159, in main
supervised_words, unsupervised_words,
File "/home/travis/build/pyro-ppl/numpyro/numpyro/hmc_util.py", line 775, in initialize_model
raise RuntimeError("Cannot find valid initial parameters. Please check your model again.")
RuntimeError: Cannot find valid initial parameters. Please check your model again.
FAILED

# Fixed code
def fori_loop(lower, upper, body_fun, init_val):
    if _DISABLE_CONTROL_FLOW_PRIM:
        val = init_val
        for i in range(int(lower), int(upper)):
            val = body_fun(i, val)
        return val
    else:
        return lax.fori_loop(lower, upper, body_fun, init_val)
# Buggy code
def visit_Name(self, node: ast.Name) -> None:
    if self._is_six(node, SIX_SIMPLE_ATTRS):
        self.six_simple[_ast_to_offset(node)] = node

    if self._scope_stack:
        if isinstance(node.ctx, ast.Load):
            self._scope_stack[-1].reads.add(node.id)
        elif isinstance(node.ctx, ast.Store):
            self._scope_stack[-1].writes.add(node.id)
        else:
            raise AssertionError(node)

    self.generic_visit(node)


# Traceback (AssertionError)
Traceback (most recent call last):
File "./venv/bin/pyupgrade", line 8, in <module>
sys.exit(main())
File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 2680, in main
ret |= _fix_file(filename, args)
File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 2638, in _fix_file
contents_text = _fix_py3_plus(contents_text, args.min_version)
File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 2002, in _fix_py3_plus
visitor.visit(ast_obj)
File "/usr/lib64/python3.8/ast.py", line 360, in visit
return visitor(node)
File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 1705, in generic_visit
super().generic_visit(node)
File "/usr/lib64/python3.8/ast.py", line 368, in generic_visit
self.visit(item)
File "/usr/lib64/python3.8/ast.py", line 360, in visit
return visitor(node)
File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 1418, in _visit_sync_func
self._visit_func(node)
File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 1414, in _visit_func
self.generic_visit(node)
File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 1705, in generic_visit
super().generic_visit(node)
File "/usr/lib64/python3.8/ast.py", line 368, in generic_visit
self.visit(item)
File "/usr/lib64/python3.8/ast.py", line 360, in visit
return visitor(node)
File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 1705, in generic_visit
super().generic_visit(node)
File "/usr/lib64/python3.8/ast.py", line 368, in generic_visit
self.visit(item)
File "/usr/lib64/python3.8/ast.py", line 360, in visit
return visitor(node)
File "/home/jon/venv/lib64/python3.8/site-packages/pyupgrade.py", line 1454, in visit_Name
raise AssertionError(node)
AssertionError: <_ast.Name object at 0x7f6b26fd0a90>

# Fixed code
def visit_Name(self, node: ast.Name) -> None:
    if self._is_six(node, SIX_SIMPLE_ATTRS):
        self.six_simple[_ast_to_offset(node)] = node

    if self._scope_stack:
        if isinstance(node.ctx, ast.Load):
            self._scope_stack[-1].reads.add(node.id)
        elif isinstance(node.ctx, (ast.Store, ast.Del)):
            self._scope_stack[-1].writes.add(node.id)
        else:
            raise AssertionError(node)

    self.generic_visit(node)
# Buggy code
def visit_ClassDef(self, node):  # type: (ast.ClassDef) -> None
    for decorator in node.decorator_list:
        if self._is_six(decorator, ("python_2_unicode_compatible",)):
            self.six_remove_decorators.add(_ast_to_offset(decorator))

    for base in node.bases:
        if isinstance(base, ast.Name) and base.id == "object":
            self.bases_to_remove.add(_ast_to_offset(base))
        elif self._is_six(base, ("Iterator",)):
            self.bases_to_remove.add(_ast_to_offset(base))

    if (
        len(node.bases) == 1
        and isinstance(node.bases[0], ast.Call)
        and self._is_six(node.bases[0].func, ("with_metaclass",))
    ):
        self.six_with_metaclass.add(_ast_to_offset(node.bases[0]))

    self._class_info_stack.append(FindPy3Plus.ClassInfo(node.name))
    self.generic_visit(node)
    self._class_info_stack.pop()


# Traceback (IndexError)
% pyupgrade --py3-only --keep-percent-format src/_pytest/fixtures.py
Traceback (most recent call last):
File "…/Vcs/pytest/.venv/bin/pyupgrade", line 11, in <module>
sys.exit(main())
File "…/Vcs/pytest/.venv/lib/python3.7/site-packages/pyupgrade.py", line 1428, in main
ret |= fix_file(filename, args)
File "…/Vcs/pytest/.venv/lib/python3.7/site-packages/pyupgrade.py", line 1402, in fix_file
contents_text = _fix_py3_plus(contents_text)
File "…/Vcs/pytest/.venv/lib/python3.7/site-packages/pyupgrade.py", line 1244, in _fix_py3_plus
_replace_call(tokens, i, end, func_args, template)
File "…/Vcs/pytest/.venv/lib/python3.7/site-packages/pyupgrade.py", line 1171, in _replace_call
src = tmpl.format(args=arg_strs, rest=rest)
IndexError: list index out of range

# Fixed code
def visit_ClassDef(self, node):  # type: (ast.ClassDef) -> None
    for decorator in node.decorator_list:
        if self._is_six(decorator, ("python_2_unicode_compatible",)):
            self.six_remove_decorators.add(_ast_to_offset(decorator))

    for base in node.bases:
        if isinstance(base, ast.Name) and base.id == "object":
            self.bases_to_remove.add(_ast_to_offset(base))
        elif self._is_six(base, ("Iterator",)):
            self.bases_to_remove.add(_ast_to_offset(base))

    if (
        len(node.bases) == 1
        and isinstance(node.bases[0], ast.Call)
        and self._is_six(node.bases[0].func, ("with_metaclass",))
        and not _starargs(node.bases[0])
    ):
        self.six_with_metaclass.add(_ast_to_offset(node.bases[0]))

    self._class_info_stack.append(FindPy3Plus.ClassInfo(node.name))
    self.generic_visit(node)
    self._class_info_stack.pop()
# Buggy code
def _fix_format_literals(contents_text):
    tokens = src_to_tokens(contents_text)

    to_replace = []
    string_start = None
    string_end = None
    seen_dot = False

    for i, token in enumerate(tokens):
        if string_start is None and token.name == "STRING":
            string_start = i
            string_end = i + 1
        elif string_start is not None and token.name == "STRING":
            string_end = i + 1
        elif string_start is not None and token.src == ".":
            seen_dot = True
        elif seen_dot and token.src == "format":
            to_replace.append((string_start, string_end))
            string_start, string_end, seen_dot = None, None, False
        elif token.name not in NON_CODING_TOKENS:
            string_start, string_end, seen_dot = None, None, False

    for start, end in reversed(to_replace):
        src = tokens_to_src(tokens[start:end])
        new_src = _rewrite_string_literal(src)
        tokens[start:end] = [Token("STRING", new_src)]

    return tokens_to_src(tokens)


# Traceback (tokenize.TokenError)
filename1.py
filename2.py
Traceback (most recent call last):
File "/home/thomas/miniconda/envs/py37/bin/pyupgrade", line 10, in <module>
sys.exit(main())
File "/home/thomas/miniconda/envs/py37/lib/python3.7/site-packages/pyupgrade.py", line 1428, in main
ret |= fix_file(filename, args)
File "/home/thomas/miniconda/envs/py37/lib/python3.7/site-packages/pyupgrade.py", line 1397, in fix_file
contents_text = _fix_format_literals(contents_text)
File "/home/thomas/miniconda/envs/py37/lib/python3.7/site-packages/pyupgrade.py", line 107, in _fix_format_literals
tokens = src_to_tokens(contents_text)
File "/home/thomas/miniconda/envs/py37/lib/python3.7/site-packages/tokenize_rt.py", line 44, in src_to_tokens
) in tokenize.generate_tokens(tokenize_target.readline):
File "/home/thomas/miniconda/envs/py37/lib/python3.7/tokenize.py", line 579, in _tokenize
raise TokenError("EOF in multi-line statement", (lnum, 0))
tokenize.TokenError: ('EOF in multi-line statement', (350, 0))

# Fixed code
def _fix_format_literals(contents_text):
    try:
        tokens = src_to_tokens(contents_text)
    except tokenize.TokenError:
        return contents_text

    to_replace = []
    string_start = None
    string_end = None
    seen_dot = False

    for i, token in enumerate(tokens):
        if string_start is None and token.name == "STRING":
            string_start = i
            string_end = i + 1
        elif string_start is not None and token.name == "STRING":
            string_end = i + 1
        elif string_start is not None and token.src == ".":
            seen_dot = True
        elif seen_dot and token.src == "format":
            to_replace.append((string_start, string_end))
            string_start, string_end, seen_dot = None, None, False
        elif token.name not in NON_CODING_TOKENS:
            string_start, string_end, seen_dot = None, None, False

    for start, end in reversed(to_replace):
        src = tokens_to_src(tokens[start:end])
        new_src = _rewrite_string_literal(src)
        tokens[start:end] = [Token("STRING", new_src)]

    return tokens_to_src(tokens)
# Buggy code
def _fix_py2_compatible(contents_text):
    try:
        ast_obj = ast_parse(contents_text)
    except SyntaxError:
        return contents_text
    visitor = Py2CompatibleVisitor()
    visitor.visit(ast_obj)
    if not any(
        (
            visitor.dicts,
            visitor.sets,
            visitor.set_empty_literals,
            visitor.is_literal,
        )
    ):
        return contents_text

    tokens = src_to_tokens(contents_text)
    for i, token in reversed_enumerate(tokens):
        if token.offset in visitor.dicts:
            _process_dict_comp(tokens, i, visitor.dicts[token.offset])
        elif token.offset in visitor.set_empty_literals:
            _process_set_empty_literal(tokens, i)
        elif token.offset in visitor.sets:
            _process_set_literal(tokens, i, visitor.sets[token.offset])
        elif token.offset in visitor.is_literal:
            _process_is_literal(tokens, i, visitor.is_literal[token.offset])
    return tokens_to_src(tokens)


# Traceback (tokenize.TokenError)
filename1.py
filename2.py
Traceback (most recent call last):
File "/home/thomas/miniconda/envs/py37/bin/pyupgrade", line 10, in <module>
sys.exit(main())
File "/home/thomas/miniconda/envs/py37/lib/python3.7/site-packages/pyupgrade.py", line 1428, in main
ret |= fix_file(filename, args)
File "/home/thomas/miniconda/envs/py37/lib/python3.7/site-packages/pyupgrade.py", line 1397, in fix_file
contents_text = _fix_format_literals(contents_text)
File "/home/thomas/miniconda/envs/py37/lib/python3.7/site-packages/pyupgrade.py", line 107, in _fix_format_literals
tokens = src_to_tokens(contents_text)
File "/home/thomas/miniconda/envs/py37/lib/python3.7/site-packages/tokenize_rt.py", line 44, in src_to_tokens
) in tokenize.generate_tokens(tokenize_target.readline):
File "/home/thomas/miniconda/envs/py37/lib/python3.7/tokenize.py", line 579, in _tokenize
raise TokenError("EOF in multi-line statement", (lnum, 0))
tokenize.TokenError: ('EOF in multi-line statement', (350, 0))

# Fixed code
def _fix_py2_compatible(contents_text):
    try:
        ast_obj = ast_parse(contents_text)
    except SyntaxError:
        return contents_text
    visitor = Py2CompatibleVisitor()
    visitor.visit(ast_obj)
    if not any(
        (
            visitor.dicts,
            visitor.sets,
            visitor.set_empty_literals,
            visitor.is_literal,
        )
    ):
        return contents_text

    try:
        tokens = src_to_tokens(contents_text)
    except tokenize.TokenError:  # pragma: no cover (bpo-2180)
        return contents_text
    for i, token in reversed_enumerate(tokens):
        if token.offset in visitor.dicts:
            _process_dict_comp(tokens, i, visitor.dicts[token.offset])
        elif token.offset in visitor.set_empty_literals:
            _process_set_empty_literal(tokens, i)
        elif token.offset in visitor.sets:
            _process_set_literal(tokens, i, visitor.sets[token.offset])
        elif token.offset in visitor.is_literal:
            _process_is_literal(tokens, i, visitor.is_literal[token.offset])
    return tokens_to_src(tokens)
# Buggy code
def _fix_percent_format(contents_text):
    try:
        ast_obj = ast_parse(contents_text)
    except SyntaxError:
        return contents_text

    visitor = FindPercentFormats()
    visitor.visit(ast_obj)

    if not visitor.found:
        return contents_text

    tokens = src_to_tokens(contents_text)

    for i, token in reversed_enumerate(tokens):
        node = visitor.found.get(token.offset)
        if node is None:
            continue

        # no .format() equivalent for bytestrings in py3
        # note that this code is only necessary when running in python2
        if _is_bytestring(tokens[i].src):  # pragma: no cover (py2-only)
            continue

        if isinstance(node.right, ast.Tuple):
            _fix_percent_format_tuple(tokens, i, node)
        elif isinstance(node.right, ast.Dict):
            _fix_percent_format_dict(tokens, i, node)

    return tokens_to_src(tokens)


# Traceback (tokenize.TokenError)
filename1.py
filename2.py
Traceback (most recent call last):
File "/home/thomas/miniconda/envs/py37/bin/pyupgrade", line 10, in <module>
sys.exit(main())
File "/home/thomas/miniconda/envs/py37/lib/python3.7/site-packages/pyupgrade.py", line 1428, in main
ret |= fix_file(filename, args)
File "/home/thomas/miniconda/envs/py37/lib/python3.7/site-packages/pyupgrade.py", line 1397, in fix_file
contents_text = _fix_format_literals(contents_text)
File "/home/thomas/miniconda/envs/py37/lib/python3.7/site-packages/pyupgrade.py", line 107, in _fix_format_literals
tokens = src_to_tokens(contents_text)
File "/home/thomas/miniconda/envs/py37/lib/python3.7/site-packages/tokenize_rt.py", line 44, in src_to_tokens
) in tokenize.generate_tokens(tokenize_target.readline):
File "/home/thomas/miniconda/envs/py37/lib/python3.7/tokenize.py", line 579, in _tokenize
raise TokenError("EOF in multi-line statement", (lnum, 0))
tokenize.TokenError: ('EOF in multi-line statement', (350, 0))

# Fixed code
def _fix_percent_format(contents_text):
    try:
        ast_obj = ast_parse(contents_text)
    except SyntaxError:
        return contents_text

    visitor = FindPercentFormats()
    visitor.visit(ast_obj)

    if not visitor.found:
        return contents_text

    try:
        tokens = src_to_tokens(contents_text)
    except tokenize.TokenError:  # pragma: no cover (bpo-2180)
        return contents_text

    for i, token in reversed_enumerate(tokens):
        node = visitor.found.get(token.offset)
        if node is None:
            continue

        # no .format() equivalent for bytestrings in py3
        # note that this code is only necessary when running in python2
        if _is_bytestring(tokens[i].src):  # pragma: no cover (py2-only)
            continue

        if isinstance(node.right, ast.Tuple):
            _fix_percent_format_tuple(tokens, i, node)
        elif isinstance(node.right, ast.Dict):
            _fix_percent_format_dict(tokens, i, node)

    return tokens_to_src(tokens)
# Buggy code
def visit_Call(self, node):  # type: (ast.Call) -> None
    if (
        isinstance(node.func, ast.Attribute)
        and isinstance(node.func.value, ast.Str)
        and node.func.attr == "format"
        and all(_simple_arg(arg) for arg in node.args)
        and all(_simple_arg(k.value) for k in node.keywords)
        and not _starargs(node)
    ):
        seen = set()
        for _, name, spec, _ in parse_format(node.func.value.s):
            # timid: difficult to rewrite correctly
            if spec is not None and "{" in spec:
                break
            if name is not None:
                candidate, _, _ = name.partition(".")
                # timid: could make the f-string longer
                if candidate and candidate in seen:
                    break
                seen.add(candidate)
        else:
            self.found[_ast_to_offset(node)] = node

    self.generic_visit(node)


# Traceback (KeyError)
Traceback (most recent call last):
File "/home/ethan/venv/bin/pyupgrade", line 10, in <module>
sys.exit(main())
File "/home/ethan/venv/lib/python3.7/site-packages/pyupgrade.py", line 1396, in main
ret |= fix_file(filename, args)
File "/home/ethan/venv/lib/python3.7/site-packages/pyupgrade.py", line 1372, in fix_file
contents_text = _fix_fstrings(contents_text)
File "/home/ethan/venv/lib/python3.7/site-packages/pyupgrade.py", line 1348, in _fix_fstrings
tokens[i] = token._replace(src=_to_fstring(token.src, node))
File "/home/ethan/venv/lib/python3.7/site-packages/pyupgrade.py", line 1310, in _to_fstring
name = ''.join((params[k or str(i)], dot, rest))
KeyError: '0[a]'

# Fixed code
def visit_Call(self, node):  # type: (ast.Call) -> None
    if (
        isinstance(node.func, ast.Attribute)
        and isinstance(node.func.value, ast.Str)
        and node.func.attr == "format"
        and all(_simple_arg(arg) for arg in node.args)
        and all(_simple_arg(k.value) for k in node.keywords)
        and not _starargs(node)
    ):
        seen = set()
        for _, name, spec, _ in parse_format(node.func.value.s):
            # timid: difficult to rewrite correctly
            if spec is not None and "{" in spec:
                break
            if name is not None:
                candidate, _, _ = name.partition(".")
                # timid: could make the f-string longer
                if candidate and candidate in seen:
                    break
                # timid: bracketed
                elif "[" in candidate:
                    break
                seen.add(candidate)
        else:
            self.found[_ast_to_offset(node)] = node

    self.generic_visit(node)
# Buggy code
def visit_Call(self, node):  # type: (ast.Call) -> None
    if (
        isinstance(node.func, ast.Attribute)
        and isinstance(node.func.value, ast.Str)
        and node.func.attr == "format"
        and all(_simple_arg(arg) for arg in node.args)
        and all(_simple_arg(k.value) for k in node.keywords)
        and not _starargs(node)
    ):
        seen = set()
        for _, name, spec, _ in parse_format(node.func.value.s):
            # timid: difficult to rewrite correctly
            if spec is not None and "{" in spec:
                break
            if name is not None:
                candidate, _, _ = name.partition(".")
                # timid: could make the f-string longer
                if candidate and candidate in seen:
                    break
                # timid: bracketed
                elif "[" in candidate:
                    break
                seen.add(candidate)
        else:
            self.found[_ast_to_offset(node)] = node

    self.generic_visit(node)


# Traceback (KeyError)
Traceback (most recent call last):
File "/home/ethan/venv/bin/pyupgrade", line 10, in <module>
sys.exit(main())
File "/home/ethan/venv/lib/python3.7/site-packages/pyupgrade.py", line 1396, in main
ret |= fix_file(filename, args)
File "/home/ethan/venv/lib/python3.7/site-packages/pyupgrade.py", line 1372, in fix_file
contents_text = _fix_fstrings(contents_text)
File "/home/ethan/venv/lib/python3.7/site-packages/pyupgrade.py", line 1348, in _fix_fstrings
tokens[i] = token._replace(src=_to_fstring(token.src, node))
File "/home/ethan/venv/lib/python3.7/site-packages/pyupgrade.py", line 1310, in _to_fstring
name = ''.join((params[k or str(i)], dot, rest))
KeyError: '0[a]'

# Fixed code
def visit_Call(self, node):  # type: (ast.Call) -> None
    if (
        isinstance(node.func, ast.Attribute)
        and isinstance(node.func.value, ast.Str)
        and node.func.attr == "format"
        and all(_simple_arg(arg) for arg in node.args)
        and all(_simple_arg(k.value) for k in node.keywords)
        and not _starargs(node)
    ):
        seen = set()
        for _, name, spec, _ in parse_format(node.func.value.s):
            # timid: difficult to rewrite correctly
            if spec is not None and "{" in spec:
                break
            if name is not None:
                candidate, _, _ = name.partition(".")
                # timid: could make the f-string longer
                if candidate and candidate in seen:
                    break
                # timid: bracketed
                elif "[" in name:
                    break
                seen.add(candidate)
        else:
            self.found[_ast_to_offset(node)] = node

    self.generic_visit(node)
# Buggy code
def _to_fstring(src, call):
    params = {}
    for i, arg in enumerate(call.args):
        params[str(i)] = _unparse(arg)
    for kwd in call.keywords:
        params[kwd.arg] = _unparse(kwd.value)

    parts = []
    for i, (s, name, spec, conv) in enumerate(parse_format("f" + src)):
        if name is not None:
            k, dot, rest = name.partition(".")
            name = "".join((params[k or str(i)], dot, rest))
        parts.append((s, name, spec, conv))
    return unparse_parsed_string(parts)


# Traceback (KeyError)
Traceback (most recent call last):
File "/usr/bin/pyupgrade", line 11, in <module>
sys.exit(main())
File "/usr/lib/python3.7/site-packages/pyupgrade.py", line 907, in main
ret |= fix_file(filename, args)
File "/usr/lib/python3.7/site-packages/pyupgrade.py", line 884, in fix_file
contents_text = _fix_fstrings(contents_text)
File "/usr/lib/python3.7/site-packages/pyupgrade.py", line 858, in _fix_fstrings
tokens[i] = token._replace(src=_to_fstring(token.src, node))
File "/usr/lib/python3.7/site-packages/pyupgrade.py", line 824, in _to_fstring
name = ''.join((params[k or str(i)], dot, rest))
KeyError: '3'

# Fixed code
def _to_fstring(src, call):
    params = {}
    for i, arg in enumerate(call.args):
        params[str(i)] = _unparse(arg)
    for kwd in call.keywords:
        params[kwd.arg] = _unparse(kwd.value)

    parts = []
    i = 0
    for s, name, spec, conv in parse_format("f" + src):
        if name is not None:
            k, dot, rest = name.partition(".")
            name = "".join((params[k or str(i)], dot, rest))
            i += 1
        parts.append((s, name, spec, conv))
    return unparse_parsed_string(parts)
# Buggy code
def format_channel_name(author, guild, exclude_channel=None):
    """Sanitises a username for use with text channel names"""
    name = author.name.lower()
    name = new_name = (
        "".join(l for l in name if l not in string.punctuation and l.isprintable())
        or "null"
    ) + f"-{author.discriminator}"

    counter = 1
    existed = set(c.name for c in guild.text_channels if c != exclude_channel)
    while new_name in existed:
        new_name = f"{name}_{counter}"  # multiple channels with same name
        counter += 1

    return new_name


# Traceback (AttributeError)
discord.errors.HTTPException: 400 Bad Request (error code: 50035): Invalid Form Body
In name: Contains words not allowed for servers in Server Discovery.
2021-01-11 08:45:24 __main__[762] - ERROR: Failed to send message:
Traceback (most recent call last):
File "/app/bot.py", line 760, in process_dm_modmail
await thread.send(message)
File "/app/core/thread.py", line 713, in send
self.bot.loop.create_task(self.bot.api.append_log(message, channel_id=self.channel.id))
AttributeError: 'NoneType' object has no attribute 'id'
2021-01-11 08:45:30 core.thread[102] - CRITICAL: An error occurred while creating a thread.
Traceback (most recent call last):
File "/app/core/thread.py", line 95, in setup
channel = await self.bot.modmail_guild.create_text_channel(
File "/app/.heroku/python/lib/python3.9/site-packages/discord/guild.py", line 905, in create_text_channel
data = await self._create_channel(name, overwrites, ChannelType.text, category, reason=reason, **options)
File "/app/.heroku/python/lib/python3.9/site-packages/discord/http.py", line 245, in request
raise HTTPException(r, data)

# Fixed code
def format_channel_name(author, guild, exclude_channel=None, force_null=False):
    """Sanitises a username for use with text channel names"""
    name = author.name.lower()
    if force_null:
        name = "null"

    name = new_name = (
        "".join(l for l in name if l not in string.punctuation and l.isprintable())
        or "null"
    ) + f"-{author.discriminator}"

    counter = 1
    existed = set(c.name for c in guild.text_channels if c != exclude_channel)
    while new_name in existed:
        new_name = f"{name}_{counter}"  # multiple channels with same name
        counter += 1

    return new_name
# Buggy code
async def on_raw_reaction_add(self, payload):
    await self.handle_reaction_events(payload)


# Traceback (AttributeError)
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.7/site-packages/discord/client.py", line 270, in _run_event
await coro(*args, **kwargs)
File "bot.py", line 868, in on_message
await self.process_commands(message)
File "bot.py", line 875, in process_commands
return await self.process_dm_modmail(message)
File "bot.py", line 744, in process_dm_modmail
thread = await self.threads.create(message.author)
File "/app/core/thread.py", line 988, in create
if category is None and len(cat.channels) == 50:
AttributeError: 'NoneType' object has no attribute 'channels'
2020-04-16 03:23:27 core.thread[918] - WARNING: Found existing thread for 301194255731392513 but the channel is invalid.
2020-04-16 03:23:30 core.thread[977] - WARNING: Found an existing thread for ElijahPepe#4897, closing previous thread.
2020-04-16 03:23:30 __main__[1126] - ERROR: Ignoring exception in on_message.
2020-04-16 03:23:30 __main__[1127] - ERROR: Unexpected exception:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.7/site-packages/discord/client.py", line 270, in _run_event
await coro(*args, **kwargs)
File "bot.py", line 868, in on_message
await self.process_commands(message)
File "bot.py", line 875, in process_commands
return await self.process_dm_modmail(message)
File "bot.py", line 744, in process_dm_modmail
thread = await self.threads.create(message.author)
File "/app/core/thread.py", line 988, in create
if category is None and len(cat.channels) == 50:
AttributeError: 'NoneType' object has no attribute 'channels'

# Fixed code
async def on_raw_reaction_add(self, payload):
    if self.config["transfer_reactions"]:
        await self.handle_reaction_events(payload)
# Buggy code
async def on_raw_reaction_remove(self, payload):
    await self.handle_reaction_events(payload)


# Traceback (AttributeError)
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.7/site-packages/discord/client.py", line 270, in _run_event
await coro(*args, **kwargs)
File "bot.py", line 868, in on_message
await self.process_commands(message)
File "bot.py", line 875, in process_commands
return await self.process_dm_modmail(message)
File "bot.py", line 744, in process_dm_modmail
thread = await self.threads.create(message.author)
File "/app/core/thread.py", line 988, in create
if category is None and len(cat.channels) == 50:
AttributeError: 'NoneType' object has no attribute 'channels'
2020-04-16 03:23:27 core.thread[918] - WARNING: Found existing thread for 301194255731392513 but the channel is invalid.
2020-04-16 03:23:30 core.thread[977] - WARNING: Found an existing thread for ElijahPepe#4897, closing previous thread.
2020-04-16 03:23:30 __main__[1126] - ERROR: Ignoring exception in on_message.
2020-04-16 03:23:30 __main__[1127] - ERROR: Unexpected exception:
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.7/site-packages/discord/client.py", line 270, in _run_event
await coro(*args, **kwargs)
File "bot.py", line 868, in on_message
await self.process_commands(message)
File "bot.py", line 875, in process_commands
return await self.process_dm_modmail(message)
File "bot.py", line 744, in process_dm_modmail
thread = await self.threads.create(message.author)
File "/app/core/thread.py", line 988, in create
if category is None and len(cat.channels) == 50:
AttributeError: 'NoneType' object has no attribute 'channels'

# Fixed code
async def on_raw_reaction_remove(self, payload):
    if self.config["transfer_reactions"]:
        await self.handle_reaction_events(payload)