|
| 1 | +"""MonkeyType configuration for AIStudioProxyAPI. |
| 2 | +
|
| 3 | +This config: |
| 4 | +- Filters to only trace project modules (exclude tests, external libs) |
| 5 | +- Enables TypedDict generation for config dicts |
| 6 | +- Applies type rewriters to clean up messy unions |
| 7 | +- Increases query limit for comprehensive coverage |
| 8 | +""" |
| 9 | + |
| 10 | +from monkeytype.config import DefaultConfig |
| 11 | +from monkeytype.typing import ( |
| 12 | + ChainedRewriter, |
| 13 | + RemoveEmptyContainers, |
| 14 | + RewriteConfigDict, |
| 15 | + RewriteLargeUnion, |
| 16 | +) |
| 17 | + |
| 18 | + |
| 19 | +class AIStudioProxyConfig(DefaultConfig): |
| 20 | + """Custom MonkeyType configuration for this project.""" |
| 21 | + |
| 22 | + def code_filter(self): |
| 23 | + """Only trace project modules, exclude tests and external libraries.""" |
| 24 | + |
| 25 | + def should_trace(code): |
| 26 | + # Normalize Windows path separators |
| 27 | + filename = code.co_filename.replace("\\", "/") |
| 28 | + |
| 29 | + # Project modules to trace |
| 30 | + project_modules = [ |
| 31 | + "api_utils", |
| 32 | + "browser_utils", |
| 33 | + "stream", |
| 34 | + "config", |
| 35 | + "models", |
| 36 | + "launcher", |
| 37 | + "logging_utils", |
| 38 | + ] |
| 39 | + |
| 40 | + # Check if file is in any project module |
| 41 | + for module in project_modules: |
| 42 | + if f"/{module}/" in filename or filename.endswith(f"/{module}.py"): |
| 43 | + # Exclude test files |
| 44 | + if "/tests/" not in filename and "/test_" not in filename: |
| 45 | + return True |
| 46 | + |
| 47 | + return False |
| 48 | + |
| 49 | + return should_trace |
| 50 | + |
| 51 | + def type_rewriter(self): |
| 52 | + """Clean up generated types with chained rewriters.""" |
| 53 | + return ChainedRewriter( |
| 54 | + [ |
| 55 | + RemoveEmptyContainers(), # Union[List[Any], List[int]] -> List[int] |
| 56 | + RewriteConfigDict(), # Union[Dict[K,V1], Dict[K,V2]] -> Dict[K, Union[V1,V2]] |
| 57 | + RewriteLargeUnion( |
| 58 | + max_union_len=3 |
| 59 | + ), # Large unions -> Any (strict: max 3 elements) |
| 60 | + ] |
| 61 | + ) |
| 62 | + |
| 63 | + def max_typed_dict_size(self) -> int: |
| 64 | + """Enable TypedDict generation for dictionaries. |
| 65 | +
|
| 66 | + Since 19.11.2, TypedDict generation is disabled by default. |
| 67 | + This enables it for dicts with up to 50 keys, which is critical |
| 68 | + for config.settings and similar modules. |
| 69 | + """ |
| 70 | + return 50 |
| 71 | + |
| 72 | + def query_limit(self) -> int: |
| 73 | + """Increase query limit for comprehensive type inference. |
| 74 | +
|
| 75 | + Default is 2000. We increase to 5000 to capture more traces |
| 76 | + and improve type accuracy, especially for polymorphic functions. |
| 77 | + """ |
| 78 | + return 5000 |
| 79 | + |
| 80 | + |
| 81 | +# MonkeyType will automatically find and use this CONFIG instance |
| 82 | +CONFIG = AIStudioProxyConfig() |
0 commit comments