(
funcname: str,
ret: Type | None, # None means use (patched) self-type
args: list[Argument],
is_classmethod: bool = False,
is_new: bool = False,
)
| 583 | ) |
| 584 | |
| 585 | def add_method( |
| 586 | funcname: str, |
| 587 | ret: Type | None, # None means use (patched) self-type |
| 588 | args: list[Argument], |
| 589 | is_classmethod: bool = False, |
| 590 | is_new: bool = False, |
| 591 | ) -> None: |
| 592 | fullname = f"{info.fullname}.{funcname}" |
| 593 | self_type = shared_self_type.copy_modified( |
| 594 | id=TypeVarId(shared_self_type.id.raw_id, namespace=fullname) |
| 595 | ) |
| 596 | if ret is None: |
| 597 | ret = self_type |
| 598 | if is_classmethod or is_new: |
| 599 | first = [Argument(Var("_cls"), TypeType.make_normalized(self_type), None, ARG_POS)] |
| 600 | else: |
| 601 | first = [Argument(Var("_self"), self_type, None, ARG_POS)] |
| 602 | args = first + args |
| 603 | |
| 604 | types = [arg.type_annotation for arg in args] |
| 605 | items = [arg.variable.name for arg in args] |
| 606 | arg_kinds = [arg.kind for arg in args] |
| 607 | assert None not in types |
| 608 | signature = CallableType(cast(list[Type], types), arg_kinds, items, ret, function_type) |
| 609 | signature.variables = (self_type,) |
| 610 | func = FuncDef(funcname, args, Block([])) |
| 611 | func.info = info |
| 612 | func.is_class = is_classmethod |
| 613 | func.type = set_callable_name(signature, func) |
| 614 | func._fullname = fullname |
| 615 | func.line = line |
| 616 | if is_classmethod: |
| 617 | v = Var(funcname, func.type) |
| 618 | v.is_classmethod = True |
| 619 | v.info = info |
| 620 | v._fullname = func._fullname |
| 621 | func.is_decorated = True |
| 622 | dec = Decorator(func, [NameExpr("classmethod")], v) |
| 623 | dec.line = line |
| 624 | sym = SymbolTableNode(MDEF, dec) |
| 625 | else: |
| 626 | sym = SymbolTableNode(MDEF, func) |
| 627 | sym.plugin_generated = True |
| 628 | info.names[funcname] = sym |
| 629 | |
| 630 | add_method( |
| 631 | "_replace", |
nothing calls this directly
no test coverage detected