-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParameterTypes.py
137 lines (94 loc) · 3.52 KB
/
ParameterTypes.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# -*- coding: utf-8 -*-
# cython: language_level = 3
"""
处理函数调用参数类型
"""
import ast
from abc import ABC
class ABCParameter(ABC):
def __init__(self, name: str):
self.name = name
def __repr__(self):
return f"{self.__class__.__name__}({self.name=})"
class ABCDefaultParameter(ABCParameter):
def __init__(self, name: str, default):
super().__init__(name)
self.default = default
def __repr__(self):
return f"{self.__class__.__name__}({self.name=}, {self.default=})"
class ABCVariableLengthParameter(ABCParameter):
...
class ABCArgument(ABCParameter):
def __init__(self, name: str, pos_only: bool | None):
ABCParameter.__init__(self, name)
self.pos_only = pos_only
def __repr__(self):
return f"{self.__class__.__name__}({self.name=}, {self.pos_only=})"
class ABCKeyword(ABCParameter):
...
class ArgParameter(ABCArgument):
def __init__(self, name: str, pos_only: bool):
super().__init__(name, pos_only)
def __repr__(self):
return f"{self.__class__.__name__}({self.name=}, {self.pos_only=})"
class DefaultArgParameter(ABCArgument, ABCDefaultParameter):
def __init__(self, name: str, default, pos_only: bool):
ABCArgument.__init__(self, name, pos_only)
ABCDefaultParameter.__init__(self, name, default)
def __repr__(self):
return f"{self.__class__.__name__}({self.name=}, {self.default=}, {self.pos_only=})"
class VarArgParameter(ABCArgument, ABCVariableLengthParameter):
def __init__(self, name: str):
ABCArgument.__init__(self, name, None)
ABCVariableLengthParameter.__init__(self, name)
class KWParameter(ABCKeyword):
def __init__(self, name: str):
super().__init__(name)
def __repr__(self):
return f"{self.__class__.__name__}({self.name=})"
class DefaultKWParameter(ABCKeyword, ABCDefaultParameter):
def __init__(self, name: str, default):
ABCKeyword.__init__(self, name)
ABCDefaultParameter.__init__(self, name, default)
def __repr__(self):
return f"{self.__class__.__name__}({self.name=}, {self.default=})"
class VarKWParameter(ABCKeyword, ABCVariableLengthParameter):
def __init__(self, name: str):
super().__init__(name)
def parse_arguments(arg_node: ast.arguments):
arguments = []
for arg in arg_node.posonlyargs:
arguments.append(ArgParameter(arg.arg, True))
for arg in arg_node.args:
arguments.append(ArgParameter(arg.arg, False))
for i, default_arg in enumerate(arg_node.defaults[::-1], start=1):
arg_data = arguments[-i]
new_arg = DefaultArgParameter(arg_data.name, default_arg, arg_data.pos_only)
arguments[-i] = new_arg
if arg_node.vararg:
arguments.append(VarArgParameter(arg_node.vararg.arg))
for arg in arg_node.kwonlyargs:
arguments.append(KWParameter(arg.arg))
for i, default_arg in enumerate(arg_node.kw_defaults[::-1], start=1):
if default_arg is None:
continue
kw_data = arguments[-i]
new_arg = DefaultKWParameter(kw_data.name, default_arg)
arguments[-i] = new_arg
if arg_node.kwarg:
arguments.append(VarKWParameter(arg_node.kwarg.arg))
return arguments
__all__ = (
"ABCParameter",
"ABCDefaultParameter",
"ABCVariableLengthParameter",
"ABCArgument",
"ABCKeyword",
"ArgParameter",
"DefaultArgParameter",
"VarArgParameter",
"KWParameter",
"DefaultKWParameter",
"VarKWParameter",
"parse_arguments",
)