-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathmethod_in_decorator.py
More file actions
25 lines (17 loc) · 801 Bytes
/
method_in_decorator.py
File metadata and controls
25 lines (17 loc) · 801 Bytes
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
from __future__ import annotations
from typing import Callable
from mypy.plugin import MethodContext, Plugin
from mypy.types import CallableType, Type, get_proper_type
class MethodDecoratorPlugin(Plugin):
def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | None:
if "Foo.a" in fullname:
return method_decorator_callback
return None
def method_decorator_callback(ctx: MethodContext) -> Type:
default = get_proper_type(ctx.default_return_type)
if isinstance(default, CallableType):
str_type = ctx.api.named_generic_type("builtins.str", [])
return default.copy_modified(ret_type=str_type)
return ctx.default_return_type
def plugin(version: str) -> type[MethodDecoratorPlugin]:
return MethodDecoratorPlugin