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

Improve error messages for super checks and add more tests #16393

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
)
TARGET_CLASS_HAS_NO_BASE_CLASS: Final = ErrorMessage("Target class has no base class")
SUPER_OUTSIDE_OF_METHOD_NOT_SUPPORTED: Final = ErrorMessage(
"super() outside of a method is not supported"
'"super" outside of a method is not supported'
)
SUPER_ENCLOSING_POSITIONAL_ARGS_REQUIRED: Final = ErrorMessage(
"super() requires one or more positional arguments in enclosing function"
'"super" requires one or more positional arguments in enclosing function'
)

# Self-type
Expand Down
21 changes: 17 additions & 4 deletions test-data/unit/check-super.test
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,12 @@ class B(A):


[case testSuperOutsideMethodNoCrash]
class C:
a = super().whatever # E: super() outside of a method is not supported
class A:
x = 1
class B(A): pass
class C(B):
b = super(B, B).x
a = super().whatever # E: "super" outside of a method is not supported
sobolevn marked this conversation as resolved.
Show resolved Hide resolved

[case testSuperWithObjectClassAsFirstArgument]
class A:
Expand Down Expand Up @@ -366,13 +370,22 @@ class C(B):
[case testSuperInMethodWithNoArguments]
class A:
def f(self) -> None: pass
@staticmethod
def st() -> int:
return 1

class B(A):
def g() -> None: # E: Method must have at least one argument. Did you forget the "self" argument?
super().f() # E: super() requires one or more positional arguments in enclosing function
super().f() # E: "super" requires one or more positional arguments in enclosing function
def h(self) -> None:
def a() -> None:
super().f() # E: super() requires one or more positional arguments in enclosing function
super().f() # E: "super" requires one or more positional arguments in enclosing function
@staticmethod
def st() -> int:
reveal_type(super(B, B).st()) # N: Revealed type is "builtins.int"
super().st() # E: "super" requires one or more positional arguments in enclosing function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here again, it's specifically the zero-argument form of super() that we're complaining about (the reveal_type() call in the line immediately above this one works fine for the two-argument form of super()). So I'd keep the () here as well, personally

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And it should be "one or two" not "one or more" 👍

return 2
[builtins fixtures/staticmethod.pyi]

[case testSuperWithUnsupportedTypeObject]
from typing import Type
Expand Down