Repository: python/mypy

Issue:
Crash in super() outside a method
The simplest repro:
```python
class C:
    a = super().whatever
```
Crash also happens for working Python code with single argument ``super()`` outside methods:
```
Traceback (most recent call last):
  File "/usr/local/bin/mypy", line 6, in <module>
    main(__file__)
  File "/usr/local/lib/python3.4/dist-packages/mypy/main.py", line 42, in main
    res = type_check_only(sources, bin_dir, options)
  File "/usr/local/lib/python3.4/dist-packages/mypy/main.py", line 86, in type_check_only
    options=options)
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 183, in build
    dispatch(sources, manager)
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 1531, in dispatch
    process_graph(graph, manager)
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 1768, in process_graph
    process_stale_scc(graph, scc)
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 1847, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/usr/local/lib/python3.4/dist-packages/mypy/build.py", line 1453, in type_check_first_pass
    self.type_checker.check_first_pass()
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 167, in check_first_pass
    self.accept(d)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 233, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/nodes.py", line 717, in accept
    return visitor.visit_class_def(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 964, in visit_class_def
    self.accept(defn.defs)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 233, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/nodes.py", line 778, in accept
    return visitor.visit_block(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 1057, in visit_block
    self.accept(s)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 233, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/nodes.py", line 819, in accept
    return visitor.visit_assignment_stmt(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 1064, in visit_assignment_stmt
    self.check_assignment(s.lvalues[-1], s.rvalue, s.type is None, s.new_syntax)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checker.py", line 1140, in check_assignment
    self.infer_variable_type(inferred, lvalue, self.expr_checker.accept(rvalue),
  File "/usr/local/lib/python3.4/dist-packages/mypy/checkexpr.py", line 2015, in accept
    typ = node.accept(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/nodes.py", line 1492, in accept
    return visitor.visit_super_expr(self)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checkexpr.py", line 1835, in visit_super_expr
    t = self.analyze_super(e, False)
  File "/usr/local/lib/python3.4/dist-packages/mypy/checkexpr.py", line 1854, in analyze_super
    args = self.chk.scope.top_function().arguments
AttributeError: 'NoneType' object has no attribute 'arguments'
```


Discussion:
@JukkaL I have a fix for this but it depends on PR https://github.com/python/mypy/pull/2847
Ok -- I'll see if I can get #2847 merged today, before the release. This issue would be nice to fix as well.

Resolving patch:
diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py
--- a/mypy/checkexpr.py
+++ b/mypy/checkexpr.py
@@ -1851,6 +1851,9 @@ def analyze_super(self, e: SuperExpr, is_lvalue: bool) -> Type:
                         return AnyType()
                     if not self.chk.in_checked_function():
                         return AnyType()
+                    if self.chk.scope.active_class() is not None:
+                        self.chk.fail('super() outside of a method is not supported', e)
+                        return AnyType()
                     args = self.chk.scope.top_function().arguments
                     # An empty args with super() is an error; we need something in declared_self
                     if not args:


Test patch:
diff --git a/test-data/unit/check-super.test b/test-data/unit/check-super.test
--- a/test-data/unit/check-super.test
+++ b/test-data/unit/check-super.test
@@ -92,6 +92,13 @@ B(1)
 B(1, 'x')
 [builtins fixtures/__new__.pyi]
 
+[case testSuperOutsideMethodNoCrash]
+class C:
+    a = super().whatever  # E: super() outside of a method is not supported
+
+reveal_type(C.a)  # E: Revealed type is 'Any'
+[out]
+
 [case testSuperWithUnknownBase]
 from typing import Any
 B = None  # type: Any