File size: 2,667 Bytes
a5784e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""MonkeyType configuration for AIStudioProxyAPI.

This config:
- Filters to only trace project modules (exclude tests, external libs)
- Enables TypedDict generation for config dicts
- Applies type rewriters to clean up messy unions
- Increases query limit for comprehensive coverage
"""

from monkeytype.config import DefaultConfig
from monkeytype.typing import (
    ChainedRewriter,
    RemoveEmptyContainers,
    RewriteConfigDict,
    RewriteLargeUnion,
)


class AIStudioProxyConfig(DefaultConfig):
    """Custom MonkeyType configuration for this project."""

    def code_filter(self):
        """Only trace project modules, exclude tests and external libraries."""

        def should_trace(code):
            # Normalize Windows path separators
            filename = code.co_filename.replace("\\", "/")

            # Project modules to trace
            project_modules = [
                "api_utils",
                "browser_utils",
                "stream",
                "config",
                "models",
                "launcher",
                "logging_utils",
            ]

            # Check if file is in any project module
            for module in project_modules:
                if f"/{module}/" in filename or filename.endswith(f"/{module}.py"):
                    # Exclude test files
                    if "/tests/" not in filename and "/test_" not in filename:
                        return True

            return False

        return should_trace

    def type_rewriter(self):
        """Clean up generated types with chained rewriters."""
        return ChainedRewriter(
            [
                RemoveEmptyContainers(),  # Union[List[Any], List[int]] -> List[int]
                RewriteConfigDict(),  # Union[Dict[K,V1], Dict[K,V2]] -> Dict[K, Union[V1,V2]]
                RewriteLargeUnion(
                    max_union_len=3
                ),  # Large unions -> Any (strict: max 3 elements)
            ]
        )

    def max_typed_dict_size(self) -> int:
        """Enable TypedDict generation for dictionaries.

        Since 19.11.2, TypedDict generation is disabled by default.
        This enables it for dicts with up to 50 keys, which is critical
        for config.settings and similar modules.
        """
        return 50

    def query_limit(self) -> int:
        """Increase query limit for comprehensive type inference.

        Default is 2000. We increase to 5000 to capture more traces
        and improve type accuracy, especially for polymorphic functions.
        """
        return 5000


# MonkeyType will automatically find and use this CONFIG instance
CONFIG = AIStudioProxyConfig()