Skip to content

Commit 6c0691e

Browse files
authored
Replace "self argument" with "self parameter"
1 parent 6fcb8f0 commit 6c0691e

11 files changed

Lines changed: 46 additions & 46 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ these optimizations.
329329
- Return early where possible in subtype check (Stanislav Terliakov, PR [19400](https://github.com/python/mypy/pull/19400))
330330
- Deduplicate some types before joining (Stanislav Terliakov, PR [19409](https://github.com/python/mypy/pull/19409))
331331
- Speed up type checking by caching argument inference context (Jukka Lehtosalo, PR [19323](https://github.com/python/mypy/pull/19323))
332-
- Optimize binding method self argument type and deprecation checks (Ivan Levkivskyi, PR [19556](https://github.com/python/mypy/pull/19556))
332+
- Optimize binding method self parameter type and deprecation checks (Ivan Levkivskyi, PR [19556](https://github.com/python/mypy/pull/19556))
333333
- Keep trivial instance types/aliases during expansion (Ivan Levkivskyi, PR [19543](https://github.com/python/mypy/pull/19543))
334334

335335
### Fixed‑Format Cache (Experimental)

docs/source/error_code_list2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ Check that methods do not have redundant Self annotations [redundant-self]
9494
--------------------------------------------------------------------------
9595

9696
If a method uses the ``Self`` type in the return type or the type of a
97-
non-self argument, there is no need to annotate the ``self`` argument
97+
non-self parameter, there is no need to annotate the ``self`` argument
9898
explicitly. Such annotations are allowed by :pep:`673` but are
9999
redundant. If you enable this error code, mypy will generate an error if
100100
there is a redundant ``Self`` type.

docs/source/more_types.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -729,7 +729,7 @@ for certain values of type arguments (Python 3.12 syntax):
729729
return self.item.upper()
730730
731731
def label(ti: Tag[int], ts: Tag[str]) -> None:
732-
ti.uppercase_item() # E: Invalid self argument "Tag[int]" to attribute function
732+
ti.uppercase_item() # E: Invalid self parameter "Tag[int]" to attribute function
733733
# "uppercase_item" with type "Callable[[Tag[str]], str]"
734734
ts.uppercase_item() # This is OK
735735
@@ -750,7 +750,7 @@ argument is itself generic (Python 3.12 syntax):
750750
page: Storage[list[str]]
751751
page.first_chunk() # OK, type is "str"
752752
753-
Storage(0).first_chunk() # Error: Invalid self argument "Storage[int]" to attribute function
753+
Storage(0).first_chunk() # Error: Invalid self parameter "Storage[int]" to attribute function
754754
# "first_chunk" with type "Callable[[Storage[Sequence[S]]], S]"
755755
756756
Finally, one can use overloads on self-type to express precise types of

mypy/checkexpr.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5695,7 +5695,7 @@ def _super_arg_types(self, e: SuperExpr) -> Type | tuple[Type, Type]:
56955695
current_type = fill_typevars(e.info)
56965696
type_type: ProperType = TypeType(current_type)
56975697

5698-
# Use the type of the self argument, in case it was annotated
5698+
# Use the type of the self parameter, in case it was annotated
56995699
method = self.chk.scope.current_function()
57005700
assert method is not None
57015701
if method.arguments:

mypy/messages.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,7 @@ def cannot_determine_type_in_base(self, name: str, base: str, context: Context)
14591459
def no_formal_self(self, name: str, item: CallableType, context: Context) -> None:
14601460
type = format_type(item, self.options)
14611461
self.fail(
1462-
f'Attribute function "{name}" with type {type} does not accept self argument', context
1462+
f'Attribute function "{name}" with type {type} does not accept self parameter', context
14631463
)
14641464

14651465
def incompatible_self_argument(
@@ -1469,7 +1469,7 @@ def incompatible_self_argument(
14691469
arg_type = format_type(arg, self.options)
14701470
sig_type = format_type(sig, self.options)
14711471
self.fail(
1472-
f'Invalid self argument {arg_type} to {kind} "{name}" with type {sig_type}', context
1472+
f'Invalid self parameter {arg_type} to {kind} "{name}" with type {sig_type}', context
14731473
)
14741474

14751475
def incompatible_conditional_function_def(

mypy/plugins/attrs.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ def add_method(
10161016
) -> None:
10171017
"""Add a method: def <method_name>(self, <args>) -> <ret_type>): ... to info.
10181018
1019-
self_type: The type to use for the self argument or None to use the inferred self type.
1019+
self_type: The type to use for the self parameter or None to use the inferred self type.
10201020
tvd: If the method is generic these should be the type variables.
10211021
"""
10221022
self_type = self_type if self_type is not None else self.self_type
@@ -1090,7 +1090,7 @@ def _get_expanded_attr_types(
10901090
_fail_not_attrs_class(ctx, display_typ, parent_typ)
10911091
return None
10921092
init_func = expand_type_by_instance(init_func, typ)
1093-
# [1:] to skip the self argument of AttrClass.__init__
1093+
# [1:] to skip the self parameter of AttrClass.__init__
10941094
field_names = cast(list[str], init_func.arg_names[1:])
10951095
field_types = init_func.arg_types[1:]
10961096
return [dict(zip(field_names, field_types))]

mypyc/irbuild/builder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ def enter_method(
12761276
"""Generate IR for a method.
12771277
12781278
If the method takes arguments, you should immediately afterwards call
1279-
add_argument() for each non-self argument (self is created implicitly).
1279+
add_argument() for each non-self parameter (self is created implicitly).
12801280
12811281
Args:
12821282
class_ir: Add method to this class

test-data/unit/check-classes.test

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3339,7 +3339,7 @@ class Test:
33393339
# E: Method must have at least one argument. Did you forget the "self" argument?
33403340

33413341
t = Test()
3342-
t.crash = 'test' # E: Attribute function "__setattr__" with type "Callable[[], None]" does not accept self argument \
3342+
t.crash = 'test' # E: Attribute function "__setattr__" with type "Callable[[], None]" does not accept self parameter \
33433343
# E: "Test" has no attribute "crash"
33443344

33453345
class A:
@@ -3574,7 +3574,7 @@ class B:
35743574
a = A
35753575
bad = lambda: 42
35763576

3577-
B().bad() # E: Attribute function "bad" with type "Callable[[], int]" does not accept self argument
3577+
B().bad() # E: Attribute function "bad" with type "Callable[[], int]" does not accept self parameter
35783578
reveal_type(B.a) # N: Revealed type is "def () -> __main__.A"
35793579
reveal_type(B().a) # N: Revealed type is "def () -> __main__.A"
35803580
reveal_type(B().a()) # N: Revealed type is "__main__.A"
@@ -5286,9 +5286,9 @@ reveal_type(A.g4) # N: Revealed type is "def () -> def () -> __main__.A"
52865286
class B(metaclass=M):
52875287
def foo(self): pass
52885288

5289-
B.g1 # E: Invalid self argument "type[B]" to attribute function "g1" with type "Callable[[type[A]], A]"
5290-
B.g2 # E: Invalid self argument "type[B]" to attribute function "g2" with type "Callable[[type[TA]], TA]"
5291-
B.g3 # E: Invalid self argument "type[B]" to attribute function "g3" with type "Callable[[TTA], TTA]"
5289+
B.g1 # E: Invalid self parameter "type[B]" to attribute function "g1" with type "Callable[[type[A]], A]"
5290+
B.g2 # E: Invalid self parameter "type[B]" to attribute function "g2" with type "Callable[[type[TA]], TA]"
5291+
B.g3 # E: Invalid self parameter "type[B]" to attribute function "g3" with type "Callable[[TTA], TTA]"
52925292
reveal_type(B.g4) # N: Revealed type is "def () -> def () -> __main__.B"
52935293

52945294
# 4 examples of unsoundness - instantiation, classmethod, staticmethod and ClassVar:
@@ -5301,9 +5301,9 @@ reveal_type(ta.g3) # N: Revealed type is "def () -> type[__main__.A]"
53015301
reveal_type(ta.g4) # N: Revealed type is "def () -> type[__main__.A]"
53025302

53035303
x: M = ta
5304-
x.g1 # E: Invalid self argument "M" to attribute function "g1" with type "Callable[[type[A]], A]"
5305-
x.g2 # E: Invalid self argument "M" to attribute function "g2" with type "Callable[[type[TA]], TA]"
5306-
x.g3 # E: Invalid self argument "M" to attribute function "g3" with type "Callable[[TTA], TTA]"
5304+
x.g1 # E: Invalid self parameter "M" to attribute function "g1" with type "Callable[[type[A]], A]"
5305+
x.g2 # E: Invalid self parameter "M" to attribute function "g2" with type "Callable[[type[TA]], TA]"
5306+
x.g3 # E: Invalid self parameter "M" to attribute function "g3" with type "Callable[[TTA], TTA]"
53075307
reveal_type(x.g4) # N: Revealed type is "def () -> __main__.M"
53085308

53095309
def r(ta: Type[TA], tta: TTA) -> None:
@@ -7889,11 +7889,11 @@ reveal_type(A().fn3) # N: Revealed type is "builtins.int"
78897889
reveal_type(A().fn4) # N: Revealed type is "builtins.int"
78907890
reveal_type(A().fn5) # N: Revealed type is "builtins.int"
78917891

7892-
reveal_type(A().g1) # E: Attribute function "g1" with type "Callable[[], None]" does not accept self argument \
7892+
reveal_type(A().g1) # E: Attribute function "g1" with type "Callable[[], None]" does not accept self parameter \
78937893
# N: Revealed type is "def ()"
7894-
reveal_type(A().g2) # E: Invalid self argument "A" to attribute function "g2" with type "Callable[[int], None]" \
7894+
reveal_type(A().g2) # E: Invalid self parameter "A" to attribute function "g2" with type "Callable[[int], None]" \
78957895
# N: Revealed type is "def ()"
7896-
reveal_type(A().g3) # E: Invalid self argument "A" to attribute function "g3" with type "Callable[[int], None]" \
7896+
reveal_type(A().g3) # E: Invalid self parameter "A" to attribute function "g3" with type "Callable[[int], None]" \
78977897
# N: Revealed type is "def ()"
78987898
[builtins fixtures/tuple.pyi]
78997899

@@ -7931,7 +7931,7 @@ class A:
79317931
return 0
79327932

79337933
reveal_type(A().fn1) # N: Revealed type is "def () -> builtins.int"
7934-
reveal_type(A().fn2) # E: Invalid self argument "A" to attribute function "fn2" with type "Callable[[int], int]" \
7934+
reveal_type(A().fn2) # E: Invalid self parameter "A" to attribute function "fn2" with type "Callable[[int], int]" \
79357935
# N: Revealed type is "def () -> builtins.int"
79367936
reveal_type(A().fn3) # N: Revealed type is "def (_x: builtins.int) -> builtins.int"
79377937

@@ -7952,11 +7952,11 @@ class B:
79527952
def fn4(self, new_self: 'B') -> int:
79537953
return 0
79547954

7955-
reveal_type(B().fn1) # E: Attribute function "fn1" with type "Callable[[], int]" does not accept self argument \
7955+
reveal_type(B().fn1) # E: Attribute function "fn1" with type "Callable[[], int]" does not accept self parameter \
79567956
# N: Revealed type is "def () -> builtins.int"
7957-
reveal_type(B().fn2) # E: Attribute function "fn2" with type "Callable[[], int]" does not accept self argument \
7957+
reveal_type(B().fn2) # E: Attribute function "fn2" with type "Callable[[], int]" does not accept self parameter \
79587958
# N: Revealed type is "def () -> builtins.int"
7959-
reveal_type(B().fn3) # E: Invalid self argument "B" to attribute function "fn3" with type "Callable[[int], int]" \
7959+
reveal_type(B().fn3) # E: Invalid self parameter "B" to attribute function "fn3" with type "Callable[[int], int]" \
79607960
# N: Revealed type is "def () -> builtins.int"
79617961
reveal_type(B().fn4) # N: Revealed type is "def () -> builtins.int"
79627962

@@ -7990,11 +7990,11 @@ class D:
79907990
def fn3(self, _x: int) -> int: # E: "self" parameter missing for a non-static method (or an invalid type for self)
79917991
return 0
79927992

7993-
reveal_type(D().fn1) # E: Invalid self argument "D" to attribute function "fn1" with type "Callable[[int], int]" \
7993+
reveal_type(D().fn1) # E: Invalid self parameter "D" to attribute function "fn1" with type "Callable[[int], int]" \
79947994
# N: Revealed type is "def () -> builtins.int"
7995-
reveal_type(D().fn2) # E: Invalid self argument "D" to attribute function "fn2" with type "Callable[[int, int], int]" \
7995+
reveal_type(D().fn2) # E: Invalid self parameter "D" to attribute function "fn2" with type "Callable[[int, int], int]" \
79967996
# N: Revealed type is "def (_x: builtins.int) -> builtins.int"
7997-
reveal_type(D().fn3) # E: Invalid self argument "D" to attribute function "fn3" with type "Callable[[int, D, int], int]" \
7997+
reveal_type(D().fn3) # E: Invalid self parameter "D" to attribute function "fn3" with type "Callable[[int, D, int], int]" \
79987998
# N: Revealed type is "def (self: __main__.D, _x: builtins.int) -> builtins.int"
79997999
[builtins fixtures/tuple.pyi]
80008000

@@ -9360,7 +9360,7 @@ class C(B):
93609360
prop_t = AT.prop # E: Incompatible types in assignment (expression has type "C", base class "B" defined the type as "str")
93619361

93629362
reveal_type(C().prop) # N: Revealed type is "builtins.str"
9363-
C().prop = "no" # E: Invalid self argument "C" to attribute function "prop" with type "Callable[[A, str], None]"
9363+
C().prop = "no" # E: Invalid self parameter "C" to attribute function "prop" with type "Callable[[A, str], None]"
93649364
reveal_type(C().prop_t) # N: Revealed type is "__main__.C"
93659365
C().prop_t = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "list[C]")
93669366
[builtins fixtures/property.pyi]

test-data/unit/check-functions.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -616,8 +616,8 @@ class A:
616616
f = x # type: ClassVar[Callable[[], None]]
617617
g = x # type: ClassVar[Callable[[B], None]]
618618
a: A
619-
a.f() # E: Attribute function "f" with type "Callable[[], None]" does not accept self argument
620-
a.g() # E: Invalid self argument "A" to attribute function "g" with type "Callable[[B], None]"
619+
a.f() # E: Attribute function "f" with type "Callable[[], None]" does not accept self parameter
620+
a.g() # E: Invalid self parameter "A" to attribute function "g" with type "Callable[[B], None]"
621621

622622
[case testMethodWithDynamicallyTypedMethodAsDataAttribute]
623623
from typing import Any, Callable, ClassVar
@@ -692,7 +692,7 @@ class A(Generic[t]):
692692
ab: A[B]
693693
ac: A[C]
694694
ab.f()
695-
ac.f() # E: Invalid self argument "A[C]" to attribute function "f" with type "Callable[[A[B]], None]"
695+
ac.f() # E: Invalid self parameter "A[C]" to attribute function "f" with type "Callable[[A[B]], None]"
696696

697697
[case testPartiallyTypedSelfInMethodDataAttribute]
698698
from typing import Any, TypeVar, Generic, Callable, ClassVar
@@ -3686,14 +3686,14 @@ reveal_type(C.x5) # N: Revealed type is "def (x: builtins.int) -> builtins.str"
36863686
reveal_type(C.x6) # N: Revealed type is "def (x: builtins.int) -> builtins.str"
36873687
reveal_type(C.x7) # N: Revealed type is "def (builtins.int) -> builtins.str"
36883688

3689-
reveal_type(C().x1) # E: Invalid self argument "C" to attribute function "x1" with type "Callable[[A, int], str]" \
3689+
reveal_type(C().x1) # E: Invalid self parameter "C" to attribute function "x1" with type "Callable[[A, int], str]" \
36903690
# N: Revealed type is "def (x: builtins.int) -> builtins.str"
36913691
reveal_type(C().x2) # N: Revealed type is "def (x: builtins.int) -> builtins.str"
36923692
reveal_type(C().x3) # N: Revealed type is "def (x: builtins.int) -> builtins.str"
36933693
reveal_type(C().x4) # N: Revealed type is "def (x: builtins.int) -> builtins.str"
36943694
reveal_type(C().x5) # N: Revealed type is "def (x: builtins.int) -> builtins.str"
36953695
reveal_type(C().x6) # N: Revealed type is "def (x: builtins.int) -> builtins.str"
3696-
reveal_type(C().x7) # E: Invalid self argument "C" to attribute function "x7" with type "Callable[[int], str]" \
3696+
reveal_type(C().x7) # E: Invalid self parameter "C" to attribute function "x7" with type "Callable[[int], str]" \
36973697
# N: Revealed type is "def () -> builtins.str"
36983698
[builtins fixtures/classmethod.pyi]
36993699

test-data/unit/check-selftype.test

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ class C(Generic[T]):
689689
i: C[int]
690690
s: C[str]
691691

692-
i.from_item() # E: Invalid self argument "C[int]" to attribute function "from_item" with type "Callable[[C[str]], None]"
692+
i.from_item() # E: Invalid self parameter "C[int]" to attribute function "from_item" with type "Callable[[C[str]], None]"
693693
s.from_item()
694694

695695
[case testSelfTypeRestrictedClassMethod]
@@ -703,9 +703,9 @@ class C(Generic[T]):
703703
class DI(C[int]): ...
704704
class DS(C[str]): ...
705705

706-
DI().from_item() # E: Invalid self argument "type[DI]" to class attribute function "from_item" with type "Callable[[type[C[str]]], None]"
706+
DI().from_item() # E: Invalid self parameter "type[DI]" to class attribute function "from_item" with type "Callable[[type[C[str]]], None]"
707707
DS().from_item()
708-
DI.from_item() # E: Invalid self argument "type[DI]" to attribute function "from_item" with type "Callable[[type[C[str]]], None]"
708+
DI.from_item() # E: Invalid self parameter "type[DI]" to attribute function "from_item" with type "Callable[[type[C[str]]], None]"
709709
DS.from_item()
710710
[builtins fixtures/classmethod.pyi]
711711

@@ -844,7 +844,7 @@ class Sub(Base[List[int]]): ...
844844
class BadSub(Base[int]): ...
845845

846846
reveal_type(Sub().get_item()) # N: Revealed type is "builtins.int"
847-
BadSub().get_item() # E: Invalid self argument "BadSub" to attribute function "get_item" with type "Callable[[Base[list[S]]], S]"
847+
BadSub().get_item() # E: Invalid self parameter "BadSub" to attribute function "get_item" with type "Callable[[Base[list[S]]], S]"
848848
[builtins fixtures/list.pyi]
849849

850850
[case testMixinAllowedWithProtocol]
@@ -871,10 +871,10 @@ class Bad(AtomicClose, Copyable):
871871
f: File
872872
b: Bad
873873
f.atomic_close() # OK
874-
b.atomic_close() # E: Invalid self argument "Bad" to attribute function "atomic_close" with type "Callable[[Resource], int]"
874+
b.atomic_close() # E: Invalid self parameter "Bad" to attribute function "atomic_close" with type "Callable[[Resource], int]"
875875

876876
reveal_type(f.copy()) # N: Revealed type is "__main__.File"
877-
b.copy() # E: Invalid self argument "Bad" to attribute function "copy" with type "Callable[[T], T]"
877+
b.copy() # E: Invalid self parameter "Bad" to attribute function "copy" with type "Callable[[T], T]"
878878
[builtins fixtures/tuple.pyi]
879879

880880
[case testMixinProtocolSuper]
@@ -912,7 +912,7 @@ class Test:
912912
def meth(self, x: str) -> int: ...
913913

914914
reveal_type(Test().meth) # N: Revealed type is "def (x: builtins.str) -> builtins.int"
915-
Test()._deco # E: Invalid self argument "Test" to attribute function "_deco" with type "Callable[[F], F]"
915+
Test()._deco # E: Invalid self parameter "Test" to attribute function "_deco" with type "Callable[[F], F]"
916916
[builtins fixtures/tuple.pyi]
917917

918918
[case testSelfTypeTrickyExample]
@@ -973,9 +973,9 @@ T = TypeVar('T')
973973

974974
class Foo(Generic[T]):
975975
def f1(self: Foo[str]) -> None:
976-
self.f2() # E: Invalid self argument "Foo[str]" to attribute function "f2" with type "Callable[[Foo[int]], None]"
976+
self.f2() # E: Invalid self parameter "Foo[str]" to attribute function "f2" with type "Callable[[Foo[int]], None]"
977977
def f2(self: Foo[int]) -> None:
978-
self.f1() # E: Invalid self argument "Foo[int]" to attribute function "f1" with type "Callable[[Foo[str]], None]"
978+
self.f1() # E: Invalid self parameter "Foo[int]" to attribute function "f1" with type "Callable[[Foo[str]], None]"
979979

980980
[case testSelfTypeStructureMetaclassMatch]
981981
from typing import TypeVar, Type, Generic, cast
@@ -1019,7 +1019,7 @@ class Bad(metaclass=Meta):
10191019
pass
10201020

10211021
Good.do_x()
1022-
Bad.do_x() # E: Invalid self argument "type[Bad]" to attribute function "do_x" with type "Callable[[type[T]], T]"
1022+
Bad.do_x() # E: Invalid self parameter "type[Bad]" to attribute function "do_x" with type "Callable[[type[T]], T]"
10231023

10241024
[case testSelfTypeProtocolClassmethodMatch]
10251025
from typing import Type, TypeVar, Protocol

0 commit comments

Comments
 (0)