Skip to content

Commit

Permalink
More helpful error for missing self (#14386)
Browse files Browse the repository at this point in the history
Fixes #14385
  • Loading branch information
hauntsaninja authored Jan 3, 2023
1 parent 692af6d commit 2413578
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 14 deletions.
5 changes: 4 additions & 1 deletion mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -947,7 +947,10 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo, has_self_type:
if func.name in ["__init_subclass__", "__class_getitem__"]:
func.is_class = True
if not func.arguments:
self.fail("Method must have at least one argument", func)
self.fail(
'Method must have at least one argument. Did you forget the "self" argument?',
func,
)
elif isinstance(functype, CallableType):
self_type = get_proper_type(functype.arg_types[0])
if isinstance(self_type, AnyType):
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -2901,7 +2901,7 @@ b.bad = 'a' # E: Incompatible types in assignment (expression has type "str", v
from typing import Any

class Test:
def __setattr__() -> None: ... # E: Method must have at least one argument # E: Invalid signature "Callable[[], None]" for "__setattr__"
def __setattr__() -> None: ... # E: Method must have at least one argument. Did you forget the "self" argument? # E: Invalid signature "Callable[[], None]" for "__setattr__"
t = Test()
t.crash = 'test' # E: "Test" has no attribute "crash"

Expand Down Expand Up @@ -7120,7 +7120,7 @@ reveal_type(Foo().y) # N: Revealed type is "builtins.list[Any]"
# flags: --check-untyped-defs

class Foo:
def bad(): # E: Method must have at least one argument
def bad(): # E: Method must have at least one argument. Did you forget the "self" argument?
self.x = 0 # E: Name "self" is not defined

[case testTypeAfterAttributeAccessWithDisallowAnyExpr]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2708,7 +2708,7 @@ class A:
@dec
def e(self) -> int: pass
@property
def g() -> int: pass # E: Method must have at least one argument
def g() -> int: pass # E: Method must have at least one argument. Did you forget the "self" argument?
@property
def h(self, *args, **kwargs) -> int: pass # OK
[builtins fixtures/property.pyi]
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-super.test
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class A:
def f(self) -> None: pass

class B(A):
def g() -> None: # E: Method must have at least one argument
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
def h(self) -> None:
def a() -> None:
Expand Down
12 changes: 6 additions & 6 deletions test-data/unit/fine-grained.test
Original file line number Diff line number Diff line change
Expand Up @@ -1586,11 +1586,11 @@ class A:
[file b.py.3]
2
[out]
a.py:3: error: Method must have at least one argument
a.py:3: error: Method must have at least one argument. Did you forget the "self" argument?
==
a.py:3: error: Method must have at least one argument
a.py:3: error: Method must have at least one argument. Did you forget the "self" argument?
==
a.py:3: error: Method must have at least one argument
a.py:3: error: Method must have at least one argument. Did you forget the "self" argument?

[case testBaseClassDeleted]
import m
Expand Down Expand Up @@ -2007,11 +2007,11 @@ class A:
class A:
def foo(self) -> int: pass
[out]
a.py:2: error: Method must have at least one argument
a.py:2: error: Method must have at least one argument. Did you forget the "self" argument?
==
a.py:2: error: Method must have at least one argument
a.py:2: error: Method must have at least one argument. Did you forget the "self" argument?
==
a.py:2: error: Method must have at least one argument
a.py:2: error: Method must have at least one argument. Did you forget the "self" argument?
==

[case testPreviousErrorInMethodSemanal2]
Expand Down
6 changes: 3 additions & 3 deletions test-data/unit/semanal-errors.test
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ import typing
class A:
def f(): pass
[out]
main:3: error: Method must have at least one argument
main:3: error: Method must have at least one argument. Did you forget the "self" argument?

[case testInvalidBaseClass]
import typing
Expand All @@ -564,8 +564,8 @@ class A:
def f() -> None: pass
def g(): pass
[out]
main:3: error: Method must have at least one argument
main:4: error: Method must have at least one argument
main:3: error: Method must have at least one argument. Did you forget the "self" argument?
main:4: error: Method must have at least one argument. Did you forget the "self" argument?

[case testMultipleMethodDefinition]
import typing
Expand Down

0 comments on commit 2413578

Please sign in to comment.