# Buggy code
def _check_late_binding_closure(self, node, assignment_node):
    if not self.linter.is_message_enabled("cell-var-from-loop"):
        return

    def _is_direct_lambda_call():
        return (
            isinstance(node_scope.parent, astroid.Call)
            and node_scope.parent.func is node_scope
        )

    node_scope = node.scope()
    if not isinstance(node_scope, (astroid.Lambda, astroid.FunctionDef)):
        return
    if isinstance(node.parent, astroid.Arguments):
        return

    if isinstance(assignment_node, astroid.Comprehension):
        if assignment_node.parent.parent_of(node.scope()):
            self.add_message("cell-var-from-loop", node=node, args=node.name)
    else:
        assign_scope = assignment_node.scope()
        maybe_for = assignment_node
        while not isinstance(maybe_for, astroid.For):
            if maybe_for is assign_scope:
                break
            maybe_for = maybe_for.parent
        else:
            if (
                maybe_for.parent_of(node_scope)
                and not _is_direct_lambda_call()
                and not isinstance(node_scope.statement(), astroid.Return)
            ):
                self.add_message("cell-var-from-loop", node=node, args=node.name)


# Traceback (AttributeError)
Traceback (most recent call last):
File "/Users/user/.local/share/virtualenvs/my_project/bin/pylint", line 8, in <module>
sys.exit(run_pylint())
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/__init__.py", line 22, in run_pylint
PylintRun(sys.argv[1:])
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/lint/run.py", line 344, in __init__
linter.check(args)
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/lint/pylinter.py", line 871, in check
self.get_ast, self._iterate_file_descrs(files_or_modules)
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/lint/pylinter.py", line 904, in _check_files
self._check_file(get_ast, check_astroid_module, name, filepath, modname)
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/lint/pylinter.py", line 930, in _check_file
check_astroid_module(ast_node)
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/lint/pylinter.py", line 1063, in check_astroid_module
ast_node, walker, rawcheckers, tokencheckers
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/lint/pylinter.py", line 1107, in _check_astroid_module
walker.walk(ast_node)
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/utils/ast_walker.py", line 75, in walk
self.walk(child)
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/utils/ast_walker.py", line 75, in walk
self.walk(child)
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/utils/ast_walker.py", line 75, in walk
self.walk(child)
[Previous line repeated 1 more times]
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/utils/ast_walker.py", line 72, in walk
callback(astroid)
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/checkers/variables.py", line 1019, in visit_name
self._check_late_binding_closure(node, defnode)
File "/Users/user/.local/share/virtualenvs/my_project/lib/python3.6/site-packages/pylint/checkers/variables.py", line 1722, in _check_late_binding_closure
maybe_for = maybe_for.parent
AttributeError: 'NoneType' object has no attribute 'parent'

# Fixed code
def _check_late_binding_closure(self, node, assignment_node):
    if not self.linter.is_message_enabled("cell-var-from-loop"):
        return

    def _is_direct_lambda_call():
        return (
            isinstance(node_scope.parent, astroid.Call)
            and node_scope.parent.func is node_scope
        )

    node_scope = node.scope()
    if not isinstance(node_scope, (astroid.Lambda, astroid.FunctionDef)):
        return
    if isinstance(node.parent, astroid.Arguments):
        return

    if isinstance(assignment_node, astroid.Comprehension):
        if assignment_node.parent.parent_of(node.scope()):
            self.add_message("cell-var-from-loop", node=node, args=node.name)
    else:
        assign_scope = assignment_node.scope()
        maybe_for = assignment_node
        while maybe_for and not isinstance(maybe_for, astroid.For):
            if maybe_for is assign_scope:
                break
            maybe_for = maybe_for.parent
        else:
            if (
                maybe_for
                and maybe_for.parent_of(node_scope)
                and not _is_direct_lambda_call()
                and not isinstance(node_scope.statement(), astroid.Return)
            ):
                self.add_message("cell-var-from-loop", node=node, args=node.name)