Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a crash when incorrect super() is used outside a method #14208

Merged
merged 1 commit into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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