Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…16038) There are several changes: 1. `add_overloaded_method_to_class` itself. It is very useful for plugin authors, because right now it is quite easy to add a regular method, but it is very hard to add a method with `@overload`s. I don't think that user must face all the chalenges that I've covered in this method. Moreover, it is quite easy even for experienced developers to forget some flags / props / etc (I am pretty sure that I might forgot something in the implementation) 2. `add_overloaded_method_to_class` and `add_method_to_class` now return added nodes, it is also helpful if you want to do something with this node in your plugin after it is created 3. I've refactored how `add_method_to_class` works and reused its parts in the new method as well 4. `tvar_def` in `add_method_to_class` can now accept a list of type vars, not just one Notice that `add_method_to_class` is unchanged from the user's POV, it should continue to work as before. Tests are also updated to check that our overloads are correct. Things to do later (in the next PRs / releases): 1. We can possibly add `is_final` param to methods as well 2. We can also support `@property` in a separate method at some point --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
ed18fea
commit 9a35360
Showing
5 changed files
with
222 additions
and
23 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Callable | ||
|
||
from mypy.nodes import ARG_POS, Argument, Var | ||
from mypy.plugin import ClassDefContext, Plugin | ||
from mypy.plugins.common import MethodSpec, add_overloaded_method_to_class | ||
|
||
|
||
class OverloadedMethodPlugin(Plugin): | ||
def get_base_class_hook(self, fullname: str) -> Callable[[ClassDefContext], None] | None: | ||
if "AddOverloadedMethod" in fullname: | ||
return add_overloaded_method_hook | ||
return None | ||
|
||
|
||
def add_overloaded_method_hook(ctx: ClassDefContext) -> None: | ||
add_overloaded_method_to_class(ctx.api, ctx.cls, "method", _generate_method_specs(ctx)) | ||
add_overloaded_method_to_class( | ||
ctx.api, ctx.cls, "clsmethod", _generate_method_specs(ctx), is_classmethod=True | ||
) | ||
add_overloaded_method_to_class( | ||
ctx.api, ctx.cls, "stmethod", _generate_method_specs(ctx), is_staticmethod=True | ||
) | ||
|
||
|
||
def _generate_method_specs(ctx: ClassDefContext) -> list[MethodSpec]: | ||
return [ | ||
MethodSpec( | ||
args=[Argument(Var("arg"), ctx.api.named_type("builtins.int"), None, ARG_POS)], | ||
return_type=ctx.api.named_type("builtins.str"), | ||
), | ||
MethodSpec( | ||
args=[Argument(Var("arg"), ctx.api.named_type("builtins.str"), None, ARG_POS)], | ||
return_type=ctx.api.named_type("builtins.int"), | ||
), | ||
] | ||
|
||
|
||
def plugin(version: str) -> type[OverloadedMethodPlugin]: | ||
return OverloadedMethodPlugin |