-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add get_expression_type to CheckerPluginInterface (#15369)
Fixes #14845. p.s. In the issue above, I was concerned that adding this method would create an avenue for infinite recursions (if called carelessly), but in fact I haven't managed to induce it, e.g. FunctionSigContext has `args` but not the call expression itself.
- Loading branch information
Showing
5 changed files
with
25 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,23 @@ | ||
from mypy.plugin import CallableType, CheckerPluginInterface, FunctionSigContext, Plugin | ||
from mypy.types import Instance, Type | ||
from mypy.plugin import CallableType, FunctionSigContext, Plugin | ||
|
||
|
||
class FunctionSigPlugin(Plugin): | ||
def get_function_signature_hook(self, fullname): | ||
if fullname == '__main__.dynamic_signature': | ||
return my_hook | ||
return None | ||
|
||
def _str_to_int(api: CheckerPluginInterface, typ: Type) -> Type: | ||
if isinstance(typ, Instance): | ||
if typ.type.fullname == 'builtins.str': | ||
return api.named_generic_type('builtins.int', []) | ||
elif typ.args: | ||
return typ.copy_modified(args=[_str_to_int(api, t) for t in typ.args]) | ||
|
||
return typ | ||
|
||
def my_hook(ctx: FunctionSigContext) -> CallableType: | ||
arg1_args = ctx.args[0] | ||
if len(arg1_args) != 1: | ||
return ctx.default_signature | ||
arg1_type = ctx.api.get_expression_type(arg1_args[0]) | ||
return ctx.default_signature.copy_modified( | ||
arg_types=[_str_to_int(ctx.api, t) for t in ctx.default_signature.arg_types], | ||
ret_type=_str_to_int(ctx.api, ctx.default_signature.ret_type), | ||
arg_types=[arg1_type], | ||
ret_type=arg1_type, | ||
) | ||
|
||
|
||
def plugin(version): | ||
return FunctionSigPlugin |