Skip to content

Commit 93e6de4

Browse files
authored
Improve error messages for super checks and add more tests (#16393)
Now all messages use the same `"super"` formatting, it used to be a bit different.
1 parent 44e527a commit 93e6de4

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

mypy/message_registry.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
206206
)
207207
TARGET_CLASS_HAS_NO_BASE_CLASS: Final = ErrorMessage("Target class has no base class")
208208
SUPER_OUTSIDE_OF_METHOD_NOT_SUPPORTED: Final = ErrorMessage(
209-
"super() outside of a method is not supported"
209+
'"super()" outside of a method is not supported'
210210
)
211211
SUPER_ENCLOSING_POSITIONAL_ARGS_REQUIRED: Final = ErrorMessage(
212-
"super() requires one or more positional arguments in enclosing function"
212+
'"super()" requires one or two positional arguments in enclosing function'
213213
)
214214

215215
# Self-type

test-data/unit/check-super.test

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,12 @@ class B(A):
280280

281281

282282
[case testSuperOutsideMethodNoCrash]
283-
class C:
284-
a = super().whatever # E: super() outside of a method is not supported
283+
class A:
284+
x = 1
285+
class B(A): pass
286+
class C(B):
287+
b = super(B, B).x
288+
a = super().whatever # E: "super()" outside of a method is not supported
285289

286290
[case testSuperWithObjectClassAsFirstArgument]
287291
class A:
@@ -366,13 +370,22 @@ class C(B):
366370
[case testSuperInMethodWithNoArguments]
367371
class A:
368372
def f(self) -> None: pass
373+
@staticmethod
374+
def st() -> int:
375+
return 1
369376

370377
class B(A):
371378
def g() -> None: # E: Method must have at least one argument. Did you forget the "self" argument?
372-
super().f() # E: super() requires one or more positional arguments in enclosing function
379+
super().f() # E: "super()" requires one or two positional arguments in enclosing function
373380
def h(self) -> None:
374381
def a() -> None:
375-
super().f() # E: super() requires one or more positional arguments in enclosing function
382+
super().f() # E: "super()" requires one or two positional arguments in enclosing function
383+
@staticmethod
384+
def st() -> int:
385+
reveal_type(super(B, B).st()) # N: Revealed type is "builtins.int"
386+
super().st() # E: "super()" requires one or two positional arguments in enclosing function
387+
return 2
388+
[builtins fixtures/staticmethod.pyi]
376389

377390
[case testSuperWithUnsupportedTypeObject]
378391
from typing import Type

0 commit comments

Comments
 (0)