File: add_classmethod.py

package info (click to toggle)
mypy 1.19.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 22,412 kB
  • sloc: python: 114,754; ansic: 13,343; cpp: 11,380; makefile: 257; sh: 28
file content (30 lines) | stat: -rw-r--r-- 902 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from __future__ import annotations

from typing import Callable

from mypy.nodes import ARG_POS, Argument, Var
from mypy.plugin import ClassDefContext, Plugin
from mypy.plugins.common import add_method
from mypy.types import NoneType


class ClassMethodPlugin(Plugin):
    def get_base_class_hook(self, fullname: str) -> Callable[[ClassDefContext], None] | None:
        if "BaseAddMethod" in fullname:
            return add_extra_methods_hook
        return None


def add_extra_methods_hook(ctx: ClassDefContext) -> None:
    add_method(ctx, "foo_classmethod", [], NoneType(), is_classmethod=True)
    add_method(
        ctx,
        "foo_staticmethod",
        [Argument(Var(""), ctx.api.named_type("builtins.int"), None, ARG_POS)],
        ctx.api.named_type("builtins.str"),
        is_staticmethod=True,
    )


def plugin(version: str) -> type[ClassMethodPlugin]:
    return ClassMethodPlugin