|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to verify all types in src/elevenlabs/types/ can be imported and instantiated. |
| 4 | +Allows pydantic ValidationError but catches other errors (like circular imports). |
| 5 | +Each type is tested in a separate Python process to avoid import cache issues. |
| 6 | +""" |
| 7 | +import subprocess |
| 8 | +import sys |
| 9 | +from pathlib import Path |
| 10 | +from concurrent.futures import ThreadPoolExecutor, as_completed |
| 11 | + |
| 12 | +# Color codes for terminal output |
| 13 | +RED = '\033[91m' |
| 14 | +GREEN = '\033[92m' |
| 15 | +YELLOW = '\033[93m' |
| 16 | +RESET = '\033[0m' |
| 17 | + |
| 18 | +def get_class_name_from_file(file_path: Path) -> str | None: |
| 19 | + """Convert a file name to the expected class name using PascalCase.""" |
| 20 | + # Remove .py extension |
| 21 | + name = file_path.stem |
| 22 | + |
| 23 | + # Skip special files |
| 24 | + if name.startswith('__'): |
| 25 | + return None |
| 26 | + |
| 27 | + # Convert snake_case to PascalCase |
| 28 | + parts = name.split('_') |
| 29 | + class_name = ''.join(word.capitalize() for word in parts) |
| 30 | + |
| 31 | + return class_name |
| 32 | + |
| 33 | +def test_type_import_subprocess(module_name: str, class_name: str) -> tuple[bool, str | None]: |
| 34 | + """ |
| 35 | + Test importing and instantiating a type in a separate subprocess. |
| 36 | + Returns (success, error_message) |
| 37 | + """ |
| 38 | + # Python code to test import in isolation |
| 39 | + test_code = f""" |
| 40 | +import sys |
| 41 | +from pydantic import ValidationError as PydanticValidationError |
| 42 | +
|
| 43 | +try: |
| 44 | + # Try to import the type |
| 45 | + from elevenlabs.types.{module_name} import {class_name} |
| 46 | +
|
| 47 | + # Try to instantiate with no args (expect ValidationError) |
| 48 | + try: |
| 49 | + {class_name}() |
| 50 | + except PydanticValidationError: |
| 51 | + pass # Expected - type requires arguments |
| 52 | + except TypeError: |
| 53 | + pass # Some types might not be Pydantic models (enums, etc.) |
| 54 | + except Exception: |
| 55 | + pass # Still fine, as long as import worked |
| 56 | +
|
| 57 | + sys.exit(0) # Success |
| 58 | +
|
| 59 | +except ImportError as e: |
| 60 | + print(f"ImportError: {{e}}", file=sys.stderr) |
| 61 | + sys.exit(1) |
| 62 | +except Exception as e: |
| 63 | + print(f"{{type(e).__name__}}: {{e}}", file=sys.stderr) |
| 64 | + sys.exit(1) |
| 65 | +""" |
| 66 | + |
| 67 | + try: |
| 68 | + result = subprocess.run( |
| 69 | + ["poetry", "run", "python", "-c", test_code], |
| 70 | + capture_output=True, |
| 71 | + text=True, |
| 72 | + timeout=10, |
| 73 | + cwd="/Users/thomas/Projects/elevenlabs-python" |
| 74 | + ) |
| 75 | + |
| 76 | + if result.returncode == 0: |
| 77 | + return True, None |
| 78 | + else: |
| 79 | + error_msg = result.stderr.strip() if result.stderr else "Unknown error" |
| 80 | + return False, error_msg |
| 81 | + |
| 82 | + except subprocess.TimeoutExpired: |
| 83 | + return False, "Timeout: Import took longer than 10 seconds" |
| 84 | + except Exception as e: |
| 85 | + return False, f"Subprocess error: {str(e)}" |
| 86 | + |
| 87 | +def main(): |
| 88 | + types_dir = Path("src/elevenlabs/types") |
| 89 | + |
| 90 | + if not types_dir.exists(): |
| 91 | + print(f"{RED}Error: {types_dir} does not exist{RESET}") |
| 92 | + sys.exit(1) |
| 93 | + |
| 94 | + # Get all Python files |
| 95 | + py_files = sorted([f for f in types_dir.glob("*.py") if not f.name.startswith("__")]) |
| 96 | + |
| 97 | + print(f"Testing {len(py_files)} type files in separate processes...\n") |
| 98 | + print(f"This may take a few minutes...\n") |
| 99 | + |
| 100 | + failures = [] |
| 101 | + successes = [] |
| 102 | + skipped = [] |
| 103 | + |
| 104 | + # Test types in parallel for speed |
| 105 | + with ThreadPoolExecutor(max_workers=10) as executor: |
| 106 | + future_to_file = {} |
| 107 | + |
| 108 | + for py_file in py_files: |
| 109 | + module_name = py_file.stem |
| 110 | + class_name = get_class_name_from_file(py_file) |
| 111 | + |
| 112 | + if class_name is None: |
| 113 | + skipped.append(module_name) |
| 114 | + continue |
| 115 | + |
| 116 | + future = executor.submit(test_type_import_subprocess, module_name, class_name) |
| 117 | + future_to_file[future] = (module_name, class_name) |
| 118 | + |
| 119 | + for future in as_completed(future_to_file): |
| 120 | + module_name, class_name = future_to_file[future] |
| 121 | + try: |
| 122 | + success, error = future.result() |
| 123 | + |
| 124 | + if success: |
| 125 | + successes.append((module_name, class_name)) |
| 126 | + print(f"{GREEN}✓{RESET} {module_name}.{class_name}") |
| 127 | + else: |
| 128 | + failures.append((module_name, class_name, error)) |
| 129 | + print(f"{RED}✗{RESET} {module_name}.{class_name}") |
| 130 | + print(f" {RED}{error}{RESET}") |
| 131 | + except Exception as e: |
| 132 | + failures.append((module_name, class_name, f"Test execution failed: {str(e)}")) |
| 133 | + print(f"{RED}✗{RESET} {module_name}.{class_name}") |
| 134 | + print(f" {RED}Test execution failed: {str(e)}{RESET}") |
| 135 | + |
| 136 | + # Print summary |
| 137 | + print(f"\n{'='*80}") |
| 138 | + print(f"Summary:") |
| 139 | + print(f" {GREEN}Successful: {len(successes)}{RESET}") |
| 140 | + print(f" {RED}Failed: {len(failures)}{RESET}") |
| 141 | + print(f" {YELLOW}Skipped: {len(skipped)}{RESET}") |
| 142 | + |
| 143 | + if failures: |
| 144 | + print(f"\n{RED}Failed imports:{RESET}") |
| 145 | + for module_name, class_name, error in failures: |
| 146 | + print(f" - {module_name}.{class_name}: {error}") |
| 147 | + sys.exit(1) |
| 148 | + else: |
| 149 | + print(f"\n{GREEN}All types can be imported successfully!{RESET}") |
| 150 | + sys.exit(0) |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + main() |
0 commit comments