File: class_attr_hook.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 (23 lines) | stat: -rw-r--r-- 585 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
from __future__ import annotations

from typing import Callable

from mypy.plugin import AttributeContext, Plugin
from mypy.types import Type as MypyType


class ClassAttrPlugin(Plugin):
    def get_class_attribute_hook(
        self, fullname: str
    ) -> Callable[[AttributeContext], MypyType] | None:
        if fullname == "__main__.Cls.attr":
            return my_hook
        return None


def my_hook(ctx: AttributeContext) -> MypyType:
    return ctx.api.named_generic_type("builtins.int", [])


def plugin(_version: str) -> type[ClassAttrPlugin]:
    return ClassAttrPlugin