diff --git a/docs/source/error_code_list2.rst b/docs/source/error_code_list2.rst index 36cf05d4f3f8..d8120abb6c7d 100644 --- a/docs/source/error_code_list2.rst +++ b/docs/source/error_code_list2.rst @@ -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. @@ -108,7 +108,7 @@ Example: from typing import Self class C: - # Error: Redundant "Self" annotation for the first method argument + # Error: Redundant "Self" annotation for the first method parameter def copy(self: Self) -> Self: return type(self)() diff --git a/mypy/checker.py b/mypy/checker.py index fc636e9a7218..7d6f2fa90ee8 100644 --- a/mypy/checker.py +++ b/mypy/checker.py @@ -1349,7 +1349,7 @@ def check_func_def( if typ.type_is: arg_index = 0 # For methods and classmethods, we want the second parameter - if ref_type is not None and defn.has_self_or_cls_argument: + if ref_type is not None and defn.has_self_or_cls_parameter: arg_index = 1 if arg_index < len(typ.arg_types) and not is_subtype( typ.type_is, typ.arg_types[arg_index] @@ -1624,7 +1624,7 @@ def require_correct_self_argument(self, func: Type, defn: FuncDef) -> bool: if ref_type is None: return False - if not defn.has_self_or_cls_argument or ( + if not defn.has_self_or_cls_parameter or ( func.arg_kinds and func.arg_kinds[0] in [nodes.ARG_STAR, nodes.ARG_STAR2] ): return False diff --git a/mypy/nodes.py b/mypy/nodes.py index c13fe2af246e..39999d750e80 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -604,7 +604,7 @@ def __init__(self) -> None: self.is_property = False self.is_class = False # Is this a `@staticmethod` (explicit or implicit)? - # Note: use has_self_or_cls_argument to check if there is `self` or `cls` argument + # Note: use has_self_or_cls_parameter to check if there is `self` or `cls` parameter self.is_static = False self.is_final = False self.is_explicit_override = False @@ -622,8 +622,8 @@ def fullname(self) -> str: return self._fullname @property - def has_self_or_cls_argument(self) -> bool: - """If used as a method, does it have an argument for method binding (`self`, `cls`)? + def has_self_or_cls_parameter(self) -> bool: + """If used as a method, does it have an parameter for method binding (`self`, `cls`)? This is true for `__new__` even though `__new__` does not undergo method binding, because we still usually assume that `cls` corresponds to the enclosing class. diff --git a/mypy/semanal.py b/mypy/semanal.py index a7ddddbe083a..097dab7d2e71 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -1133,12 +1133,12 @@ def remove_unpack_kwargs(self, defn: FuncDef, typ: CallableType) -> CallableType return typ.copy_modified(arg_types=new_arg_types, unpack_kwargs=True) def prepare_method_signature(self, func: FuncDef, info: TypeInfo, has_self_type: bool) -> None: - """Check basic signature validity and tweak annotation of self/cls argument.""" + """Check basic signature validity and tweak annotation of self/cls parameter.""" # Only non-static methods are special, as well as __new__. functype = func.type if func.name == "__new__": func.is_static = True - if func.has_self_or_cls_argument: + if func.has_self_or_cls_parameter: if func.name in ["__init_subclass__", "__class_getitem__"]: func.is_class = True if func.arguments and isinstance(functype, CallableType): @@ -1161,7 +1161,7 @@ def prepare_method_signature(self, func: FuncDef, info: TypeInfo, has_self_type: # This error is off by default, since it is explicitly allowed # by the PEP 673. self.fail( - 'Redundant "Self" annotation for the first method argument', + 'Redundant "Self" annotation for the first method parameter', func, code=codes.REDUNDANT_SELF_TYPE, ) @@ -1680,10 +1680,10 @@ def analyze_function_body(self, defn: FuncItem) -> None: for arg in defn.arguments: self.add_local(arg.variable, defn) - # The first argument of a non-static, non-class method is like 'self' + # The first parameter of a non-static, non-class method is like 'self' # (though the name could be different), having the enclosing class's # instance type. - if is_method and defn.has_self_or_cls_argument and defn.arguments: + if is_method and defn.has_self_or_cls_parameter and defn.arguments: if not defn.is_class: defn.arguments[0].variable.is_self = True else: @@ -6129,7 +6129,7 @@ def visit_member_expr(self, expr: MemberExpr) -> None: # check for self.bar or cls.bar in method/classmethod func_def = self.function_stack[-1] if ( - func_def.has_self_or_cls_argument + func_def.has_self_or_cls_parameter and func_def.info is self.type and isinstance(func_def.type, CallableType) and func_def.arguments diff --git a/mypy/suggestions.py b/mypy/suggestions.py index 39a220e34091..1e4df58ac3b6 100644 --- a/mypy/suggestions.py +++ b/mypy/suggestions.py @@ -487,7 +487,7 @@ def get_suggestion(self, mod: str, node: FuncDef) -> PyAnnotateSignature: if self.no_errors and orig_errors: raise SuggestionFailure("Function does not typecheck.") - is_method = bool(node.info) and node.has_self_or_cls_argument + is_method = bool(node.info) and node.has_self_or_cls_parameter with state.strict_optional_set(graph[mod].options.strict_optional): guesses = self.get_guesses( diff --git a/mypy/typeops.py b/mypy/typeops.py index 839c6454ca28..db1a44c77f65 100644 --- a/mypy/typeops.py +++ b/mypy/typeops.py @@ -885,7 +885,7 @@ def callable_type( fdef: FuncItem, fallback: Instance, ret_type: Type | None = None ) -> CallableType: # TODO: somewhat unfortunate duplication with prepare_method_signature in semanal - if fdef.info and fdef.has_self_or_cls_argument and fdef.arg_names: + if fdef.info and fdef.has_self_or_cls_parameter and fdef.arg_names: self_type: Type = fill_typevars(fdef.info) if fdef.is_class or fdef.name == "__new__": self_type = TypeType.make_normalized(self_type) diff --git a/test-data/unit/check-selftype.test b/test-data/unit/check-selftype.test index c15720035256..0785532c832f 100644 --- a/test-data/unit/check-selftype.test +++ b/test-data/unit/check-selftype.test @@ -1728,13 +1728,13 @@ class C: from typing import Self, Type class C: - def copy(self: Self) -> Self: # E: Redundant "Self" annotation for the first method argument + def copy(self: Self) -> Self: # E: Redundant "Self" annotation for the first method parameter d: Defer class Defer: ... return self @classmethod - def g(cls: Type[Self]) -> Self: # E: Redundant "Self" annotation for the first method argument + def g(cls: Type[Self]) -> Self: # E: Redundant "Self" annotation for the first method parameter d: DeferAgain class DeferAgain: ... return cls() @@ -1746,13 +1746,13 @@ class C: from typing import Self class C: - def copy(self: Self) -> Self: # E: Redundant "Self" annotation for the first method argument + def copy(self: Self) -> Self: # E: Redundant "Self" annotation for the first method parameter d: Defer class Defer: ... return self @classmethod - def g(cls: type[Self]) -> Self: # E: Redundant "Self" annotation for the first method argument + def g(cls: type[Self]) -> Self: # E: Redundant "Self" annotation for the first method parameter d: DeferAgain class DeferAgain: ... return cls()