Skip to content

Add get_expression_type to CheckerPluginInterface #15369

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add get_expression_type to CheckerPluginInterface
  • Loading branch information
ikonst committed Jun 5, 2023
commit e0d20638f6d541de63587b37275722c1dc8dedfe
3 changes: 3 additions & 0 deletions mypy/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -6793,6 +6793,9 @@ def has_valid_attribute(self, typ: Type, name: str) -> bool:
)
return not watcher.has_new_errors()

def get_expression_type(self, node: Expression, type_context: Type | None = None) -> Type:
return self.expr_checker.accept(node, type_context=type_context)


class CollectArgTypeVarTypes(TypeTraverserVisitor):
"""Collects the non-nested argument types in a set."""
Expand Down
5 changes: 5 additions & 0 deletions mypy/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,11 @@ def named_generic_type(self, name: str, args: list[Type]) -> Instance:
"""Construct an instance of a builtin type with given type arguments."""
raise NotImplementedError

@abstractmethod
def get_expression_type(self, node: Expression, type_context: Type | None = None) -> Type:
"""Checks the type of the given expression."""
raise NotImplementedError


@trait
class SemanticAnalyzerPluginInterface:
Expand Down
5 changes: 4 additions & 1 deletion test-data/unit/check-custom-plugin.test
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,10 @@ plugins=<ROOT>/test-data/unit/plugins/descriptor.py
# flags: --config-file tmp/mypy.ini

def dynamic_signature(arg1: str) -> str: ...
reveal_type(dynamic_signature(1)) # N: Revealed type is "builtins.int"
a: int = 1
reveal_type(dynamic_signature(a)) # N: Revealed type is "builtins.int"
b: bytes = b'foo'
reveal_type(dynamic_signature(b)) # N: Revealed type is "builtins.bytes"
[file mypy.ini]
\[mypy]
plugins=<ROOT>/test-data/unit/plugins/function_sig_hook.py
Expand Down
21 changes: 9 additions & 12 deletions test-data/unit/plugins/function_sig_hook.py
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