Repository: python/mypy

Issue:
Crash with IndexError when using TypeVar(..., Sequence, ...)
The following test case crashes on mypy 0.4.6 and up:

```python
from typing import Sequence, TypeVar

S = TypeVar('S', Sequence)


def my_len(s: S) -> int:
    return len(s)


def crash() -> None:
    my_len((0,))
```

Here's the traceback produced with mypy 0.4.6 installed from pypi:

```pytb
$ mypy --show-traceback repro.py
repro.py:11: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues
Traceback (most recent call last):
  File "/home/adrake/ve/app/bin/mypy", line 6, in <module>
    main(__file__)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/main.py", line 41, in main
    res = type_check_only(sources, bin_dir, options)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/main.py", line 86, in type_check_only
    options=options)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/build.py", line 183, in build
    dispatch(sources, manager)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/build.py", line 1512, in dispatch
    process_graph(graph, manager)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/build.py", line 1692, in process_graph
    process_stale_scc(graph, scc)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/build.py", line 1771, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/build.py", line 1440, in type_check_first_pass
    self.type_checker.check_first_pass()
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checker.py", line 174, in check_first_pass
    self.accept(d)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checker.py", line 243, in accept
    typ = node.accept(self)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/nodes.py", line 532, in accept
    return visitor.visit_func_def(self)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checker.py", line 455, in visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checker.py", line 518, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checker.py", line 648, in check_func_def
    self.accept(item.body)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checker.py", line 243, in accept
    typ = node.accept(self)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/nodes.py", line 771, in accept
    return visitor.visit_block(self)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checker.py", line 1087, in visit_block
    self.accept(s)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checker.py", line 243, in accept
    typ = node.accept(self)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/nodes.py", line 785, in accept
    return visitor.visit_expression_stmt(self)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checker.py", line 1522, in visit_expression_stmt
    self.accept(s.expr)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checker.py", line 243, in accept
    typ = node.accept(self)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/nodes.py", line 1247, in accept
    return visitor.visit_call_expr(self)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checker.py", line 1968, in visit_call_expr
    return self.expr_checker.visit_call_expr(e)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checkexpr.py", line 179, in visit_call_expr
    return self.check_call_expr_with_callee_type(callee_type, e)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checkexpr.py", line 239, in check_call_expr_with_callee_type
    e.arg_names, callable_node=e.callee)[0]
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checkexpr.py", line 280, in check_call
    callee, args, arg_kinds, formal_to_actual, context)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checkexpr.py", line 539, in infer_function_type_arguments
    context)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checkexpr.py", line 612, in apply_inferred_arguments
    return self.apply_generic_arguments(callee_type, inferred_args, context)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/checkexpr.py", line 884, in apply_generic_arguments
    return applytype.apply_generic_arguments(callable, types, self.msg, context)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/applytype.py", line 37, in apply_generic_arguments
    if mypy.subtypes.is_subtype(type, value):
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/subtypes.py", line 51, in is_subtype
    return left.accept(SubtypeVisitor(right, type_parameter_checker))
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/types.py", line 785, in accept
    return visitor.visit_tuple_type(self)
  File "/home/adrake/ve/app/lib/python3.5/site-packages/mypy/subtypes.py", line 174, in visit_tuple_type
    iter_type = right.args[0]
IndexError: list index out of range
```

This crash also reproduces on master. While I haven't had a chance to dig into it much, I did observe that `Sequence[Any]` does not crash, so I suspect it's a quick fix.


Discussion:
This feels awfully familiar but I can't find a duplicate issue.

Note that the demo code is wrong -- you should use
```python
S = TypeVar('S', bound=Sequence)
```

(But of course it shouldn't crash!)
Yeah -- the original code pre-testcase-reduction was `S = TypeVar('S', Sequence, Dict[str, str])` or something more complicated (which still crashes).
Oh wait. I was distracted by the broken `TypeVar`. The crash is on the `my_len((0,))` call. I have a fix, will send the PR soon.
Ah, sorry. I was so caught up with the typechecker, I didn't even check to see whether the testcase ran! Never thought I'd have that problem in this language ;)

Resolving patch:
diff --git a/mypy/subtypes.py b/mypy/subtypes.py
--- a/mypy/subtypes.py
+++ b/mypy/subtypes.py
@@ -186,7 +186,10 @@ def visit_tuple_type(self, left: TupleType) -> bool:
                   is_named_instance(right, 'typing.Container') or
                   is_named_instance(right, 'typing.Sequence') or
                   is_named_instance(right, 'typing.Reversible')):
-                iter_type = right.args[0]
+                if right.args:
+                    iter_type = right.args[0]
+                else:
+                    iter_type = AnyType()
                 return all(is_subtype(li, iter_type) for li in left.items)
             elif is_subtype(left.fallback, right, self.check_type_parameter):
                 return True


Test patch:
diff --git a/test-data/unit/check-typevar-values.test b/test-data/unit/check-typevar-values.test
--- a/test-data/unit/check-typevar-values.test
+++ b/test-data/unit/check-typevar-values.test
@@ -497,3 +497,9 @@ class C:
     def f(self, x: T) -> T:
         A = C.T
         return x
+
+[case testParameterLessGenericAsRestriction]
+from typing import Sequence, Iterable, TypeVar
+S = TypeVar('S', Sequence, Iterable)
+def my_len(s: S) -> None: pass
+def crash() -> None: my_len((0,))