diff --git a/mypy/checkexpr.py b/mypy/checkexpr.py index be43618011549..0df8f937b43cc 100644 --- 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: diff --git a/test-data/unit/check-super.test b/test-data/unit/check-super.test index 50ab79199c8b7..8853e3ddace06 100644 --- 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