MCPcopy
hub / github.com/python/mypy / add_method_to_class

Function add_method_to_class

mypy/plugins/common.py:225–253  ·  view source on GitHub ↗

Adds a new method to a class definition.

(
    api: SemanticAnalyzerPluginInterface | CheckerPluginInterface,
    cls: ClassDef,
    name: str,
    # MethodSpec items kept for backward compatibility:
    args: list[Argument],
    return_type: Type,
    self_type: Type | None = None,
    tvar_def: list[TypeVarType] | TypeVarType | None = None,
    is_classmethod: bool = False,
    is_staticmethod: bool = False,
)

Source from the content-addressed store, hash-verified

223
224
225def add_method_to_class(
226 api: SemanticAnalyzerPluginInterface | CheckerPluginInterface,
227 cls: ClassDef,
228 name: str,
229 # MethodSpec items kept for backward compatibility:
230 args: list[Argument],
231 return_type: Type,
232 self_type: Type | None = None,
233 tvar_def: list[TypeVarType] | TypeVarType | None = None,
234 is_classmethod: bool = False,
235 is_staticmethod: bool = False,
236) -> FuncDef | Decorator:
237 """Adds a new method to a class definition."""
238 _prepare_class_namespace(cls, name)
239
240 if tvar_def is not None and not isinstance(tvar_def, list):
241 tvar_def = [tvar_def]
242
243 func, sym = _add_method_by_spec(
244 api,
245 cls.info,
246 name,
247 MethodSpec(args=args, return_type=return_type, self_type=self_type, tvar_defs=tvar_def),
248 is_classmethod=is_classmethod,
249 is_staticmethod=is_staticmethod,
250 )
251 cls.info.names[name] = sym
252 cls.info.defn.defs.body.append(func)
253 return func
254
255
256def add_overloaded_method_to_class(

Callers 8

transformMethod · 0.90
_add_dunder_replaceMethod · 0.90
add_methodMethod · 0.90
add_methodFunction · 0.85

Calls 5

_prepare_class_namespaceFunction · 0.85
isinstanceFunction · 0.85
_add_method_by_specFunction · 0.85
MethodSpecClass · 0.85
appendMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…