@cacheit
def _expand_delta(expr, index):
    if not expr.is_Mul:
        return expr
    delta = None
    func = Add
    terms = [S(1)]
    for h in expr.args:
        if delta is None and h.is_Add and _has_simple_delta(h, index):
            delta = True
            func = h.func
            terms = [terms[0]*t for t in h.args]
        else:
            terms = [t*h for t in terms]
    return func(*terms)
Expand the first Add containing a simple KroneckerDelta.
dataset/ETHPy150Open sympy/sympy/sympy/concrete/delta.py/_expand_delta