Repository: python/mypy

Issue:
Exception in partial_type.type
I got this error parsing a large file:

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.5/bin/mypy", line 6, in <module>
    main(**file**)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/main.py", line 55, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/main.py", line 103, in type_check_only
    python_path=options.python_path)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/build.py", line 217, in build
    dispatch(sources, manager)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/build.py", line 1353, in dispatch
    process_graph(graph, manager)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/build.py", line 1486, in process_graph
    process_stale_scc(graph, scc)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/build.py", line 1516, in process_stale_scc
    graph[id].type_check()
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/build.py", line 1333, in type_check
    manager.type_checker.visit_file(self.tree, self.xpath)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 430, in visit_file
    self.accept(d)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 479, in accept
    typ = node.accept(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/nodes.py", line 650, in accept
    return visitor.visit_class_def(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 1069, in visit_class_def
    self.accept(defn.defs)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 479, in accept
    typ = node.accept(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/nodes.py", line 713, in accept
    return visitor.visit_block(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 1164, in visit_block
    self.accept(s)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 479, in accept
    typ = node.accept(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/nodes.py", line 534, in accept
    return visitor.visit_decorator(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 1973, in visit_decorator
    e.func.accept(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/nodes.py", line 465, in accept
    return visitor.visit_func_def(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 592, in visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 650, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 759, in check_func_def
    self.accept_in_frame(item.body)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 494, in accept_in_frame
    answer = self.accept(node, type_context)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 479, in accept
    typ = node.accept(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/nodes.py", line 713, in accept
    return visitor.visit_block(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 1164, in visit_block
    self.accept(s)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 479, in accept
    typ = node.accept(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/nodes.py", line 727, in accept
    return visitor.visit_expression_stmt(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 1603, in visit_expression_stmt
    self.accept(s.expr)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 479, in accept
    typ = node.accept(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/nodes.py", line 1183, in accept
    return visitor.visit_call_expr(self)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checker.py", line 2035, in visit_call_expr
    return self.expr_checker.visit_call_expr(e)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checkexpr.py", line 136, in visit_call_expr
    self.try_infer_partial_type(e)
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/mypy/checkexpr.py", line 160, in try_infer_partial_type
    if partial_type.type is None:
AttributeError: 'NoneType' object has no attribute 'type'

The referenced line of code was the second one here:

results.sort(cmp=lambda x, y: cmp(x[0], y[0]))
results = [x[1] for x in results]



Discussion:
This is using the following args on a single file: --check-untyped-defs --silent-imports --py2


Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -157,7 +157,7 @@ def try_infer_partial_type(self, e: CallExpr) -> None:
             partial_types = self.chk.find_partial_types(var)
             if partial_types is not None and not self.chk.current_node_deferred:
                 partial_type = cast(PartialType, var.type)
-                if partial_type.type is None:
+                if partial_type is None or partial_type.type is None:
                     # A partial None type -> can't infer anything.
                     return
                 typename = partial_type.type.fullname()


Test patch: