# Buggy code
def handle_cannot_determine_type(self, name: str, context: Context) -> None:
    node = self.scope.top_function()
    if self.pass_num < LAST_PASS and isinstance(node, (FuncDef, LambdaExpr)):
        # Don't report an error yet. Just defer.
        if self.errors.type_name:
            type_name = self.errors.type_name[-1]
        else:
            type_name = None
        # Shouldn't we freeze the entire scope?
        enclosing_class = self.scope.enclosing_class()
        self.deferred_nodes.append(DeferredNode(node, type_name, enclosing_class))
        # Set a marker so that we won't infer additional types in this
        # function. Any inferred types could be bogus, because there's at
        # least one type that we don't know.
        self.current_node_deferred = True
    else:
        self.msg.cannot_determine_type(name, context)


# Traceback (AssertionError)
~/s/px (python|●2✚1) $ mypy --show-traceback --check-untyped-defs --ignore-missing-imports *.py */*.py
px/px_terminal.py:23: error: Incompatible types in assignment (expression has type "int", variable has type "str")
px/px_terminal.py:24: error: Unsupported operand types for > ("int" and "str")
px/px_terminal.py:29: error: Incompatible types in assignment (expression has type "int", variable has type "str")
px/px_terminal.py:30: error: Unsupported operand types for > ("int" and "str")
px/px_ipc_map.py:45: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.511
Traceback (most recent call last):
File "/usr/local/bin/mypy", line 6, in <module>
main(__file__)
File "/usr/local/lib/python3.6/site-packages/mypy/main.py", line 46, in main
res = type_check_only(sources, bin_dir, options)
File "/usr/local/lib/python3.6/site-packages/mypy/main.py", line 93, in type_check_only
options=options)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 188, in build
graph = dispatch(sources, manager)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 1595, in dispatch
process_graph(graph, manager)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 1838, in process_graph
process_stale_scc(graph, scc, manager)
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 1942, in process_stale_scc
if graph[id].type_check_second_pass():
File "/usr/local/lib/python3.6/site-packages/mypy/build.py", line 1516, in type_check_second_pass
return self.type_checker.check_second_pass()
File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 219, in check_second_pass
self.check_partial(node)
File "/usr/local/lib/python3.6/site-packages/mypy/checker.py", line 226, in check_partial
self.expr_checker.accept(node)
File "/usr/local/lib/python3.6/site-packages/mypy/checkexpr.py", line 2058, in accept
typ = node.accept(self)
File "/usr/local/lib/python3.6/site-packages/mypy/nodes.py", line 1548, in accept
return visitor.visit_lambda_expr(self)
File "/usr/local/lib/python3.6/site-packages/mypy/checkexpr.py", line 1792, in visit_lambda_expr
ret_type = self.accept(e.expr(), allow_none_return=True)
File "/usr/local/lib/python3.6/site-packages/mypy/checkexpr.py", line 2058, in accept
typ = node.accept(self)
File "/usr/local/lib/python3.6/site-packages/mypy/nodes.py", line 1451, in accept
return visitor.visit_op_expr(self)
File "/usr/local/lib/python3.6/site-packages/mypy/checkexpr.py", line 1137, in visit_op_expr
return self.check_boolean_op(e, e)
File "/usr/local/lib/python3.6/site-packages/mypy/checkexpr.py", line 1370, in check_boolean_op
right_type = self.analyze_cond_branch(right_map, e.right, left_type)
File "/usr/local/lib/python3.6/site-packages/mypy/checkexpr.py", line 2025, in analyze_cond_branch
with self.chk.binder.frame_context(can_skip=True, fall_through=0):
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/contextlib.py", line 82, in __enter__
return next(self.gen)
File "/usr/local/lib/python3.6/site-packages/mypy/binder.py", line 312, in frame_context
assert len(self.frames) > 1
AssertionError:
px/px_ipc_map.py:45: note: use --pdb to drop into pdb
~/s/px (python|●2✚1) $

# Fixed code
def handle_cannot_determine_type(self, name: str, context: Context) -> None:
    node = self.scope.top_non_lambda_function()
    if self.pass_num < LAST_PASS and isinstance(node, FuncDef):
        # Don't report an error yet. Just defer. Note that we don't defer
        # lambdas because they are coupled to the surrounding function
        # through the binder and the inferred type of the lambda, so it
        # would get messy.
        if self.errors.type_name:
            type_name = self.errors.type_name[-1]
        else:
            type_name = None
        # Shouldn't we freeze the entire scope?
        enclosing_class = self.scope.enclosing_class()
        self.deferred_nodes.append(DeferredNode(node, type_name, enclosing_class))
        # Set a marker so that we won't infer additional types in this
        # function. Any inferred types could be bogus, because there's at
        # least one type that we don't know.
        self.current_node_deferred = True
    else:
        self.msg.cannot_determine_type(name, context)