def _add_replace_method(self, node, cls): typevar = abstract.TypeParameter( abstract_utils.T + cls.name, self.vm, bound=cls) cls.members["replace"] = overlay_utils.make_method( vm=self.vm, node=node, name="replace", return_type=typevar, self_param=overlay_utils.Param("self", typevar), kwargs=overlay_utils.Param("changes"), )
def _make_new(self, node, member_type, cls): return overlay_utils.make_method( vm=self.vm, node=node, name="__new__", params=[overlay_utils.Param("value", member_type)], return_type=cls)
def make_replace_method(ctx, node, cls, *, kwargs_name="kwargs"): """Create a replace() method for a dataclass.""" # This is used by several packages that extend dataclass. # The signature is # def replace(self: T, **kwargs) -> T typevar = abstract.TypeParameter(abstract_utils.T + cls.name, ctx, bound=cls) return overlay_utils.make_method( ctx=ctx, node=node, name="replace", return_type=typevar, self_param=overlay_utils.Param("self", typevar), kwargs=overlay_utils.Param(kwargs_name), )
def _add_from_tuple_method(self, node, cls): # from_tuple is discouraged anyway, so we provide only bare-bones types. cls.members["from_tuple"] = overlay_utils.make_method( vm=self.vm, node=node, name="from_tuple", params=[overlay_utils.Param("args")], return_type=cls, kind=pytd.MethodTypes.STATICMETHOD, )