Debian Codegraph @

test-data/unit/plugins/named_callable.py

raw
1 from __future__ import annotations
2
3 from typing import Callable
4
5 from mypy.plugin import FunctionContext, Plugin
6 from mypy.types import CallableType, Type, get_proper_type
7
8
9 class MyPlugin(Plugin):
10 def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type] | None:
11 if fullname == "m.decorator1":
12 return decorator_call_hook
13 if fullname == "m._decorated": # This is a dummy name generated by the plugin
14 return decorate_hook
15 return None
16
17
18 def decorator_call_hook(ctx: FunctionContext) -> Type:
19 default = get_proper_type(ctx.default_return_type)
20 if isinstance(default, CallableType):
21 return default.copy_modified(name="m._decorated")
22 return ctx.default_return_type
23
24
25 def decorate_hook(ctx: FunctionContext) -> Type:
26 default = get_proper_type(ctx.default_return_type)
27 if isinstance(default, CallableType):
28 return default.copy_modified(ret_type=ctx.api.named_generic_type("builtins.str", []))
29 return ctx.default_return_type
30
31
32 def plugin(version: str) -> type[MyPlugin]:
33 return MyPlugin