-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for classmethods and staticmethods in add_method (#13397)
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
- Loading branch information
Showing
4 changed files
with
114 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Callable, Optional | ||
|
||
from mypy.nodes import ARG_POS, Argument, Var | ||
from mypy.plugin import ClassDefContext, Plugin | ||
from mypy.plugins.common import add_method | ||
from mypy.types import NoneType | ||
|
||
|
||
class ClassMethodPlugin(Plugin): | ||
def get_base_class_hook(self, fullname: str) -> Optional[Callable[[ClassDefContext], None]]: | ||
if "BaseAddMethod" in fullname: | ||
return add_extra_methods_hook | ||
return None | ||
|
||
|
||
def add_extra_methods_hook(ctx: ClassDefContext) -> None: | ||
add_method(ctx, "foo_classmethod", [], NoneType(), is_classmethod=True) | ||
add_method( | ||
ctx, | ||
"foo_staticmethod", | ||
[Argument(Var(""), ctx.api.named_type("builtins.int"), None, ARG_POS)], | ||
ctx.api.named_type("builtins.str"), | ||
is_staticmethod=True, | ||
) | ||
|
||
|
||
def plugin(version): | ||
return ClassMethodPlugin |