| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from typing import Callable |
| 4 |
|
| 5 |
from mypy.plugin import FunctionSigContext, Plugin |
| 6 |
from mypy.types import CallableType |
| 7 |
|
| 8 |
|
| 9 |
class FunctionSigPlugin(Plugin): |
| 10 |
def get_function_signature_hook( |
| 11 |
self, fullname: str |
| 12 |
) -> Callable[[FunctionSigContext], CallableType] | None: |
| 13 |
if fullname == "__main__.dynamic_signature": |
| 14 |
return my_hook |
| 15 |
return None |
| 16 |
|
| 17 |
|
| 18 |
def my_hook(ctx: FunctionSigContext) -> CallableType: |
| 19 |
arg1_args = ctx.args[0] |
| 20 |
if len(arg1_args) != 1: |
| 21 |
return ctx.default_signature |
| 22 |
arg1_type = ctx.api.get_expression_type(arg1_args[0]) |
| 23 |
return ctx.default_signature.copy_modified(arg_types=[arg1_type], ret_type=arg1_type) |
| 24 |
|
| 25 |
|
| 26 |
def plugin(version: str) -> type[FunctionSigPlugin]: |
| 27 |
return FunctionSigPlugin |