1from mypy.plugin import CallableType, CheckerPluginInterface, FunctionSigContext, Plugin 2from mypy.types import Instance, Type 3 4class FunctionSigPlugin(Plugin): 5 def get_function_signature_hook(self, fullname): 6 if fullname == '__main__.dynamic_signature': 7 return my_hook 8 return None 9 10def _str_to_int(api: CheckerPluginInterface, typ: Type) -> Type: 11 if isinstance(typ, Instance): 12 if typ.type.fullname == 'builtins.str': 13 return api.named_generic_type('builtins.int', []) 14 elif typ.args: 15 return typ.copy_modified(args=[_str_to_int(api, t) for t in typ.args]) 16 17 return typ 18 19def my_hook(ctx: FunctionSigContext) -> CallableType: 20 return ctx.default_signature.copy_modified( 21 arg_types=[_str_to_int(ctx.api, t) for t in ctx.default_signature.arg_types], 22 ret_type=_str_to_int(ctx.api, ctx.default_signature.ret_type), 23 ) 24 25def plugin(version): 26 return FunctionSigPlugin 27