Adds a new overloaded method to a class definition.
(
api: SemanticAnalyzerPluginInterface | CheckerPluginInterface,
cls: ClassDef,
name: str,
items: list[MethodSpec],
is_classmethod: bool = False,
is_staticmethod: bool = False,
)
| 254 | |
| 255 | |
| 256 | def add_overloaded_method_to_class( |
| 257 | api: SemanticAnalyzerPluginInterface | CheckerPluginInterface, |
| 258 | cls: ClassDef, |
| 259 | name: str, |
| 260 | items: list[MethodSpec], |
| 261 | is_classmethod: bool = False, |
| 262 | is_staticmethod: bool = False, |
| 263 | ) -> OverloadedFuncDef: |
| 264 | """Adds a new overloaded method to a class definition.""" |
| 265 | assert len(items) >= 2, "Overloads must contain at least two cases" |
| 266 | |
| 267 | # Save old definition, if it exists. |
| 268 | _prepare_class_namespace(cls, name) |
| 269 | |
| 270 | # Create function bodies for each passed method spec. |
| 271 | funcs: list[Decorator | FuncDef] = [] |
| 272 | for item in items: |
| 273 | func, _sym = _add_method_by_spec( |
| 274 | api, |
| 275 | cls.info, |
| 276 | name=name, |
| 277 | spec=item, |
| 278 | is_classmethod=is_classmethod, |
| 279 | is_staticmethod=is_staticmethod, |
| 280 | ) |
| 281 | if isinstance(func, FuncDef): |
| 282 | var = Var(func.name, func.type) |
| 283 | var.set_line(func.line) |
| 284 | func.is_decorated = True |
| 285 | |
| 286 | deco = Decorator(func, [], var) |
| 287 | else: |
| 288 | deco = func |
| 289 | deco.is_overload = True |
| 290 | funcs.append(deco) |
| 291 | |
| 292 | # Create the final OverloadedFuncDef node: |
| 293 | overload_def = OverloadedFuncDef(funcs) |
| 294 | overload_def.info = cls.info |
| 295 | overload_def.is_class = is_classmethod |
| 296 | overload_def.is_static = is_staticmethod |
| 297 | sym = SymbolTableNode(MDEF, overload_def) |
| 298 | sym.plugin_generated = True |
| 299 | |
| 300 | cls.info.names[name] = sym |
| 301 | cls.info.defn.defs.body.append(overload_def) |
| 302 | return overload_def |
| 303 | |
| 304 | |
| 305 | def _prepare_class_namespace(cls: ClassDef, name: str) -> None: |
no test coverage detected
searching dependent graphs…