Skip to content

Commit

Permalink
Fix a crash when incorrect super() is used outside a method (#14208)
Browse files Browse the repository at this point in the history
Ref #14201
  • Loading branch information
ilevkivskyi committed Nov 28, 2022
1 parent 71288c7 commit 00ee7d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4390,13 +4390,14 @@ def visit_super_expr(self, e: SuperExpr) -> Type:
index = mro.index(type_info)
else:
method = self.chk.scope.top_function()
assert method is not None
# Mypy explicitly allows supertype upper bounds (and no upper bound at all)
# for annotating self-types. However, if such an annotation is used for
# checking super() we will still get an error. So to be consistent, we also
# allow such imprecise annotations for use with super(), where we fall back
# to the current class MRO instead.
if is_self_type_like(instance_type, is_classmethod=method.is_class):
# to the current class MRO instead. This works only from inside a method.
if method is not None and is_self_type_like(
instance_type, is_classmethod=method.is_class
):
if e.info and type_info in e.info.mro:
mro = e.info.mro
index = mro.index(type_info)
Expand Down
7 changes: 7 additions & 0 deletions test-data/unit/check-super.test
Original file line number Diff line number Diff line change
Expand Up @@ -409,3 +409,10 @@ class B(A):
reveal_type(super().foo()) # N: Revealed type is "T`-1"
return super().foo()
[builtins fixtures/classmethod.pyi]

[case testWrongSuperOutsideMethodNoCrash]
class B:
x: int
class C1(B): ...
class C2(B): ...
super(C1, C2).x # E: Argument 2 for "super" not an instance of argument 1

0 comments on commit 00ee7d5

Please sign in to comment.