forked from python/mypy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdescriptor.py
More file actions
34 lines (25 loc) · 910 Bytes
/
descriptor.py
File metadata and controls
34 lines (25 loc) · 910 Bytes
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
from mypy.plugin import Plugin
from mypy.types import NoneType, CallableType
class DescriptorPlugin(Plugin):
def get_method_hook(self, fullname):
if fullname == "__main__.Desc.__get__":
return get_hook
return None
def get_method_signature_hook(self, fullname):
if fullname == "__main__.Desc.__set__":
return set_hook
return None
def get_hook(ctx):
if isinstance(ctx.arg_types[0][0], NoneType):
return ctx.api.named_type("builtins.str")
return ctx.api.named_type("builtins.int")
def set_hook(ctx):
return CallableType(
[ctx.api.named_type("__main__.Cls"), ctx.api.named_type("builtins.int")],
ctx.default_signature.arg_kinds,
ctx.default_signature.arg_names,
ctx.default_signature.ret_type,
ctx.default_signature.fallback,
)
def plugin(version):
return DescriptorPlugin