# Buggy code
def is_assigned(self, node):
    assigned = False
    if self.ignore_nodes:
        if isinstance(self.ignore_nodes, (list, tuple, object)):
            if isinstance(node, self.ignore_nodes):
                return assigned

    if isinstance(node, ast.Expr):
        assigned = self.is_assigned(node.value)
    elif isinstance(node, ast.FunctionDef):
        for name in node.args.args:
            if isinstance(name, ast.Name):
                if name.id == self.var_name.id:
                    # If is param the assignations are not affected
                    return assigned
        assigned = self.is_assigned_in(node.body)
    elif isinstance(node, ast.With):
        if six.PY2:
            if node.optional_vars.id == self.var_name.id:
                assigned = node
            else:
                assigned = self.is_assigned_in(node.body)
        else:
            for withitem in node.items:
                if withitem.optional_vars.id == self.var_name.id:
                    assigned = node
                else:
                    assigned = self.is_assigned_in(node.body)
    elif six.PY2 and isinstance(node, ast.TryFinally):
        assigned = []
        assigned.extend(self.is_assigned_in(node.body))
        assigned.extend(self.is_assigned_in(node.finalbody))
    elif six.PY2 and isinstance(node, ast.TryExcept):
        assigned = []
        assigned.extend(self.is_assigned_in(node.body))
        assigned.extend(self.is_assigned_in(node.handlers))
        assigned.extend(self.is_assigned_in(node.orelse))
    elif not six.PY2 and isinstance(node, ast.Try):
        assigned = []
        assigned.extend(self.is_assigned_in(node.body))
        assigned.extend(self.is_assigned_in(node.handlers))
        assigned.extend(self.is_assigned_in(node.orelse))
        assigned.extend(self.is_assigned_in(node.finalbody))
    elif isinstance(node, ast.ExceptHandler):
        assigned = []
        assigned.extend(self.is_assigned_in(node.body))
    elif isinstance(node, (ast.If, ast.For, ast.While)):
        assigned = []
        assigned.extend(self.is_assigned_in(node.body))
        assigned.extend(self.is_assigned_in(node.orelse))
    elif isinstance(node, ast.AugAssign):
        if isinstance(node.target, ast.Name):
            if node.target.id == self.var_name.id:
                assigned = node.value
    elif isinstance(node, ast.Assign) and node.targets:
        target = node.targets[0]
        if isinstance(target, ast.Name):
            if target.id == self.var_name.id:
                assigned = node.value
        elif isinstance(target, ast.Tuple):
            pos = 0
            for name in target.elts:
                if name.id == self.var_name.id:
                    assigned = node.value.elts[pos]
                    break
                pos += 1
    return assigned


# Traceback (AttributeError)
[tester]	ERROR	Bandit internal error running: django_mark_safe on file ./venv/lib/python3.7/site-packages/django/template/base.py at line 738: 'NoneType' object has no attribute 'id'Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/bandit/core/tester.py", line 64, in run_tests
result = test(context)
File "/usr/local/lib/python3.7/site-packages/bandit/plugins/django_xss.py", line 295, in django_mark_safe
return check_risk(context.node)
File "/usr/local/lib/python3.7/site-packages/bandit/plugins/django_xss.py", line 243, in check_risk
secure = evaluate_var(xss_var, parent, node.lineno)
File "/usr/local/lib/python3.7/site-packages/bandit/plugins/django_xss.py", line 123, in evaluate_var
to = analyser.is_assigned(node)
File "/usr/local/lib/python3.7/site-packages/bandit/plugins/django_xss.py", line 89, in is_assigned
assigned.extend(self.is_assigned_in(node.body))
File "/usr/local/lib/python3.7/site-packages/bandit/plugins/django_xss.py", line 33, in is_assigned_in
new_assigned = self.is_assigned(ast_inst)
File "/usr/local/lib/python3.7/site-packages/bandit/plugins/django_xss.py", line 90, in is_assigned
assigned.extend(self.is_assigned_in(node.orelse))
File "/usr/local/lib/python3.7/site-packages/bandit/plugins/django_xss.py", line 33, in is_assigned_in
new_assigned = self.is_assigned(ast_inst)
File "/usr/local/lib/python3.7/site-packages/bandit/plugins/django_xss.py", line 89, in is_assigned
assigned.extend(self.is_assigned_in(node.body))
File "/usr/local/lib/python3.7/site-packages/bandit/plugins/django_xss.py", line 33, in is_assigned_in
new_assigned = self.is_assigned(ast_inst)
File "/usr/local/lib/python3.7/site-packages/bandit/plugins/django_xss.py", line 65, in is_assigned
if withitem.optional_vars.id == self.var_name.id:
AttributeError: 'NoneType' object has no attribute 'id'

# Fixed code
def is_assigned(self, node):
    assigned = False
    if self.ignore_nodes:
        if isinstance(self.ignore_nodes, (list, tuple, object)):
            if isinstance(node, self.ignore_nodes):
                return assigned

    if isinstance(node, ast.Expr):
        assigned = self.is_assigned(node.value)
    elif isinstance(node, ast.FunctionDef):
        for name in node.args.args:
            if isinstance(name, ast.Name):
                if name.id == self.var_name.id:
                    # If is param the assignations are not affected
                    return assigned
        assigned = self.is_assigned_in(node.body)
    elif isinstance(node, ast.With):
        if six.PY2:
            if node.optional_vars.id == self.var_name.id:
                assigned = node
            else:
                assigned = self.is_assigned_in(node.body)
        else:
            for withitem in node.items:
                var_id = getattr(withitem.optional_vars, "id", None)
                if var_id == self.var_name.id:
                    assigned = node
                else:
                    assigned = self.is_assigned_in(node.body)
    elif six.PY2 and isinstance(node, ast.TryFinally):
        assigned = []
        assigned.extend(self.is_assigned_in(node.body))
        assigned.extend(self.is_assigned_in(node.finalbody))
    elif six.PY2 and isinstance(node, ast.TryExcept):
        assigned = []
        assigned.extend(self.is_assigned_in(node.body))
        assigned.extend(self.is_assigned_in(node.handlers))
        assigned.extend(self.is_assigned_in(node.orelse))
    elif not six.PY2 and isinstance(node, ast.Try):
        assigned = []
        assigned.extend(self.is_assigned_in(node.body))
        assigned.extend(self.is_assigned_in(node.handlers))
        assigned.extend(self.is_assigned_in(node.orelse))
        assigned.extend(self.is_assigned_in(node.finalbody))
    elif isinstance(node, ast.ExceptHandler):
        assigned = []
        assigned.extend(self.is_assigned_in(node.body))
    elif isinstance(node, (ast.If, ast.For, ast.While)):
        assigned = []
        assigned.extend(self.is_assigned_in(node.body))
        assigned.extend(self.is_assigned_in(node.orelse))
    elif isinstance(node, ast.AugAssign):
        if isinstance(node.target, ast.Name):
            if node.target.id == self.var_name.id:
                assigned = node.value
    elif isinstance(node, ast.Assign) and node.targets:
        target = node.targets[0]
        if isinstance(target, ast.Name):
            if target.id == self.var_name.id:
                assigned = node.value
        elif isinstance(target, ast.Tuple):
            pos = 0
            for name in target.elts:
                if name.id == self.var_name.id:
                    assigned = node.value.elts[pos]
                    break
                pos += 1
    return assigned