|
| 1 | +""" |
| 2 | +Mock Python 测试文件 - 完整版 |
| 3 | +用于验证 parser 的符号提取准确性 |
| 4 | +""" |
| 5 | + |
| 6 | +from typing import TypeAlias, List, Optional |
| 7 | +import os |
| 8 | +from pathlib import Path |
| 9 | + |
| 10 | +# ============================================ |
| 11 | +# 1. 顶层变量 (Top-level VAR) |
| 12 | +# ============================================ |
| 13 | + |
| 14 | +TOP_STRING = "hello" |
| 15 | +TOP_NUMBER = 42 |
| 16 | +TOP_LIST = [1, 2, 3] |
| 17 | + |
| 18 | +# 带类型注解的变量 |
| 19 | +TYPED_VAR: int = 100 |
| 20 | + |
| 21 | +# ============================================ |
| 22 | +# 2. 顶层类型别名 (Top-level TYPE) |
| 23 | +# ============================================ |
| 24 | + |
| 25 | +TopType1: TypeAlias = str |
| 26 | +TopType2: TypeAlias = int |
| 27 | +TopListType: TypeAlias = List[int] |
| 28 | +TopOptionalType: TypeAlias = Optional[str] |
| 29 | + |
| 30 | +# ============================================ |
| 31 | +# 3. 顶层函数 (Top-level FUNC) |
| 32 | +# ============================================ |
| 33 | + |
| 34 | +def top_func_no_params(): |
| 35 | + """顶层函数:无参数""" |
| 36 | + pass |
| 37 | + |
| 38 | +def top_func_with_params(a: int, b: str) -> bool: |
| 39 | + """顶层函数:有参数和返回值""" |
| 40 | + return True |
| 41 | + |
| 42 | +def top_func_calling(): |
| 43 | + """顶层函数:调用其他函数和类""" |
| 44 | + result = helper_func() |
| 45 | + obj = SymbolA() |
| 46 | + return result |
| 47 | + |
| 48 | +def helper_func(): |
| 49 | + """辅助函数""" |
| 50 | + return TOP_NUMBER |
| 51 | + |
| 52 | +def func_with_type_annotations(x: int, y: str = "default") -> List[int]: |
| 53 | + """带完整类型注解的函数""" |
| 54 | + return [x] |
| 55 | + |
| 56 | +# ============================================ |
| 57 | +# 4. 顶层类 (Top-level CLASS) |
| 58 | +# ============================================ |
| 59 | + |
| 60 | +class SymbolA: |
| 61 | + """顶层类""" |
| 62 | + value: str = "default" |
| 63 | + |
| 64 | + def get_value(self) -> str: |
| 65 | + return self.value |
| 66 | + |
| 67 | + |
| 68 | +# 继承示例 |
| 69 | +class ChildClass(SymbolA): |
| 70 | + """子类:继承 SymbolA""" |
| 71 | + |
| 72 | + child_attr: int = 10 |
| 73 | + |
| 74 | + def get_value(self) -> str: |
| 75 | + """方法覆盖""" |
| 76 | + return f"Child: {self.value}" |
| 77 | + |
| 78 | + |
| 79 | +# 多继承示例 |
| 80 | +class Mixin: |
| 81 | + def mixin_method(self): |
| 82 | + return "mixin" |
| 83 | + |
| 84 | + |
| 85 | +class MultiInherit(SymbolA, Mixin): |
| 86 | + """多继承类""" |
| 87 | + pass |
| 88 | + |
| 89 | + |
| 90 | +# ============================================ |
| 91 | +# 5. 导入语句(测试导入解析) |
| 92 | +# ============================================ |
| 93 | + |
| 94 | +# 标准导入 |
| 95 | +import sys |
| 96 | +import os as operating_system |
| 97 | + |
| 98 | +# from 导入 |
| 99 | +from typing import Dict, Tuple |
| 100 | + |
| 101 | +# 带别名的导入 |
| 102 | +from pathlib import Path as FilePath |
| 103 | + |
| 104 | + |
| 105 | +# ============================================ |
| 106 | +# 6. 装饰器(测试装饰器解析) |
| 107 | +# ============================================ |
| 108 | + |
| 109 | +class ClassWithDecorators: |
| 110 | + """带装饰器的类""" |
| 111 | + |
| 112 | + @property |
| 113 | + def prop(self) -> str: |
| 114 | + return "property" |
| 115 | + |
| 116 | + @staticmethod |
| 117 | + def static_method(): |
| 118 | + return "static" |
| 119 | + |
| 120 | + @classmethod |
| 121 | + def class_method(cls): |
| 122 | + return "class" |
| 123 | + |
| 124 | + |
| 125 | +# ============================================ |
| 126 | +# 7. 局部符号(在函数内) |
| 127 | +# ============================================ |
| 128 | + |
| 129 | +def func_with_locals(): |
| 130 | + """ |
| 131 | + 包含局部符号的函数 |
| 132 | + 这些不应该出现在 file_structure 中 |
| 133 | + """ |
| 134 | + |
| 135 | + # 局部变量 |
| 136 | + local_var_1 = 1 |
| 137 | + local_var_2: str = "local" |
| 138 | + |
| 139 | + # 局部类型别名 |
| 140 | + LocalType: TypeAlias = int |
| 141 | + |
| 142 | + # 局部函数 |
| 143 | + def local_func(): |
| 144 | + return local_var_1 |
| 145 | + |
| 146 | + # 局部类 |
| 147 | + class LocalClass: |
| 148 | + pass |
| 149 | + |
| 150 | + return local_func() |
| 151 | + |
| 152 | + |
| 153 | +def func_with_nested(): |
| 154 | + """包含嵌套局部符号的函数""" |
| 155 | + |
| 156 | + def nested_func(): |
| 157 | + nested_var = 100 |
| 158 | + return nested_var |
| 159 | + |
| 160 | + return nested_func() |
| 161 | + |
| 162 | + |
| 163 | +# ============================================ |
| 164 | +# 8. 控制流(测试复杂表达式) |
| 165 | +# ============================================ |
| 166 | + |
| 167 | +def func_with_control_flow(x: int) -> int: |
| 168 | + """包含控制流的函数""" |
| 169 | + if x > 0: |
| 170 | + return x |
| 171 | + else: |
| 172 | + return -x |
| 173 | + |
| 174 | + |
| 175 | +def func_with_loop(items: List[int]) -> int: |
| 176 | + """包含循环的函数""" |
| 177 | + total = 0 |
| 178 | + for item in items: |
| 179 | + total += item |
| 180 | + return total |
| 181 | + |
| 182 | + |
| 183 | +def func_with_exception() -> str: |
| 184 | + """包含异常处理的函数""" |
| 185 | + try: |
| 186 | + return "success" |
| 187 | + except Exception as e: |
| 188 | + return str(e) |
| 189 | + |
| 190 | + |
| 191 | +# ============================================ |
| 192 | +# 9. 类成员访问 |
| 193 | +# ============================================ |
| 194 | + |
| 195 | +def func_using_class_members(): |
| 196 | + """使用类成员""" |
| 197 | + obj = SymbolA() |
| 198 | + value = obj.get_value() # 方法调用 |
| 199 | + attr = obj.value # 属性访问 |
| 200 | + return value |
| 201 | + |
| 202 | + |
| 203 | +# ============================================ |
| 204 | +# 10. 异步函数 |
| 205 | +# ============================================ |
| 206 | + |
| 207 | +async def async_func(): |
| 208 | + """异步函数""" |
| 209 | + await helper_func() |
| 210 | + return "async" |
| 211 | + |
| 212 | + |
| 213 | +# ============================================ |
| 214 | +# 导出 |
| 215 | +# ============================================ |
| 216 | + |
| 217 | +__all__ = [ |
| 218 | + 'TOP_STRING', |
| 219 | + 'TOP_NUMBER', |
| 220 | + 'TopType1', |
| 221 | + 'TopType2', |
| 222 | + 'top_func_no_params', |
| 223 | + 'top_func_calling', |
| 224 | + 'helper_func', |
| 225 | + 'SymbolA', |
| 226 | + 'ChildClass', |
| 227 | +] |
0 commit comments