33from pathlib import Path
44
55TYPE_MAP = {
6+ # 基础类型
67 "int" : "integer" ,
78 "float" : "number" ,
89 "str" : "string" ,
910 "bool" : "boolean" ,
11+ "bytes" : "string" ,
12+ # 容器类型
1013 "dict" : "object" ,
14+ "Dict" : "object" ,
15+ "Union" : "object" ,
1116 "list" : "array" ,
17+ "List" : "array" ,
1218 "tuple" : "array" ,
19+ "Tuple" : "array" ,
1320 "set" : "array" ,
21+ "Set" : "array" ,
22+ # 特殊类型
23+ "None" : "null" ,
1424}
1525
26+ type_errors = []
27+
1628def parse_type (annotation ):
1729 """解析参数类型"""
18- if isinstance (annotation , ast .Name ):
19- return TYPE_MAP .get (annotation .id , "string" ), None , True # True=必填
30+ global type_errors
31+
32+ if annotation is None :
33+ type_errors .append ("缺少类型注解(必须显式指定参数类型)" )
34+ return "invalid" , None , True
35+
36+ elif isinstance (annotation , ast .Name ):
37+ if annotation .id in TYPE_MAP :
38+ return TYPE_MAP [annotation .id ], None , True
39+ else :
40+ type_errors .append (f"不支持的类型: { annotation .id } " )
41+ return "invalid" , None , True
42+
43+ elif isinstance (annotation , ast .Constant ) and annotation .value is None :
44+ return "null" , None , False
2045
2146 elif isinstance (annotation , ast .Subscript ):
2247 if isinstance (annotation .value , ast .Name ):
2348 container = annotation .value .id
2449
25- # List[int] / list[str]
50+ # List[int]
2651 if container in ("list" , "List" ):
2752 item_type , _ , _ = parse_type (annotation .slice )
53+ if item_type == "invalid" :
54+ type_errors .append (f"不支持的列表元素类型: { annotation .slice } " )
55+ return "invalid" , None , True
2856 return "array" , {"type" : item_type }, True
2957
3058 # Dict[str, int] → object
@@ -34,13 +62,48 @@ def parse_type(annotation):
3462 # Optional[int]
3563 elif container == "Optional" :
3664 inner_type , inner_items , _ = parse_type (annotation .slice )
65+ if inner_type == "invalid" :
66+ type_errors .append (f"不支持的Optional类型: { annotation .slice } " )
67+ return "invalid" , None , False
3768 return inner_type , inner_items , False
38-
39- # Union[str, int] → 简化为 string
69+
70+ # Union[str, int]
4071 elif container == "Union" :
41- return "string" , None , True
42-
43- return "string" , None , True
72+ return "object" , None , True
73+
74+ # Tuple[str]
75+ elif container in ("tuple" , "Tuple" ):
76+ items = []
77+ if isinstance (annotation .slice , ast .Tuple ):
78+ for elt in annotation .slice .elts :
79+ item_type , _ , _ = parse_type (elt )
80+ if item_type == "invalid" :
81+ type_errors .append (f"不支持的元组元素类型: { ast .dump (elt )} " )
82+ return "invalid" , None , True
83+ items .append ({"type" :item_type })
84+ return "array" , f"{ items } " , True
85+ else :
86+ item_type , _ , _ = parse_type (annotation .slice )
87+ if item_type == "invalid" :
88+ type_errors .append (f"不支持的元组元素类型: { ast .dump (annotation .slice )} " )
89+ return "invalid" , None , True
90+ return "array" , {"type" :item_type }, True
91+
92+ # Set[int]
93+ elif container in ("set" , "Set" ):
94+ item_type , _ , _ = parse_type (annotation .slice )
95+ if item_type == "invalid" :
96+ type_errors .append (f"不支持的集合元素类型: { annotation .slice } " )
97+ return "invalid" , None , True
98+ return "array" , {"type" : item_type }, True
99+
100+
101+ else :
102+ type_errors .append (f"不支持的容器类型: { container } " )
103+ return "invalid" , None , True
104+
105+ type_errors .append (f"无法识别的类型: { ast .dump (annotation )} " )
106+ return "invalid" , None , True
44107
45108
46109def parse_parameters (args ):
@@ -52,11 +115,7 @@ def parse_parameters(args):
52115 for arg in args .args :
53116 arg_name = arg .arg
54117 order .append (arg_name )
55- arg_type = "string"
56- items = None
57- is_required = True
58- if arg .annotation :
59- arg_type , items , is_required = parse_type (arg .annotation )
118+ arg_type , items , is_required = parse_type (arg .annotation )
60119 # 定义参数
61120 prop_def = {
62121 "defaultValue" : "" ,
@@ -160,7 +219,7 @@ def parse_python_file(file_path: Path):
160219 "order" : order ,
161220 "return" : {
162221 "name" : "" ,
163- "description" : description or f"{ func_name } 的返回值 " ,
222+ "description" : f"{ func_name } 函数的返回值 " ,
164223 "type" : return_schema ["type" ],
165224 ** ({"items" : return_schema ["items" ]} if "items" in return_schema else {}),
166225 "convertor" : "" ,
0 commit comments