Repository: python/mypy

Issue:
[0.530] AssertionError: Internal error: Unresolved forward reference to UnboundType
```
Traceback (most recent call last):
  File "/Users/daenyth/Curata/cmp/.tox/mypy/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/main.py", line 50, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/main.py", line 103, in type_check_only
    options=options)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 198, in build
    graph = dispatch(sources, manager)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 1841, in dispatch
    process_graph(graph, manager)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 2091, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 2203, in process_stale_scc
    graph[id].write_cache()
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 1822, in write_cache
    self.manager)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 1088, in write_cache
    data = tree.serialize()
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 238, in serialize
    'names': self.names.serialize(self._fullname),
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 2432, in serialize
    data[key] = value.serialize(fullname, key)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 2371, in serialize
    data['node'] = self.node.serialize()
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 2194, in serialize
    'names': self.names.serialize(self.fullname()),
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 2432, in serialize
    data[key] = value.serialize(fullname, key)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 2371, in serialize
    data['node'] = self.node.serialize()
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 533, in serialize
    'type': None if self.type is None else self.type.serialize(),
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 857, in serialize
    'arg_types': [t.serialize() for t in self.arg_types],
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 857, in <listcomp>
    'arg_types': [t.serialize() for t in self.arg_types],
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 496, in serialize
    data['args'] = [arg.serialize() for arg in self.args]
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 496, in <listcomp>
    data['args'] = [arg.serialize() for arg in self.args]
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 994, in serialize
    'items': [t.serialize() for t in self.items],
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 994, in <listcomp>
    'items': [t.serialize() for t in self.items],
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 1410, in serialize
    assert False, "Internal error: Unresolved forward reference to {}".format(name)
AssertionError: Internal error: Unresolved forward reference to UnboundType
```

I get this running on my codebase after updating to 0.530. It passes without error in 0.521. I'm not sure what's triggering it - if you can help me figure that out I can try to minimize the failure.


Discussion:
This is something related to a forward reference. Far example:
```python
x: 'A'
A = List[int]
```
There is a chance (but not 100%) that you have a ``# type: ignore`` on the offending line.

Can you actually share the code? Alternatively I can prepare a "diagnostic" branch and then you could show me the output.
Unfortunately I can't share the code since it's closed source. I'll try to figure out which sub module is causing it and try to find a minimal reproduction.

The codebase is python2, if that helps.
Running with `-v` I get this immediately prior to the traceback:
```
LOG:  Processing the last 247 queued SCCs
LOG:  Processing SCC singleton (cmp.reporting.types) as inherently stale
LOG:  Writing cmp.reporting.types /Users/daenyth/Curata/cmp/cmp/reporting/types.py .mypy_cache/2.7/cmp/reporting/types.meta.json .mypy_cache/2.7/cmp/reporting/types.data.json
LOG:  Build finished in 7.030 seconds with 794 modules, 55 types, and 0 errors
```

I can confirm that running it on that single file fails, I'll try to minimize
It's something a little more complicated than just that.

The file contains a line similar to `Foo = NewType('NotFoo', int)`. When I fix that, mypy does not crash.
When I copy the same file contents to a foo.py not inside the package directory on disk, mypy *also* does not crash.
> The file contains a line similar to ``Foo = NewType('NotFoo', int)``

This is a very good hint. I remember having problems with NewTypes I thought have cleaned-up them all, probably not.
Could you please grep for the places where ``Foo`` was used, it looks like some of this places is not visited in the third pass of semantic analysis.
It looks like it's used in multiple files.

- The only direct use is as part of an alias for `Bar = Tuple[Foo, Blah]`.
- Bar is always used in a typevar position, eg `List[Foo]` or some other generic type
- The files that it's used in have a `mypy.ini` section with `ignore_errors = True`
In particular, does it appear in an overload function signature? I have found a repro, but it is not related to NewTypes, it can happen with any broken forward reference in an overload.
The codebase doesn't have any uses of `overload`
OK, this means that we have at least two crashes :-)
Could you please try the original crash with mypy installed from current master? How does the crash look like? (There are chances it will crash differently.)
```
LOG:  Processing SCC singleton (cmp.reporting.types) as inherently stale with stale deps (__future__ builtins datetime typing)
LOG:  Writing cmp.reporting.types /Users/daenyth/Curata/cmp/cmp/reporting/types.py .mypy_cache/2.7/cmp/reporting/types.meta.json .mypy_cache/2.7/cmp/reporting/types.data.json
LOG:  Build finished in 0.972 seconds with 12 modules, 1757 types, and 0 errors
Traceback (most recent call last):
  File "/Users/daenyth/Curata/cmp/.tox/mypy/bin/mypy", line 11, in <module>
    load_entry_point('mypy===0.540-dev-77ce5e206089e796cf3c563b52791e8a0b4d2d54', 'console_scripts', 'mypy')()
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/main.py", line 50, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/main.py", line 103, in type_check_only
    options=options)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 199, in build
    graph = dispatch(sources, manager)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 1865, in dispatch
    process_graph(graph, manager)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 2115, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 2227, in process_stale_scc
    graph[id].write_cache()
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 1846, in write_cache
    self.manager)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 1090, in write_cache
    data = tree.serialize()
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 238, in serialize
    'names': self.names.serialize(self._fullname),
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 2440, in serialize
    data[key] = value.serialize(fullname, key)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 2379, in serialize
    data['node'] = self.node.serialize()
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 2170, in serialize
    'names': self.names.serialize(self.fullname()),
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 2440, in serialize
    data[key] = value.serialize(fullname, key)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 2379, in serialize
    data['node'] = self.node.serialize()
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 510, in serialize
    'type': None if self.type is None else self.type.serialize(),
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 856, in serialize
    'arg_types': [t.serialize() for t in self.arg_types],
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 856, in <listcomp>
    'arg_types': [t.serialize() for t in self.arg_types],
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 496, in serialize
    data['args'] = [arg.serialize() for arg in self.args]
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 496, in <listcomp>
    data['args'] = [arg.serialize() for arg in self.args]
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 993, in serialize
    'items': [t.serialize() for t in self.items],
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 993, in <listcomp>
    'items': [t.serialize() for t in self.items],
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 1418, in serialize
    assert False, "Internal error: Unresolved forward reference to {}".format(name)
AssertionError: Internal error: Unresolved forward reference to Foo
```
I tried to play with this for some time, but still can't get a repro. It is quite mysterious, since the code provoking the crash looks very simple, from the traceback I see that there is a method (most likely in a module level class) that has one of the argument types of the form ``OneClass[Tuple[Foo, OtherClass]]`` where ``Foo`` is the broken ``NewType`` definition.

@Daenyth maybe you could find one of such methods, and isolate the related code, i.e. keep the import structure between files, but only leave the necessary definitions in the corresponding files, i.e. something like this:
```python
# file a.py
from c import C
Foo = NewType('NotFoo', int)

# file b.py
from a import Foo
Bar = Tuple[Foo, int]

# file c.py
from b import Bar
class C:
    def meth(self, x):
        # type: (List[Bar]) -> None
        pass
```
And then see if this causes the crash? This will be very helpful.
OK, I was able to make a reproduction into a single file. The stack trace I get is not exactly the same as running on the real code, but it's *a* crash, and a similar message.

```python
# This is python2
from __future__ import absolute_import, division, print_function, unicode_literals
from typing import Any, Dict, List, NewType

Foo = NewType('NotFoo', int)
Foos = NewType('Foos', List[Foo])

def frob_the_widget(foos):
    # type: (Dict[Any, Foos]) -> None
    f = foos.get(1)
    reveal_type(f)
    list(f)

```

Removing the `list()` invocation makes it no longer crash.

```
# mypy==0.530
bar.py:4: error: String argument 1 'NotFoo' to NewType(...) does not match variable name 'Foo'
bar.py:5: error: Invalid type "bar.Foo"
bar.py:10: error: Revealed type is 'Union[bar.Foos*, builtins.None]'
bar.py:11: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.530
Traceback (most recent call last):
  File "/Users/daenyth/Curata/cmp/.tox/mypy/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/main.py", line 50, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/main.py", line 103, in type_check_only
    options=options)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 198, in build
    graph = dispatch(sources, manager)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 1841, in dispatch
    process_graph(graph, manager)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 2091, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 2194, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/build.py", line 1753, in type_check_first_pass
    self.type_checker.check_first_pass()
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checker.py", line 194, in check_first_pass
    self.accept(d)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checker.py", line 282, in accept
    stmt.accept(self)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 519, in accept
    return visitor.visit_func_def(self)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checker.py", line 530, in visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checker.py", line 590, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checker.py", line 750, in check_func_def
    self.accept(item.body)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checker.py", line 282, in accept
    stmt.accept(self)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 773, in accept
    return visitor.visit_block(self)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checker.py", line 1304, in visit_block
    self.accept(s)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checker.py", line 282, in accept
    stmt.accept(self)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/nodes.py", line 787, in accept
    return visitor.visit_expression_stmt(self)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checker.py", line 2001, in visit_expression_stmt
    self.expr_checker.accept(s.expr, allow_none_return=True, always_allow_any=True)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checkexpr.py", line 2368, in accept
    typ = self.visit_call_expr(node, allow_none_return=True)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checkexpr.py", line 266, in visit_call_expr
    ret_type = self.check_call_expr_with_callee_type(callee_type, e, fullname, object_type)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checkexpr.py", line 503, in check_call_expr_with_callee_type
    object_type=object_type)[0]
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checkexpr.py", line 608, in check_call
    object_type=object_type)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checkexpr.py", line 565, in check_call
    callee, args, arg_kinds, formal_to_actual, context)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/checkexpr.py", line 813, in infer_function_type_arguments
    strict=self.chk.in_checked_function())
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/infer.py", line 31, in infer_function_type_arguments
    callee_type, arg_types, arg_kinds, formal_to_actual)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/constraints.py", line 63, in infer_constraints_for_callable
    SUPERTYPE_OF)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/constraints.py", line 147, in infer_constraints
    res.extend(infer_constraints(template, a_item, direction))
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/constraints.py", line 174, in infer_constraints
    return template.accept(ConstraintBuilderVisitor(actual, direction))
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 476, in accept
    return visitor.visit_instance(self)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/constraints.py", line 332, in visit_instance
    mapped = map_instance_to_supertype(instance, template.type)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/maptype.py", line 23, in map_instance_to_supertype
    return map_instance_to_supertypes(instance, superclass)[0]
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/maptype.py", line 36, in map_instance_to_supertypes
    a.extend(map_instance_to_direct_supertypes(t, sup))
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/maptype.py", line 81, in map_instance_to_direct_supertypes
    t = expand_type(b, env)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/expandtype.py", line 16, in expand_type
    return typ.accept(ExpandTypeVisitor(env))
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 476, in accept
    return visitor.visit_instance(self)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/expandtype.py", line 83, in visit_instance
    args = self.expand_types(t.args)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/expandtype.py", line 132, in expand_types
    a.append(t.accept(self))
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 1399, in accept
    return visitor.visit_forwardref_type(self)
  File "/Users/daenyth/Curata/cmp/.tox/mypy/lib/python3.6/site-packages/mypy/types.py", line 1489, in visit_forwardref_type
    raise RuntimeError('Internal error: unresolved forward reference')
RuntimeError: Internal error: unresolved forward reference
bar.py:11: : note: use --pdb to drop into pdb
```
Thank you! I will make a fix tomorrow and you can then test if it fixes the original crash (with all files).
At least I can reproduce the original traceback with slightly modified repro.

Resolving patch:
diff --git a/mypy/nodes.py b/mypy/nodes.py
--- a/mypy/nodes.py
+++ b/mypy/nodes.py
@@ -1793,6 +1793,7 @@ class NewTypeExpr(Expression):
     def __init__(self, name: str, old_type: 'Optional[mypy.types.Type]', line: int) -> None:
         self.name = name
         self.old_type = old_type
+        self.line = line
 
     def accept(self, visitor: ExpressionVisitor[T]) -> T:
         return visitor.visit_newtype_expr(self)
diff --git a/mypy/semanal_pass3.py b/mypy/semanal_pass3.py
--- a/mypy/semanal_pass3.py
+++ b/mypy/semanal_pass3.py
@@ -17,7 +17,7 @@
     Node, Expression, MypyFile, FuncDef, FuncItem, Decorator, RefExpr, Context, TypeInfo, ClassDef,
     Block, TypedDictExpr, NamedTupleExpr, AssignmentStmt, IndexExpr, TypeAliasExpr, NameExpr,
     CallExpr, NewTypeExpr, ForStmt, WithStmt, CastExpr, TypeVarExpr, TypeApplication, Lvalue,
-    TupleExpr, RevealTypeExpr, SymbolTableNode, Var, ARG_POS
+    TupleExpr, RevealTypeExpr, SymbolTableNode, Var, ARG_POS, OverloadedFuncDef
 )
 from mypy.types import (
     Type, Instance, AnyType, TypeOfAny, CallableType, TupleType, TypeVarType, TypedDictType,
@@ -85,6 +85,10 @@ def visit_func_def(self, fdef: FuncDef) -> None:
         super().visit_func_def(fdef)
         self.errors.pop_function()
 
+    def visit_overloaded_func_def(self, fdef: OverloadedFuncDef) -> None:
+        self.analyze(fdef.type, fdef)
+        super().visit_overloaded_func_def(fdef)
+
     def visit_class_def(self, tdef: ClassDef) -> None:
         # NamedTuple base classes are validated in check_namedtuple_classdef; we don't have to
         # check them again here.
@@ -116,12 +120,7 @@ def visit_class_def(self, tdef: ClassDef) -> None:
                 self.analyze(tdef.analyzed.info.typeddict_type, tdef.analyzed, warn=True)
             elif isinstance(tdef.analyzed, NamedTupleExpr):
                 self.analyze(tdef.analyzed.info.tuple_type, tdef.analyzed, warn=True)
-                for name in tdef.analyzed.info.names:
-                    sym = tdef.analyzed.info.names[name]
-                    if isinstance(sym.node, (FuncDef, Decorator)):
-                        self.accept(sym.node)
-                    if isinstance(sym.node, Var):
-                        self.analyze(sym.node.type, sym.node)
+                self.analyze_info(tdef.analyzed.info)
         super().visit_class_def(tdef)
 
     def visit_decorator(self, dec: Decorator) -> None:
@@ -189,6 +188,10 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
             analyzed = s.rvalue.analyzed
             if isinstance(analyzed, NewTypeExpr):
                 self.analyze(analyzed.old_type, analyzed)
+                if analyzed.info:
+                    # Currently NewTypes only have __init__, but to be future proof,
+                    # we analyze all symbols.
+                    self.analyze_info(analyzed.info)
                 if analyzed.info and analyzed.info.mro:
                     analyzed.info.mro = []  # Force recomputation
                     mypy.semanal.calculate_class_mro(analyzed.info.defn, self.fail_blocker)
@@ -203,12 +206,7 @@ def visit_assignment_stmt(self, s: AssignmentStmt) -> None:
                 self.analyze(analyzed.info.typeddict_type, analyzed, warn=True)
             if isinstance(analyzed, NamedTupleExpr):
                 self.analyze(analyzed.info.tuple_type, analyzed, warn=True)
-                for name in analyzed.info.names:
-                    sym = analyzed.info.names[name]
-                    if isinstance(sym.node, (FuncDef, Decorator)):
-                        self.accept(sym.node)
-                    if isinstance(sym.node, Var):
-                        self.analyze(sym.node.type, sym.node)
+                self.analyze_info(analyzed.info)
         # We need to pay additional attention to assignments that define a type alias.
         # The resulting type is also stored in the 'type_override' attribute of
         # the corresponding SymbolTableNode.
@@ -255,12 +253,27 @@ def perform_transform(self, node: Union[Node, SymbolTableNode],
             for n in node.target:
                 if isinstance(n, NameExpr) and isinstance(n.node, Var) and n.node.type:
                     n.node.type = transform(n.node.type)
-        if isinstance(node, (FuncDef, CastExpr, AssignmentStmt, TypeAliasExpr, Var)):
+        if isinstance(node, (FuncDef, OverloadedFuncDef, CastExpr, AssignmentStmt,
+                             TypeAliasExpr, Var)):
             assert node.type, "Scheduled patch for non-existent type"
             node.type = transform(node.type)
         if isinstance(node, NewTypeExpr):
             assert node.old_type, "Scheduled patch for non-existent type"
             node.old_type = transform(node.old_type)
+            if node.info:
+                new_bases = []  # type: List[Instance]
+                for b in node.info.bases:
+                    new_b = transform(b)
+                    # TODO: this code can be combined with code in second pass.
+                    if isinstance(new_b, Instance):
+                        new_bases.append(new_b)
+                    elif isinstance(new_b, TupleType):
+                        new_bases.append(new_b.fallback)
+                    else:
+                        self.fail("Argument 2 to NewType(...) must be subclassable"
+                                  " (got {})".format(new_b), node)
+                        new_bases.append(self.builtin_type('object'))
+                node.info.bases = new_bases
         if isinstance(node, TypeVarExpr):
             if node.upper_bound:
                 node.upper_bound = transform(node.upper_bound)
@@ -292,7 +305,7 @@ def perform_transform(self, node: Union[Node, SymbolTableNode],
                     new_bases.append(new_base)
                 else:
                     # Don't fix the NamedTuple bases, they are Instance's intentionally.
-                    # Patch the 'args' just in case, although generic tuple type are
+                    # Patch the 'args' just in case, although generic tuple types are
                     # not supported yet.
                     alt_base = Instance(base.type, [transform(a) for a in base.args])
                     new_bases.append(alt_base)
@@ -339,6 +352,15 @@ def patch() -> None:
                                                                   node, warn=False)))
             self.patches.append(patch)
 
+    def analyze_info(self, info: TypeInfo) -> None:
+        # Similar to above but for nodes with synthetic TypeInfos (NamedTuple and NewType).
+        for name in info.names:
+            sym = info.names[name]
+            if isinstance(sym.node, (FuncDef, Decorator)):
+                self.accept(sym.node)
+            if isinstance(sym.node, Var):
+                self.analyze(sym.node.type, sym.node)
+
     def make_type_analyzer(self, indicator: Dict[str, bool]) -> TypeAnalyserPass3:
         return TypeAnalyserPass3(self.sem.lookup_qualified,
                                  self.sem.lookup_fully_qualified,
diff --git a/mypy/typeanal.py b/mypy/typeanal.py
--- a/mypy/typeanal.py
+++ b/mypy/typeanal.py
@@ -14,7 +14,7 @@
     Type, UnboundType, TypeVarType, TupleType, TypedDictType, UnionType, Instance, AnyType,
     CallableType, NoneTyp, DeletedType, TypeList, TypeVarDef, TypeVisitor, SyntheticTypeVisitor,
     StarType, PartialType, EllipsisType, UninhabitedType, TypeType, get_typ_args, set_typ_args,
-    CallableArgument, get_type_vars, TypeQuery, union_items, TypeOfAny, ForwardRef
+    CallableArgument, get_type_vars, TypeQuery, union_items, TypeOfAny, ForwardRef, Overloaded
 )
 
 from mypy.nodes import (
@@ -765,6 +765,10 @@ def visit_callable_type(self, t: CallableType) -> None:
         for arg_type in t.arg_types:
             arg_type.accept(self)
 
+    def visit_overloaded(self, t: Overloaded) -> None:
+        for item in t.items():
+            item.accept(self)
+
     def visit_tuple_type(self, t: TupleType) -> None:
         for item in t.items:
             item.accept(self)


Test patch:
diff --git a/test-data/unit/check-classes.test b/test-data/unit/check-classes.test
--- a/test-data/unit/check-classes.test
+++ b/test-data/unit/check-classes.test
@@ -3623,6 +3623,55 @@ def parse_ast(name_dict: NameDict) -> None:
 [builtins fixtures/isinstancelist.pyi]
 [out]
 
+[case testNoCrashForwardRefToBrokenDoubleNewType]
+from typing import Any, Dict, List, NewType
+
+Foo = NewType('NotFoo', int) # E: String argument 1 'NotFoo' to NewType(...) does not match variable name 'Foo'
+Foos = NewType('Foos', List[Foo]) # type: ignore
+
+def frob(foos: Dict[Any, Foos]) -> None:
+    foo = foos.get(1)
+    dict(foo)
+[builtins fixtures/dict.pyi]
+[out]
+
+[case testNoCrashForwardRefToBrokenDoubleNewTypeClass]
+from typing import Any, Dict, List, NewType
+
+Foo = NewType('NotFoo', int) # type: ignore
+Foos = NewType('Foos', List[Foo]) # type: ignore
+
+x: C
+class C:
+    def frob(self, foos: Dict[Any, Foos]) -> None:
+        foo = foos.get(1)
+        dict(foo)
+
+reveal_type(x.frob) # E: Revealed type is 'def (foos: builtins.dict[Any, __main__.Foos])'
+[builtins fixtures/dict.pyi]
+[out]
+
+[case testNewTypeFromForwardNamedTuple]
+from typing import NewType, NamedTuple, Tuple
+
+NT = NewType('NT', N)
+class N(NamedTuple):
+    x: int
+
+x: NT = N(1) # E: Incompatible types in assignment (expression has type "N", variable has type "NT")
+x = NT(N(1))
+[out]
+
+[case testNewTypeFromForwardTypedDict]
+from typing import NewType, Tuple
+from mypy_extensions import TypedDict
+
+NT = NewType('NT', N) # E: Argument 2 to NewType(...) must be subclassable (got TypedDict('__main__.N', {'x': builtins.int}))
+class N(TypedDict):
+    x: int
+[builtins fixtures/dict.pyi]
+[out]
+
 [case testCorrectAttributeInForwardRefToNamedTuple]
 from typing import NamedTuple
 proc: Process
diff --git a/test-data/unit/check-incremental.test b/test-data/unit/check-incremental.test
--- a/test-data/unit/check-incremental.test
+++ b/test-data/unit/check-incremental.test
@@ -3046,6 +3046,71 @@ class Pair(NamedTuple):
 Person(name=Pair(first="John", last="Doe"))
 [out]
 
+[case testNoCrashForwardRefToBrokenDoubleNewTypeIncremental]
+from typing import Any, List, NewType
+
+Foo = NewType('NotFoo', int) # type: ignore
+Foos = NewType('Foos', List[Foo]) # type: ignore
+
+def frob(foos: List[Foos]) -> None:
+    pass
+[builtins fixtures/list.pyi]
+[out]
+
+[case testNoCrashForwardRefOverloadIncremental]
+from typing import overload, List
+
+@overload
+def f(x: int) -> int: ...
+@overload
+def f(x: F) -> F: ...
+def f(x):
+    pass
+
+F = List[int]
+[builtins fixtures/list.pyi]
+[out]
+
+[case testNoCrashForwardRefOverloadIncrementalClass]
+from typing import overload, Tuple, NamedTuple
+
+x: C
+class C:
+    @overload
+    def f(self, x: str) -> N: pass
+    @overload
+    def f(self, x: int) -> int: pass
+    def f(self, x):
+        pass
+
+class N(NamedTuple):
+    x: A
+A = Tuple[int]
+[builtins fixtures/tuple.pyi]
+[out]
+
+[case testNewTypeFromForwardNamedTupleIncremental]
+from typing import NewType, NamedTuple, Tuple
+
+NT = NewType('NT', N)
+class N(NamedTuple):
+    x: int
+
+x: NT = N(1) # type: ignore
+x = NT(N(1))
+[out]
+
+[case testNewTypeFromForwardTypedDictIncremental]
+from typing import NewType, Tuple, Dict
+from mypy_extensions import TypedDict
+
+NT = NewType('NT', N) # type: ignore
+class N(TypedDict):
+    x: A
+A = Dict[str, int]
+[builtins fixtures/dict.pyi]
+[out]
+
 -- Some crazy selef-referential named tuples, types dicts, and aliases
 -- to be sure that everything can be _serialized_ (i.e. ForwardRef's are removed).
 -- For this reason errors are silenced (tests with # type: ignore have equivalents in other files)