Check basic signature validity and tweak annotation of self/cls argument.
(self, func: FuncDef, info: TypeInfo, has_self_type: bool)
| 1132 | return typ.copy_modified(arg_types=new_arg_types, unpack_kwargs=True) |
| 1133 | |
| 1134 | def prepare_method_signature(self, func: FuncDef, info: TypeInfo, has_self_type: bool) -> None: |
| 1135 | """Check basic signature validity and tweak annotation of self/cls argument.""" |
| 1136 | # Only non-static methods are special, as well as __new__. |
| 1137 | functype = func.type |
| 1138 | if func.name == "__new__": |
| 1139 | func.is_static = True |
| 1140 | if func.has_self_or_cls_argument: |
| 1141 | if func.name in ["__init_subclass__", "__class_getitem__"]: |
| 1142 | func.is_class = True |
| 1143 | if func.arguments and isinstance(functype, CallableType): |
| 1144 | self_type = get_proper_type(functype.arg_types[0]) |
| 1145 | if isinstance(self_type, AnyType): |
| 1146 | if has_self_type: |
| 1147 | assert self.type is not None and self.type.self_type is not None |
| 1148 | leading_type: Type = self.type.self_type |
| 1149 | else: |
| 1150 | func.is_trivial_self = True |
| 1151 | leading_type = fill_typevars(info) |
| 1152 | if func.is_class or func.name == "__new__": |
| 1153 | leading_type = self.class_type(leading_type) |
| 1154 | func.type = replace_implicit_first_type(functype, leading_type) |
| 1155 | elif has_self_type and isinstance(func.unanalyzed_type, CallableType): |
| 1156 | if not isinstance(get_proper_type(func.unanalyzed_type.arg_types[0]), AnyType): |
| 1157 | if self.is_expected_self_type( |
| 1158 | self_type, func.is_class or func.name == "__new__" |
| 1159 | ): |
| 1160 | # This error is off by default, since it is explicitly allowed |
| 1161 | # by the PEP 673. |
| 1162 | self.fail( |
| 1163 | 'Redundant "Self" annotation for the first method argument', |
| 1164 | func, |
| 1165 | code=codes.REDUNDANT_SELF_TYPE, |
| 1166 | ) |
| 1167 | else: |
| 1168 | self.fail( |
| 1169 | "Method cannot have explicit self annotation and Self type", func |
| 1170 | ) |
| 1171 | elif has_self_type: |
| 1172 | self.fail("Static methods cannot use Self type", func) |
| 1173 | |
| 1174 | def is_expected_self_type(self, typ: Type, is_classmethod: bool) -> bool: |
| 1175 | """Does this (analyzed or not) type represent the expected Self type for a method?""" |
no test coverage detected