-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerate_docs.py
More file actions
1485 lines (1263 loc) · 56.3 KB
/
generate_docs.py
File metadata and controls
1485 lines (1263 loc) · 56.3 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# mypy: ignore-errors
import ast
import re
import shutil
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Dict, List, Optional
from jinja2 import Template
from numpydoc.docscrape import FunctionDoc
@dataclass
class ParameterInfo:
name: str
type_annotation: str
description: str
default_value: Optional[str] = None
is_required: bool = True
type_info: Optional[Dict[str, Any]] = None
@dataclass
class ReturnInfo:
name: str
type: str
description: str
type_info: Optional[Dict[str, Any]] = None
@dataclass
class FunctionInfo:
name: str
signature: str
docstring: str
enhanced_description: str
parameters: List[ParameterInfo]
returns: List[ReturnInfo]
examples: List[str]
module: str
is_method: bool = False
@dataclass
class FieldInfo:
name: str
type_annotation: str
description: str
default_value: Optional[str] = None
is_required: bool = True
is_optional: bool = False
field_type: str = "string" # string, number, boolean, object, array
enum_values: Optional[List[str]] = None
nested_type: Optional[str] = None
type_info: Optional[Dict[str, Any]] = None
@dataclass
class TypeInfo:
name: str
docstring: str
enhanced_description: str
fields: Optional[List[FieldInfo]] = None
enum_values: Optional[List[Dict[str, str]]] = None
is_enum: bool = False
is_manifest: bool = False
module: str = ""
client_module: str = ""
base_classes: Optional[List[str]] = None
is_response_type: bool = False
is_input_type: bool = False
structure_example: str = ""
link_to_type: str = ""
@dataclass
class ClientModuleInfo:
name: str
display_name: str
description: str
methods: List[FunctionInfo]
class TrueFoundrySDKDocGenerator:
def __init__(self, sdk_path: str, output_path: str, docs_path: str):
self.sdk_path = Path(sdk_path)
self.output_path = Path(output_path)
self.types: Dict[str, TypeInfo] = {}
self.types_docs_path = f"{docs_path}/types"
self.enums_docs_path = f"{docs_path}/enums"
self.clients_docs_path = f"{docs_path}/"
# Define client modules and their categories
# Get all directories from sdk_path as module names
module_dirs = [
item.name
for item in self.sdk_path.iterdir()
if item.is_dir() and not item.name.startswith("_") and not item.name in ["core", "errors", "types"]
]
self.client_modules = {
module_name: {
"display_name": module_name.replace("_", " ").title(),
"description": f"Manage {module_name.replace('_', ' ')}",
}
for module_name in module_dirs
}
def extract_all_documentation(self) -> Dict[str, ClientModuleInfo]:
client_modules = {}
# Discover all types dynamically
all_types = self._discover_all_types()
self.types = all_types
print(f"Discovered {len(all_types)} types")
# Extract main client documentation
main_client_info = self._extract_main_client_info()
client_modules["main_client"] = main_client_info
# Extract each client module
for module_name, module_config in self.client_modules.items():
module_info = self._extract_client_module_info(module_name, module_config, all_types)
if module_info:
client_modules[module_name] = module_info
return client_modules
def _is_property_method(self, node: ast.FunctionDef) -> bool:
"""Check if a function is decorated with @property"""
for decorator in node.decorator_list:
if isinstance(decorator, ast.Name) and decorator.id == "property":
return True
elif isinstance(decorator, ast.Attribute) and decorator.attr == "property":
return True
return False
def _extract_main_client_info(self) -> ClientModuleInfo:
# Read the main client file
client_file = self.sdk_path / "client.py"
with open(client_file, "r", encoding="utf-8") as f:
content = f.read()
tree = ast.parse(content)
base_client_file = self.sdk_path / "base_client.py"
with open(base_client_file, "r", encoding="utf-8") as f:
base_client_content = f.read()
base_tree = ast.parse(base_client_content)
# Extract methods from TrueFoundry class
methods = []
methods_map = {}
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef) and node.name == "TrueFoundry":
for child in node.body:
if isinstance(child, ast.FunctionDef) and not child.name.startswith("_"):
# Skip @property methods
if self._is_property_method(child):
continue
method_info = self._extract_function_info(child, "client", is_method=True, all_types=self.types)
if method_info and method_info.name not in methods_map:
methods.append(method_info)
methods_map[method_info.name] = method_info
break
for node in ast.walk(base_tree):
if isinstance(node, ast.ClassDef) and node.name == "BaseTrueFoundry":
for child in node.body:
if isinstance(child, ast.FunctionDef) and not child.name.startswith("_"):
# Skip @property methods
if self._is_property_method(child):
continue
method_info = self._extract_function_info(child, "client", is_method=True, all_types=self.types)
if method_info and method_info.name not in methods_map:
methods.append(method_info)
methods_map[method_info.name] = method_info
return ClientModuleInfo(
name="main_client",
display_name="TrueFoundry Client",
description="Main client for TrueFoundry SDK operations",
methods=methods,
)
def _extract_client_module_info(
self, module_name: str, module_config: Dict, all_types: Dict[str, TypeInfo]
) -> Optional[ClientModuleInfo]:
"""Extract documentation for a specific client module"""
# Read the client file
client_file = self.sdk_path / module_name / "client.py"
if not client_file.exists():
return None
with open(client_file, "r", encoding="utf-8") as f:
content = f.read()
tree = ast.parse(content)
# Extract methods from the main client class (not Async)
methods = []
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef) and not node.name.startswith("Async"):
for child in node.body:
if isinstance(child, ast.FunctionDef) and not child.name.startswith("_"):
method_info = self._extract_function_info(
child, f"{module_name}.client", is_method=True, all_types=all_types
)
if method_info:
methods.append(method_info)
break
# Also extract get_by_fqn methods from wrapped clients if they exist
wrapped_methods = self._extract_wrapped_client_methods(module_name, all_types)
methods.extend(wrapped_methods)
return ClientModuleInfo(
name=module_name,
display_name=module_config["display_name"],
description=module_config["description"],
methods=methods,
)
def _extract_wrapped_client_methods(self, module_name: str, all_types: Dict[str, TypeInfo]) -> List[FunctionInfo]:
"""Extract get_by_fqn methods from wrapped clients"""
methods = []
# Read the wrapped clients file
wrapped_clients_file = self.sdk_path / "_wrapped_clients.py"
if not wrapped_clients_file.exists():
return methods
try:
with open(wrapped_clients_file, "r", encoding="utf-8") as f:
content = f.read()
tree = ast.parse(content)
# Look for the wrapped client class for this module
wrapped_class_name = f"Wrapped{module_name.replace('_', ' ').title().replace(' ', '')}Client"
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef) and node.name == wrapped_class_name:
for child in node.body:
if isinstance(child, ast.FunctionDef) and child.name == "get_by_fqn":
method_info = self._extract_function_info(
child, f"{module_name}.client", is_method=True, all_types=all_types
)
if method_info:
methods.append(method_info)
break
except Exception as e:
print(f"Error extracting wrapped client methods for {module_name}: {e}")
return methods
def _extract_type_info(
self, node: ast.ClassDef, module_name: str, client_module: str, all_types: Dict[str, TypeInfo]
) -> Optional[TypeInfo]:
"""Extract detailed information about a type/class"""
try:
# Get docstring
docstring = ast.get_docstring(node) or ""
# Get base classes
base_classes = []
for base in node.bases:
if isinstance(base, ast.Name):
base_classes.append(base.id)
elif isinstance(base, ast.Attribute):
if hasattr(base.value, "id") and isinstance(base.value, ast.Name):
base_classes.append(f"{base.value.id}.{base.attr}")
else:
base_classes.append(base.attr)
# Check if it's an enum
is_enum = any("enum" in base.lower() for base in base_classes)
# Only process enums or types with user-settable values
if not is_enum:
return None
# Extract enum values
enum_values = []
for child in node.body:
if isinstance(child, ast.Assign):
for target in child.targets:
if isinstance(target, ast.Name):
attr_name = target.id
attr_value = self._get_node_value(child.value)
if attr_value:
enum_values.append({"name": attr_name, "value": attr_value, "description": ""})
return TypeInfo(
name=node.name,
docstring=docstring,
enhanced_description="",
enum_values=enum_values if enum_values else None,
is_enum=is_enum,
module=module_name,
client_module=client_module,
link_to_type=self._get_link_to_type(node.name, is_enum),
)
except Exception as e:
print(f"Error extracting type {node.name}: {e}")
return None
def _get_link_to_type(self, type_name: str, is_enum: bool = False) -> str:
"""Get the link to a type"""
if is_enum:
return f"{self.enums_docs_path}#{type_name.lower()}"
else:
return f"{self.types_docs_path}#{type_name.lower()}"
def _extract_function_info(
self,
node: ast.FunctionDef,
module_name: str,
is_method: bool = False,
all_types: Optional[Dict[str, TypeInfo]] = None,
) -> Optional[FunctionInfo]:
"""Extract detailed information about a function using numpydoc"""
try:
# Skip __init__ methods and with_raw_response methods
if node.name == "__init__" or node.name == "with_raw_response":
return None
# Get docstring
docstring = ast.get_docstring(node) or ""
# Create a mock function object for numpydoc
class MockFunction:
def __init__(self, name, docstring, signature):
self.__name__ = name
self.__doc__ = docstring
self.__signature__ = signature
# Get function signature
signature = self._create_signature_from_node(node)
mock_func = MockFunction(node.name, docstring, signature)
# Parse with numpydoc
try:
doc = FunctionDoc(mock_func)
# Extract parameters
parameters = []
for param in doc["Parameters"]:
if param.name not in ["request_options"]: # Skip internal parameters
# Extract type information and create links
type_info = self._extract_type_info_from_param(param.type, all_types or {})
param_info = ParameterInfo(
name=param.name,
type_annotation=param.type,
description="\n".join(param.desc),
is_required="Optional" not in param.type,
type_info=type_info,
)
parameters.append(param_info)
# Extract returns
returns = []
for ret in doc["Returns"]:
return_info = ReturnInfo(
name=ret.type,
type=ret.type,
description="\n".join(ret.desc),
type_info=self._extract_type_info_from_param(ret.type, all_types or {}),
)
returns.append(return_info)
# Generate proper Python examples based on method signature
examples = []
# Create a proper Python example
example_lines = [
"from truefoundry import TrueFoundry",
"",
"client = TrueFoundry(",
' api_key="YOUR_API_KEY",',
' base_url="https://yourhost.com/path/to/api",',
")",
"",
]
# Add method call based on parameters
# For main client methods, use client.method_name
# For module methods, use client.module_name.method_name
if module_name == "client":
method_call = f"client.{node.name}("
else:
# Extract module name from module_name (e.g., "ml_repos.client" -> "ml_repos")
module_part = module_name.split(".")[0]
method_call = f"client.{module_part}.{node.name}("
# Add parameters based on what we have
param_lines = []
for param in parameters:
if param.name == "manifest":
param_lines.append(f' {param.name}={{"key": "value"}},')
elif param.name in ["id", "application_id", "cluster_id", "workspace_id"]:
param_lines.append(f' {param.name}="{param.name}_value",')
elif param.name in ["limit", "offset"]:
param_lines.append(f" {param.name}=10,")
elif param.name in ["dry_run", "force_deploy", "trigger_on_deploy"]:
param_lines.append(f" {param.name}=False,")
else:
param_lines.append(f' {param.name}="value",')
if param_lines:
method_call += "\n" + "\n".join(param_lines)
method_call += "\n)"
example_lines.append(method_call)
# Add response handling if it's a list method
if node.name == "list":
example_lines.extend(
[
"",
"# Iterate through results",
"for item in response:",
" print(item.name)",
"",
"# Or paginate page by page",
"for page in response.iter_pages():",
" for item in page:",
" print(item.name)",
]
)
examples.append("\n".join(example_lines))
# Get signature from AST
signature = self._create_signature_from_node(node)
# Clean up the enhanced description to avoid MDX parsing issues
summary_lines = doc.get("Summary", [])
enhanced_description = ""
if summary_lines:
# Only take the first line of summary to avoid raw docstring content
enhanced_description = summary_lines[0].strip()
return FunctionInfo(
name=node.name,
signature=signature,
docstring=docstring,
enhanced_description=enhanced_description,
parameters=parameters,
returns=returns,
examples=examples,
module=module_name,
is_method=is_method,
)
except Exception as e:
# Fallback to basic extraction if numpydoc fails
print(f"Warning: numpydoc failed for {node.name}, using fallback: {e}")
# return self._extract_function_info_fallback(node, module_name, is_method, all_types)
return None
except Exception as e:
print(f"Error extracting function {node.name}: {e}")
return None
def _get_type_annotation(self, annotation) -> str:
"""Extract type annotation as string"""
if annotation is None:
return "Any"
if isinstance(annotation, ast.Name):
return annotation.id
elif isinstance(annotation, ast.Attribute):
if hasattr(annotation.value, "id") and isinstance(annotation.value, ast.Name):
return f"{annotation.value.id}.{annotation.attr}"
else:
return annotation.attr
elif isinstance(annotation, ast.Subscript):
# Handle generic types like List[str], Dict[str, int]
value = self._get_type_annotation(annotation.value)
slice_value = self._get_type_annotation(annotation.slice)
return f"{value}[{slice_value}]"
elif isinstance(annotation, ast.Tuple):
# Handle tuple types
elements = [self._get_type_annotation(el) for el in annotation.elts]
return f"({', '.join(elements)})"
else:
return "Any"
def _get_node_value(self, node) -> str:
"""Get string representation of a node value"""
if isinstance(node, ast.Constant):
# For strings, just return the value without extra quotes
if isinstance(node.value, str):
return node.value
else:
return repr(node.value)
elif isinstance(node, ast.Name):
return node.id
elif isinstance(node, ast.Attribute):
if hasattr(node.value, "id") and isinstance(node.value, ast.Name):
return f"{node.value.id}.{node.attr}"
else:
return node.attr
else:
return "..."
def _create_signature_from_node(self, node: ast.FunctionDef) -> str:
"""Create a readable function signature from AST node"""
params = []
for arg in node.args.args:
if arg.arg == "self":
continue
param_str = arg.arg
if arg.annotation:
param_str += f": {self._get_type_annotation(arg.annotation)}"
params.append(param_str)
# Handle defaults
defaults = node.args.defaults
if defaults:
num_defaults = len(defaults)
num_params = len(params)
for i, default in enumerate(defaults):
param_index = num_params - num_defaults + i
if param_index < len(params):
default_value = self._get_node_value(default)
params[param_index] += f" = {default_value}"
return f"{node.name}({', '.join(params)})"
def _extract_type_info_from_param(self, type_str: str, all_types: Dict[str, TypeInfo]) -> Optional[Dict[str, Any]]:
for type_name, type_info in all_types.items():
if type_name == type_str:
link = self._get_link_to_type(type_info.name, type_info.is_enum)
return {"name": type_info.name, "description": type_info.enhanced_description or "", "link": link}
best_match = None
best_match_length = 0
for type_name, type_info in all_types.items():
# Check if type_name is a complete word within type_str
# This prevents "ApplyMlEntityResponse" from matching "ApplyMlEntityResponseData"
if type_name in type_str:
# Prefer longer type names as they're more specific
if len(type_name) > best_match_length:
best_match = type_info
best_match_length = len(type_name)
if best_match:
link = self._get_link_to_type(best_match.name, best_match.is_enum)
return {"name": best_match.name, "description": best_match.enhanced_description or "", "link": link}
return None
def _is_manifest_relevant_to_module(self, manifest_type_name: str, module_name: str) -> bool:
base_name = manifest_type_name.replace("Manifest", "").lower()
module_name_lower = module_name.lower()
# Direct match
if base_name == module_name_lower:
return True
# Check for partial matches
if module_name_lower in base_name or base_name in module_name_lower:
return True
# Special cases for compound names
if module_name_lower == "applications" and "application" in base_name:
return True
elif module_name_lower == "data_directories" and "datadirectory" in base_name:
return True
elif module_name_lower == "personal_access_tokens" and "personalaccesstoken" in base_name:
return True
elif module_name_lower == "virtual_accounts" and "virtualaccount" in base_name:
return True
elif module_name_lower == "tracing_projects" and "tracingproject" in base_name:
return True
return False
def _discover_all_types(self) -> Dict[str, TypeInfo]:
"""Dynamically discover all types from the types directory"""
all_types = {}
# Discover types from main types directory
main_types_dir = self.sdk_path / "types"
if main_types_dir.exists():
for type_file in main_types_dir.glob("*.py"):
if type_file.name.startswith("_"):
continue
types_from_file = self._extract_types_from_file(type_file, "types")
all_types.update(types_from_file)
# Discover types from client-specific type directories
for module_name in self.client_modules.keys():
types_dir = self.sdk_path / module_name / "types"
if types_dir.exists():
for type_file in types_dir.glob("*.py"):
if type_file.name.startswith("_"):
continue
types_from_file = self._extract_types_from_file(type_file, f"{module_name}.types")
all_types.update(types_from_file)
return all_types
def _extract_types_from_file(self, type_file: Path, module_name: str) -> Dict[str, TypeInfo]:
types = {}
try:
with open(type_file, "r", encoding="utf-8") as f:
content = f.read()
tree = ast.parse(content)
for node in ast.walk(tree):
if isinstance(node, ast.ClassDef):
type_info = self._extract_detailed_type_info(node, f"{module_name}.{type_file.stem}")
if type_info:
types[type_info.name] = type_info
elif isinstance(node, ast.Assign):
# Handle type aliases like TypeName = typing.Union[...]
for target in node.targets:
if isinstance(target, ast.Name):
type_name = target.id
if isinstance(node.value, ast.Attribute) and node.value.attr == "Union":
# This is a typing.Union type alias
type_info = self._extract_union_type_info(
type_name, node.value, f"{module_name}.{type_file.stem}"
)
if type_info:
types[type_info.name] = type_info
elif (
isinstance(node.value, ast.Subscript)
and isinstance(node.value.value, ast.Attribute)
and node.value.value.attr == "Union"
):
# This is a typing.Union[...] type alias
type_info = self._extract_union_type_info(
type_name, node.value, f"{module_name}.{type_file.stem}"
)
if type_info:
types[type_info.name] = type_info
except Exception as e:
print(f"Error extracting types from {type_file}: {e}")
return types
def _extract_union_type_info(self, type_name: str, union_node, module_name: str) -> Optional[TypeInfo]:
"""Extract information about a union type alias"""
try:
# Get the union types
union_types = []
if isinstance(union_node, ast.Subscript):
# Handle typing.Union[Type1, Type2, ...]
if isinstance(union_node.slice, ast.Tuple):
for elt in union_node.slice.elts:
if isinstance(elt, ast.Name):
union_types.append(elt.id)
elif isinstance(elt, ast.Attribute):
union_types.append(f"{elt.value.id}.{elt.attr}" if hasattr(elt.value, "id") else elt.attr)
else:
# Single type in Union
if isinstance(union_node.slice, ast.Name):
union_types.append(union_node.slice.id)
elif isinstance(union_node.slice, ast.Attribute):
union_types.append(
f"{union_node.slice.value.id}.{union_node.slice.attr}"
if hasattr(union_node.slice.value, "id")
else union_node.slice.attr
)
# Create a description for the union type
description = ""
return TypeInfo(
name=type_name,
docstring=description,
enhanced_description=description,
fields=None,
enum_values=None,
is_enum=False,
is_manifest="Manifest" in type_name,
module=module_name,
base_classes=union_types,
is_response_type=any(
word in type_name.lower() for word in ["response", "get", "list", "create", "update", "delete"]
),
is_input_type=any(word in type_name.lower() for word in ["request", "input", "manifest"]),
structure_example=f"One of: {', '.join(union_types)}",
link_to_type=self._get_link_to_type(type_name, False),
)
except Exception as e:
print(f"Error extracting union type {type_name}: {e}")
return None
def _extract_detailed_type_info(self, node: ast.ClassDef, module_name: str) -> Optional[TypeInfo]:
try:
# Get docstring
docstring = ast.get_docstring(node) or ""
# Clean up the docstring
cleaned_docstring = self._clean_description(docstring)
# Get base classes
base_classes = []
for base in node.bases:
if isinstance(base, ast.Name):
base_classes.append(base.id)
elif isinstance(base, ast.Attribute):
if hasattr(base.value, "id") and isinstance(base.value, ast.Name):
base_classes.append(f"{base.value.id}.{base.attr}")
else:
base_classes.append(base.attr)
# Check if it's an enum
is_enum = any("enum" in base.lower() for base in base_classes)
# Check if it's a manifest type
is_manifest = "Manifest" in node.name
# Check if it's a response type
is_response_type = any(
word in node.name.lower() for word in ["response", "get", "list", "create", "update", "delete"]
)
# Check if it's an input type
is_input_type = any(word in node.name.lower() for word in ["request", "input", "manifest"])
# Extract fields
fields = []
enum_values = []
if is_enum:
# Extract enum values
for child in node.body:
if isinstance(child, ast.Assign):
for target in child.targets:
if isinstance(target, ast.Name):
attr_name = target.id
attr_value = self._get_node_value(child.value)
if attr_value:
enum_values.append({"name": attr_name, "value": attr_value, "description": ""})
else:
# Extract fields from class attributes
for child in node.body:
if isinstance(child, ast.AnnAssign) and child.target:
field_info = self._extract_field_info(child, node)
if field_info:
fields.append(field_info)
# Generate a structure example for the type
structure_example = self._generate_structure_example(fields)
return TypeInfo(
name=node.name,
docstring=cleaned_docstring,
enhanced_description=cleaned_docstring.split(".")[0] if cleaned_docstring else "",
fields=fields if fields else None,
enum_values=enum_values if enum_values else None,
is_enum=is_enum,
is_manifest=is_manifest,
module=module_name,
base_classes=base_classes,
is_response_type=is_response_type,
is_input_type=is_input_type,
structure_example=structure_example,
link_to_type=self._get_link_to_type(node.name, is_enum),
)
except Exception as e:
print(f"Error extracting type {node.name}: {e}")
return None
def _is_global_constant(self, node: ast.Assign) -> bool:
"""Check if an assignment node represents a global constant (uppercase screaming ones)"""
try:
# Check if all targets are uppercase names (global constants)
for target in node.targets:
if isinstance(target, ast.Name):
# If the target name is all uppercase, it's likely a global constant
if target.id.isupper():
return True
elif isinstance(target, ast.Tuple):
# Check all elements in a tuple assignment
for elt in target.elts:
if isinstance(elt, ast.Name) and elt.id.isupper():
return True
return False
except Exception:
return False
def _extract_type_alias_info(self, node: ast.Assign, module_name: str) -> Optional[TypeInfo]:
"""Extract information about a type alias (like Union types)"""
try:
# Check if this is a type alias assignment
if len(node.targets) == 1 and isinstance(node.targets[0], ast.Name):
type_name = node.targets[0].id
# Get the type annotation
type_annotation = self._get_type_annotation(node.value)
# Create a basic TypeInfo for the type alias
return TypeInfo(
name=type_name,
docstring="", # Type aliases typically don't have docstrings
enhanced_description=f"Type alias: {type_annotation}",
fields=None, # Type aliases don't have fields
enum_values=None,
is_enum=False,
is_manifest=False,
module=module_name,
base_classes=None,
is_response_type=False,
is_input_type=False,
structure_example=type_annotation,
link_to_type=self._get_link_to_type(type_name, False),
)
except Exception as e:
print(f"Error extracting type alias info: {e}")
return None
def _extract_default_value_from_field(self, node) -> Optional[str]:
"""Extract default value from pydantic.Field call, filtering out unnecessary defaults"""
if not node:
return None
# If it's a pydantic.Field call, extract the default argument
if isinstance(node, ast.Call) and hasattr(node, 'keywords'):
for keyword in node.keywords:
if keyword.arg == "default":
default_value = self._get_node_value(keyword.value)
# Filter out unnecessary defaults
if self._is_meaningful_default(default_value):
return default_value
return None
return None
else:
# Direct assignment
default_value = self._get_node_value(node)
if self._is_meaningful_default(default_value):
return default_value
return None
def _is_meaningful_default(self, default_value: str) -> bool:
"""Check if a default value is meaningful enough to display"""
if not default_value:
return False
# Remove quotes for string comparison
clean_value = default_value.strip("'\"")
# Filter out common unmeaningful defaults
unmeaningful_defaults = {
"None", "False", "True", "0", "", "[]", "{}", "()",
"pydantic.Field(default=None)", "pydantic.Field(default=False)",
"pydantic.Field(default=True)", "pydantic.Field(default=0)",
"pydantic.Field(default='')", "pydantic.Field(default=[])",
"pydantic.Field(default={})", "pydantic.Field(default=())"
}
return default_value not in unmeaningful_defaults and clean_value not in unmeaningful_defaults
def _extract_field_info(self, node: ast.AnnAssign, class_node: ast.ClassDef) -> Optional[FieldInfo]:
"""Extract detailed information about a field"""
try:
if not isinstance(node.target, ast.Name):
return None
field_name = node.target.id
type_annotation = self._get_type_annotation(node.annotation)
# Get field description from the docstring that follows the field
description = ""
# Look for docstring after this field assignment
if node.value and hasattr(node.value, "keywords") and isinstance(node.value, ast.Call):
for keyword in node.value.keywords:
if keyword.arg == "description":
raw_description = self._get_node_value(keyword.value)
description = self._clean_description(raw_description)
break
# If no description found in pydantic.Field, look for docstring in the class
if not description:
# Find the docstring that follows this field assignment
for i, child in enumerate(class_node.body):
if child == node and i + 1 < len(class_node.body):
next_child = class_node.body[i + 1]
if isinstance(next_child, ast.Expr) and isinstance(next_child.value, ast.Constant):
docstring = next_child.value.value
if isinstance(docstring, str):
description = self._clean_description(docstring)
break
# Determine field properties
is_optional = "Optional" in type_annotation or "typing.Optional" in type_annotation
is_required = not is_optional
# Determine field type
field_type = self._determine_field_type(type_annotation)
# Extract nested type information
nested_type = self._extract_nested_type(type_annotation)
# Get default value - only meaningful ones
default_value = self._extract_default_value_from_field(node.value)
return FieldInfo(
name=field_name,
type_annotation=type_annotation,
description=re.sub(r"([<>\{\}])", r"\\\1", description),
default_value=default_value,
is_required=is_required,
is_optional=is_optional,
field_type=field_type,
enum_values=[],
nested_type=nested_type,
)
except Exception as e:
print(f"Error extracting field info: {e}")
return None
def _add_type_info_to_field(self, field: FieldInfo, all_types: Dict[str, TypeInfo]) -> FieldInfo:
"""Add type information to a field"""
type_info = self._extract_type_info_from_param(
field.nested_type if field.nested_type else field.type_annotation, all_types
)
if type_info:
field.type_info = type_info
return field
def _clean_description(self, description: str) -> str:
"""Clean up description by removing metadata and formatting"""
if not description:
return ""
# Remove metadata lines that start with +
lines = description.split("\n")
cleaned_lines = []
for line in lines:
line = line.strip()
# Skip metadata lines
if line.startswith("+"):
continue
# Skip empty lines
if not line:
continue
# Skip lines that are just metadata
if (
line.startswith("+label=")
or line.startswith("+icon=")
or line.startswith("+sort=")
or line.startswith("+usage=")
or line.startswith("+message=")
or line.startswith("+placeholder=")
or line.startswith("+uiType=")
or line.startswith("+uiProps=")
):
continue
# Skip lines that contain only metadata patterns
if any(
pattern in line
for pattern in [
"+label=",
"+icon=",
"+sort=",
"+usage=",
"+message=",
"+placeholder=",
"+uiType=",
"+uiProps=",
]
):
continue
cleaned_lines.append(line)
result = " ".join(cleaned_lines)
# Remove any remaining metadata patterns
import re
result = re.sub(r"\+[a-zA-Z_]+=[^,\s]+", "", result)
result = re.sub(r"\+[a-zA-Z_]+=\{[^}]+\}", "", result)
# Clean up extra whitespace