-
-
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.
Call get_method_hook when methods are used in decorators (#10675)
When a method is used in a decorator, pass the full name to plugins through the get_method_hook function, just like we would normally do for other places that methods got called. Previously, in these situations, we would call get_function_hook instead with '{method_name} of {class_name}', which loses information about which module the class is located in, and gives information about a method to the hook meant for functions. Currently this is limited to situations where the method is accessed in the decorator itself, so @obj.meth def f() -> None: ... would work but method = obj.meth @method def f() -> None: ... probably wouldn't work.
- Loading branch information
1 parent
08e6ba8
commit e4c2668
Showing
3 changed files
with
47 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from mypy.types import CallableType, Type | ||
from typing import Callable, Optional | ||
from mypy.plugin import MethodContext, Plugin | ||
|
||
|
||
class MethodDecoratorPlugin(Plugin): | ||
def get_method_hook(self, fullname: str) -> Optional[Callable[[MethodContext], Type]]: | ||
if 'Foo.a' in fullname: | ||
return method_decorator_callback | ||
return None | ||
|
||
def method_decorator_callback(ctx: MethodContext) -> Type: | ||
if isinstance(ctx.default_return_type, CallableType): | ||
str_type = ctx.api.named_generic_type('builtins.str', []) | ||
return ctx.default_return_type.copy_modified(ret_type=str_type) | ||
return ctx.default_return_type | ||
|
||
def plugin(version): | ||
return MethodDecoratorPlugin |