| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Callable |
| 4 |
|
| 5 |
from mypy.plugin import MethodContext, Plugin |
| 6 |
from mypy.types import CallableType, Type, get_proper_type |
| 7 |
|
| 8 |
|
| 9 |
class MethodDecoratorPlugin(Plugin): |
| 10 |
def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | None: |
| 11 |
if "Foo.a" in fullname: |
| 12 |
return method_decorator_callback |
| 13 |
return None |
| 14 |
|
| 15 |
|
| 16 |
def method_decorator_callback(ctx: MethodContext) -> Type: |
| 17 |
default = get_proper_type(ctx.default_return_type) |
| 18 |
if isinstance(default, CallableType): |
| 19 |
str_type = ctx.api.named_generic_type("builtins.str", []) |
| 20 |
return default.copy_modified(ret_type=str_type) |
| 21 |
return ctx.default_return_type |
| 22 |
|
| 23 |
|
| 24 |
def plugin(version: str) -> type[MethodDecoratorPlugin]: |
| 25 |
return MethodDecoratorPlugin |