-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy patharg_names.py
More file actions
51 lines (40 loc) · 1.67 KB
/
arg_names.py
File metadata and controls
51 lines (40 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from __future__ import annotations
from typing import Callable
from mypy.nodes import StrExpr
from mypy.plugin import FunctionContext, MethodContext, Plugin
from mypy.types import Type
class ArgNamesPlugin(Plugin):
def get_function_hook(self, fullname: str) -> Callable[[FunctionContext], Type] | None:
if fullname in {
"mod.func",
"mod.func_unfilled",
"mod.func_star_expr",
"mod.ClassInit",
"mod.Outer.NestedClassInit",
}:
return extract_classname_and_set_as_return_type_function
return None
def get_method_hook(self, fullname: str) -> Callable[[MethodContext], Type] | None:
if fullname in {
"mod.Class.method",
"mod.Class.myclassmethod",
"mod.Class.mystaticmethod",
"mod.ClassUnfilled.method",
"mod.ClassStarExpr.method",
"mod.ClassChild.method",
"mod.ClassChild.myclassmethod",
}:
return extract_classname_and_set_as_return_type_method
return None
def extract_classname_and_set_as_return_type_function(ctx: FunctionContext) -> Type:
arg = ctx.args[ctx.callee_arg_names.index("classname")][0]
if not isinstance(arg, StrExpr):
return ctx.default_return_type
return ctx.api.named_generic_type(arg.value, [])
def extract_classname_and_set_as_return_type_method(ctx: MethodContext) -> Type:
arg = ctx.args[ctx.callee_arg_names.index("classname")][0]
if not isinstance(arg, StrExpr):
return ctx.default_return_type
return ctx.api.named_generic_type(arg.value, [])
def plugin(version: str) -> type[ArgNamesPlugin]:
return ArgNamesPlugin