Skip to content

Commit 2413578

Browse files
authored
More helpful error for missing self (#14386)
Fixes #14385
1 parent 692af6d commit 2413578

File tree

6 files changed

+17
-14
lines changed

6 files changed

+17
-14
lines changed

mypy/semanal.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,7 +947,10 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo, has_self_type:
947947
if func.name in ["__init_subclass__", "__class_getitem__"]:
948948
func.is_class = True
949949
if not func.arguments:
950-
self.fail("Method must have at least one argument", func)
950+
self.fail(
951+
'Method must have at least one argument. Did you forget the "self" argument?',
952+
func,
953+
)
951954
elif isinstance(functype, CallableType):
952955
self_type = get_proper_type(functype.arg_types[0])
953956
if isinstance(self_type, AnyType):

test-data/unit/check-classes.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2901,7 +2901,7 @@ b.bad = 'a' # E: Incompatible types in assignment (expression has type "str", v
29012901
from typing import Any
29022902

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

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

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

71267126
[case testTypeAfterAttributeAccessWithDisallowAnyExpr]

test-data/unit/check-functions.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2708,7 +2708,7 @@ class A:
27082708
@dec
27092709
def e(self) -> int: pass
27102710
@property
2711-
def g() -> int: pass # E: Method must have at least one argument
2711+
def g() -> int: pass # E: Method must have at least one argument. Did you forget the "self" argument?
27122712
@property
27132713
def h(self, *args, **kwargs) -> int: pass # OK
27142714
[builtins fixtures/property.pyi]

test-data/unit/check-super.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ class A:
365365
def f(self) -> None: pass
366366

367367
class B(A):
368-
def g() -> None: # E: Method must have at least one argument
368+
def g() -> None: # E: Method must have at least one argument. Did you forget the "self" argument?
369369
super().f() # E: super() requires one or more positional arguments in enclosing function
370370
def h(self) -> None:
371371
def a() -> None:

test-data/unit/fine-grained.test

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1586,11 +1586,11 @@ class A:
15861586
[file b.py.3]
15871587
2
15881588
[out]
1589-
a.py:3: error: Method must have at least one argument
1589+
a.py:3: error: Method must have at least one argument. Did you forget the "self" argument?
15901590
==
1591-
a.py:3: error: Method must have at least one argument
1591+
a.py:3: error: Method must have at least one argument. Did you forget the "self" argument?
15921592
==
1593-
a.py:3: error: Method must have at least one argument
1593+
a.py:3: error: Method must have at least one argument. Did you forget the "self" argument?
15941594

15951595
[case testBaseClassDeleted]
15961596
import m
@@ -2007,11 +2007,11 @@ class A:
20072007
class A:
20082008
def foo(self) -> int: pass
20092009
[out]
2010-
a.py:2: error: Method must have at least one argument
2010+
a.py:2: error: Method must have at least one argument. Did you forget the "self" argument?
20112011
==
2012-
a.py:2: error: Method must have at least one argument
2012+
a.py:2: error: Method must have at least one argument. Did you forget the "self" argument?
20132013
==
2014-
a.py:2: error: Method must have at least one argument
2014+
a.py:2: error: Method must have at least one argument. Did you forget the "self" argument?
20152015
==
20162016

20172017
[case testPreviousErrorInMethodSemanal2]

test-data/unit/semanal-errors.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ import typing
542542
class A:
543543
def f(): pass
544544
[out]
545-
main:3: error: Method must have at least one argument
545+
main:3: error: Method must have at least one argument. Did you forget the "self" argument?
546546

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

570570
[case testMultipleMethodDefinition]
571571
import typing

0 commit comments

Comments
 (0)