File: callable_instance.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-- 852 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.plugin import MethodContext, Plugin
from mypy.types import Instance, Type


class CallableInstancePlugin(Plugin):
    def get_function_hook(self, fullname: str) -> None:
        assert not fullname.endswith(" of Foo")

    def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | None:
        # Ensure that all names are fully qualified
        assert not fullname.endswith(" of Foo")

        if fullname == "__main__.Class.__call__":
            return my_hook

        return None


def my_hook(ctx: MethodContext) -> Type:
    if isinstance(ctx.type, Instance) and len(ctx.type.args) == 1:
        return ctx.type.args[0]
    return ctx.default_return_type


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