Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ these optimizations.
- Return early where possible in subtype check (Stanislav Terliakov, PR [19400](https://github.com/python/mypy/pull/19400))
- Deduplicate some types before joining (Stanislav Terliakov, PR [19409](https://github.com/python/mypy/pull/19409))
- Speed up type checking by caching argument inference context (Jukka Lehtosalo, PR [19323](https://github.com/python/mypy/pull/19323))
- Optimize binding method self argument type and deprecation checks (Ivan Levkivskyi, PR [19556](https://github.com/python/mypy/pull/19556))
- Optimize binding method self parameter type and deprecation checks (Ivan Levkivskyi, PR [19556](https://github.com/python/mypy/pull/19556))
- Keep trivial instance types/aliases during expansion (Ivan Levkivskyi, PR [19543](https://github.com/python/mypy/pull/19543))

### Fixed‑Format Cache (Experimental)
Expand Down
2 changes: 1 addition & 1 deletion docs/source/error_code_list2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ Check that methods do not have redundant Self annotations [redundant-self]
--------------------------------------------------------------------------

If a method uses the ``Self`` type in the return type or the type of a
non-self argument, there is no need to annotate the ``self`` argument
non-self parameter, there is no need to annotate the ``self`` parameter
explicitly. Such annotations are allowed by :pep:`673` but are
redundant. If you enable this error code, mypy will generate an error if
there is a redundant ``Self`` type.
Expand Down
4 changes: 2 additions & 2 deletions docs/source/more_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ for certain values of type arguments (Python 3.12 syntax):
return self.item.upper()

def label(ti: Tag[int], ts: Tag[str]) -> None:
ti.uppercase_item() # E: Invalid self argument "Tag[int]" to attribute function
ti.uppercase_item() # E: Invalid self parameter "Tag[int]" to attribute function
# "uppercase_item" with type "Callable[[Tag[str]], str]"
ts.uppercase_item() # This is OK

Expand All @@ -750,7 +750,7 @@ argument is itself generic (Python 3.12 syntax):
page: Storage[list[str]]
page.first_chunk() # OK, type is "str"

Storage(0).first_chunk() # Error: Invalid self argument "Storage[int]" to attribute function
Storage(0).first_chunk() # Error: Invalid self parameter "Storage[int]" to attribute function
# "first_chunk" with type "Callable[[Storage[Sequence[S]]], S]"

Finally, one can use overloads on self-type to express precise types of
Expand Down
2 changes: 1 addition & 1 deletion mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -5695,7 +5695,7 @@ def _super_arg_types(self, e: SuperExpr) -> Type | tuple[Type, Type]:
current_type = fill_typevars(e.info)
type_type: ProperType = TypeType(current_type)

# Use the type of the self argument, in case it was annotated
# Use the type of the self parameter, in case it was annotated
method = self.chk.scope.current_function()
assert method is not None
if method.arguments:
Expand Down
4 changes: 2 additions & 2 deletions mypy/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -1459,7 +1459,7 @@ def cannot_determine_type_in_base(self, name: str, base: str, context: Context)
def no_formal_self(self, name: str, item: CallableType, context: Context) -> None:
type = format_type(item, self.options)
self.fail(
f'Attribute function "{name}" with type {type} does not accept self argument', context
f'Attribute function "{name}" with type {type} does not accept self parameter', context
)

def incompatible_self_argument(
Expand All @@ -1469,7 +1469,7 @@ def incompatible_self_argument(
arg_type = format_type(arg, self.options)
sig_type = format_type(sig, self.options)
self.fail(
f'Invalid self argument {arg_type} to {kind} "{name}" with type {sig_type}', context
f'Invalid self parameter {arg_type} to {kind} "{name}" with type {sig_type}', context
)

def incompatible_conditional_function_def(
Expand Down
4 changes: 2 additions & 2 deletions mypy/plugins/attrs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ def add_method(
) -> None:
"""Add a method: def <method_name>(self, <args>) -> <ret_type>): ... to info.

self_type: The type to use for the self argument or None to use the inferred self type.
self_type: The type to use for the self parameter or None to use the inferred self type.
tvd: If the method is generic these should be the type variables.
"""
self_type = self_type if self_type is not None else self.self_type
Expand Down Expand Up @@ -1090,7 +1090,7 @@ def _get_expanded_attr_types(
_fail_not_attrs_class(ctx, display_typ, parent_typ)
return None
init_func = expand_type_by_instance(init_func, typ)
# [1:] to skip the self argument of AttrClass.__init__
# [1:] to skip the self parameter of AttrClass.__init__
field_names = cast(list[str], init_func.arg_names[1:])
field_types = init_func.arg_types[1:]
return [dict(zip(field_names, field_types))]
Expand Down
2 changes: 1 addition & 1 deletion mypyc/irbuild/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -1276,7 +1276,7 @@ def enter_method(
"""Generate IR for a method.

If the method takes arguments, you should immediately afterwards call
add_argument() for each non-self argument (self is created implicitly).
add_argument() for each non-self parameter (self is created implicitly).

Args:
class_ir: Add method to this class
Expand Down
38 changes: 19 additions & 19 deletions test-data/unit/check-classes.test
Original file line number Diff line number Diff line change
Expand Up @@ -3339,7 +3339,7 @@ class Test:
# E: Method must have at least one argument. Did you forget the "self" argument?

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

class A:
Expand Down Expand Up @@ -3574,7 +3574,7 @@ class B:
a = A
bad = lambda: 42

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

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

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

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

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

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

Expand Down Expand Up @@ -7931,7 +7931,7 @@ class A:
return 0

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

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

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

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

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

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

reveal_type(C().prop) # N: Revealed type is "builtins.str"
C().prop = "no" # E: Invalid self argument "C" to attribute function "prop" with type "Callable[[A, str], None]"
C().prop = "no" # E: Invalid self parameter "C" to attribute function "prop" with type "Callable[[A, str], None]"
reveal_type(C().prop_t) # N: Revealed type is "__main__.C"
C().prop_t = 1 # E: Incompatible types in assignment (expression has type "int", variable has type "list[C]")
[builtins fixtures/property.pyi]
Expand Down
10 changes: 5 additions & 5 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -616,8 +616,8 @@ class A:
f = x # type: ClassVar[Callable[[], None]]
g = x # type: ClassVar[Callable[[B], None]]
a: A
a.f() # E: Attribute function "f" with type "Callable[[], None]" does not accept self argument
a.g() # E: Invalid self argument "A" to attribute function "g" with type "Callable[[B], None]"
a.f() # E: Attribute function "f" with type "Callable[[], None]" does not accept self parameter
a.g() # E: Invalid self parameter "A" to attribute function "g" with type "Callable[[B], None]"

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

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

reveal_type(C().x1) # E: Invalid self argument "C" to attribute function "x1" with type "Callable[[A, int], str]" \
reveal_type(C().x1) # E: Invalid self parameter "C" to attribute function "x1" with type "Callable[[A, int], str]" \
# N: Revealed type is "def (x: builtins.int) -> builtins.str"
reveal_type(C().x2) # N: Revealed type is "def (x: builtins.int) -> builtins.str"
reveal_type(C().x3) # N: Revealed type is "def (x: builtins.int) -> builtins.str"
reveal_type(C().x4) # N: Revealed type is "def (x: builtins.int) -> builtins.str"
reveal_type(C().x5) # N: Revealed type is "def (x: builtins.int) -> builtins.str"
reveal_type(C().x6) # N: Revealed type is "def (x: builtins.int) -> builtins.str"
reveal_type(C().x7) # E: Invalid self argument "C" to attribute function "x7" with type "Callable[[int], str]" \
reveal_type(C().x7) # E: Invalid self parameter "C" to attribute function "x7" with type "Callable[[int], str]" \
# N: Revealed type is "def () -> builtins.str"
[builtins fixtures/classmethod.pyi]

Expand Down
20 changes: 10 additions & 10 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ class C(Generic[T]):
i: C[int]
s: C[str]

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

[case testSelfTypeRestrictedClassMethod]
Expand All @@ -703,9 +703,9 @@ class C(Generic[T]):
class DI(C[int]): ...
class DS(C[str]): ...

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

Expand Down Expand Up @@ -844,7 +844,7 @@ class Sub(Base[List[int]]): ...
class BadSub(Base[int]): ...

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

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

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

[case testMixinProtocolSuper]
Expand Down Expand Up @@ -912,7 +912,7 @@ class Test:
def meth(self, x: str) -> int: ...

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

[case testSelfTypeTrickyExample]
Expand Down Expand Up @@ -973,9 +973,9 @@ T = TypeVar('T')

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

[case testSelfTypeStructureMetaclassMatch]
from typing import TypeVar, Type, Generic, cast
Expand Down Expand Up @@ -1019,7 +1019,7 @@ class Bad(metaclass=Meta):
pass

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

[case testSelfTypeProtocolClassmethodMatch]
from typing import Type, TypeVar, Protocol
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-typevar-tuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -2034,12 +2034,12 @@ class B(Generic[Unpack[Ts]]):
def on_pair(self: B[T, S]) -> Tuple[T, S]: ...

b1: B[int]
reveal_type(b1.on_pair()) # E: Invalid self argument "B[int]" to attribute function "on_pair" with type "Callable[[B[T, S]], tuple[T, S]]" \
reveal_type(b1.on_pair()) # E: Invalid self parameter "B[int]" to attribute function "on_pair" with type "Callable[[B[T, S]], tuple[T, S]]" \
# N: Revealed type is "tuple[Never, Never]"
b2: B[int, str]
reveal_type(b2.on_pair()) # N: Revealed type is "tuple[builtins.int, builtins.str]"
b3: B[int, str, int]
reveal_type(b3.on_pair()) # E: Invalid self argument "B[int, str, int]" to attribute function "on_pair" with type "Callable[[B[T, S]], tuple[T, S]]" \
reveal_type(b3.on_pair()) # E: Invalid self parameter "B[int, str, int]" to attribute function "on_pair" with type "Callable[[B[T, S]], tuple[T, S]]" \
# N: Revealed type is "tuple[Never, Never]"

class C(B[T, S]): ...
Expand Down