Repository: python/mypy

Issue:
Crash on namedtuples with attribute named `count`
Repro (mypy 0.520):

```python
from typing import NamedTuple

class Crash(NamedTuple):
    count: int
```

No crash if you rename the attribute, or stop subclassing NamedTuple.

Output:
```
test.py:4: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.520
Traceback (most recent call last):
  File "<...>/venv/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "<...>/venv/lib/python3.6/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "<...>/venv/lib/python3.6/site-packages/mypy/main.py", line 50, in main
    res = type_check_only(sources, bin_dir, options)
  File "<...>/venv/lib/python3.6/site-packages/mypy/main.py", line 97, in type_check_only
    options=options)
  File "<...>/venv/lib/python3.6/site-packages/mypy/build.py", line 196, in build
    graph = dispatch(sources, manager)
  File "<...>/venv/lib/python3.6/site-packages/mypy/build.py", line 1769, in dispatch
    process_graph(graph, manager)
  File "<...>/venv/lib/python3.6/site-packages/mypy/build.py", line 2012, in process_graph
    process_stale_scc(graph, scc, manager)
  File "<...>/venv/lib/python3.6/site-packages/mypy/build.py", line 2115, in process_stale_scc
    graph[id].type_check_first_pass()
  File "<...>/venv/lib/python3.6/site-packages/mypy/build.py", line 1684, in type_check_first_pass
    self.type_checker.check_first_pass()
  File "<...>/venv/lib/python3.6/site-packages/mypy/checker.py", line 185, in check_first_pass
    self.accept(d)
  File "<...>/venv/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "<...>/venv/lib/python3.6/site-packages/mypy/nodes.py", line 754, in accept
    return visitor.visit_class_def(self)
  File "<...>/venv/lib/python3.6/site-packages/mypy/checker.py", line 1109, in visit_class_def
    self.accept(defn.defs)
  File "<...>/venv/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "<...>/venv/lib/python3.6/site-packages/mypy/nodes.py", line 815, in accept
    return visitor.visit_block(self)
  File "<...>/venv/lib/python3.6/site-packages/mypy/checker.py", line 1202, in visit_block
    self.accept(s)
  File "<...>/venv/lib/python3.6/site-packages/mypy/checker.py", line 273, in accept
    stmt.accept(self)
  File "<...>/venv/lib/python3.6/site-packages/mypy/nodes.py", line 859, in accept
    return visitor.visit_assignment_stmt(self)
  File "<...>/venv/lib/python3.6/site-packages/mypy/checker.py", line 1209, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "<...>/venv/lib/python3.6/site-packages/mypy/checker.py", line 1241, in check_assignment
    if self.check_compatibility_all_supers(lvalue, lvalue_type, rvalue):
  File "<...>/venv/lib/python3.6/site-packages/mypy/checker.py", line 1331, in check_compatibility_all_supers
    base_type, base_node = self.lvalue_type_from_base(lvalue_node, base)
  File "<...>/venv/lib/python3.6/site-packages/mypy/checker.py", line 1418, in lvalue_type_from_base
    itype = map_instance_to_supertype(instance, base)
  File "<...>/venv/lib/python3.6/site-packages/mypy/maptype.py", line 15, in map_instance_to_supertype
    if instance.type == superclass:
AttributeError: 'TupleType' object has no attribute 'type'
```


Discussion:
The problem is that ``tuple`` has itself a ``count`` attribute, which is a method. It looks like ``builtins.tuple`` gets mixed with ``typing.Tuple`` somewhere in mypy internals. I will take a look now.

Resolving patch:
diff --git a/mypy/checker.py b/mypy/checker.py
--- a/mypy/checker.py
+++ b/mypy/checker.py
@@ -1440,8 +1440,11 @@ def lvalue_type_from_base(self, expr_node: Var,
 
             if base_type:
                 if not has_no_typevars(base_type):
-                    # TODO: Handle TupleType, don't cast
-                    instance = cast(Instance, self.scope.active_self_type())
+                    self_type = self.scope.active_self_type()
+                    if isinstance(self_type, TupleType):
+                        instance = self_type.fallback
+                    else:
+                        instance = self_type
                     itype = map_instance_to_supertype(instance, base)
                     base_type = expand_type_by_instance(base_type, itype)
 


Test patch:
diff --git a/test-data/unit/check-namedtuple.test b/test-data/unit/check-namedtuple.test
--- a/test-data/unit/check-namedtuple.test
+++ b/test-data/unit/check-namedtuple.test
@@ -412,6 +412,12 @@ b = B._make([''])  # type: B
 
 [builtins fixtures/list.pyi]
 
+[case testNamedTupleIncompatibleRedefinition]
+from typing import NamedTuple
+class Crash(NamedTuple):
+    count: int  # E: Incompatible types in assignment (expression has type "int", base class "tuple" defined the type as Callable[[Tuple[Any, ...], Any], int])
+[builtins fixtures/tuple.pyi]
+
 [case testNamedTupleInClassNamespace]
 # https://github.com/python/mypy/pull/2553#issuecomment-266474341
 from typing import NamedTuple