| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Callable |
| 4 |
|
| 5 |
from mypy.nodes import ARG_POS, Argument, Var |
| 6 |
from mypy.plugin import ClassDefContext, Plugin |
| 7 |
from mypy.plugins.common import add_method |
| 8 |
from mypy.types import NoneType |
| 9 |
|
| 10 |
|
| 11 |
class ClassMethodPlugin(Plugin): |
| 12 |
def get_base_class_hook(self, fullname: str) -> Callable[[ClassDefContext], None] | None: |
| 13 |
if "BaseAddMethod" in fullname: |
| 14 |
return add_extra_methods_hook |
| 15 |
return None |
| 16 |
|
| 17 |
|
| 18 |
def add_extra_methods_hook(ctx: ClassDefContext) -> None: |
| 19 |
add_method(ctx, "foo_classmethod", [], NoneType(), is_classmethod=True) |
| 20 |
add_method( |
| 21 |
ctx, |
| 22 |
"foo_staticmethod", |
| 23 |
[Argument(Var(""), ctx.api.named_type("builtins.int"), None, ARG_POS)], |
| 24 |
ctx.api.named_type("builtins.str"), |
| 25 |
is_staticmethod=True, |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
def plugin(version: str) -> type[ClassMethodPlugin]: |
| 30 |
return ClassMethodPlugin |