MCPcopy Create free account
hub / github.com/python/mypy / MethodAdder

Class MethodAdder

mypy/plugins/attrs.py:990–1018  ·  view source on GitHub ↗

Helper to add methods to a TypeInfo. ctx: The ClassDefCtx we are using on which we will add methods.

Source from the content-addressed store, hash-verified

988
989
990class MethodAdder:
991 """Helper to add methods to a TypeInfo.
992
993 ctx: The ClassDefCtx we are using on which we will add methods.
994 """
995
996 # TODO: Combine this with the code build_namedtuple_typeinfo to support both.
997
998 def __init__(self, ctx: mypy.plugin.ClassDefContext) -> None:
999 self.ctx = ctx
1000 self.self_type = fill_typevars(ctx.cls.info)
1001
1002 def add_method(
1003 self,
1004 method_name: str,
1005 args: list[Argument],
1006 ret_type: Type,
1007 self_type: Type | None = None,
1008 tvd: TypeVarType | None = None,
1009 ) -> None:
1010 """Add a method: def <method_name>(self, <args>) -> <ret_type>): ... to info.
1011
1012 self_type: The type to use for the self argument or None to use the inferred self type.
1013 tvd: If the method is generic these should be the type variables.
1014 """
1015 self_type = self_type if self_type is not None else self.self_type
1016 add_method_to_class(
1017 self.ctx.api, self.ctx.cls, method_name, args, ret_type, self_type, tvd
1018 )
1019
1020
1021def _get_attrs_init_type(typ: Instance) -> CallableType | None:

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…