Skip to content

Commit 7ef5337

Browse files
author
smulet
committed
pyright-internal python parser
1 parent f13d9e4 commit 7ef5337

1,873 files changed

Lines changed: 297168 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

python-parser/.gitignore

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Pyright 内部生成文件 & 缓存
2+
packages/python-parser/dist/
3+
packages/python-parser/typeshed-fallback/
4+
typeshed-fallback/
5+
dist/
6+
*.pyi
7+
8+
# 通用 Python 缓存
9+
__pycache__/
10+
*.py[cod]
11+
12+
# TypeScript 编译输出
13+
*.js
14+
*.js.map
15+
*.d.ts
16+
*.tsbuildinfo
17+
18+
# 构建输出目录
19+
dist/
20+
out/
21+
build/
22+
23+
# 内部生成的声明与映射
24+
packages/**/*.js
25+
packages/**/*.js.map
26+
packages/**/*.d.ts
27+
28+
# 日志、IDE、缓存
29+
node_modules/
30+
.DS_Store
31+
.idea/
32+
.vscode/
33+
*.log
34+
35+
*.md
36+
.spec/
37+
.repotalk/
38+
.claude/
39+
.abcoder/
40+
.mcp.json
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pkg_a import get_symbol_a, SymbolA
2+
from pkg_b import get_symbol_b, SymbolB
3+
4+
5+
def main() -> None:
6+
a: SymbolA = get_symbol_a()
7+
b: SymbolB = get_symbol_b(a)
8+
print(a.get_value())
9+
print(b.get_value())
10+
11+
12+
if __name__ == "__main__":
13+
main()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from .module_a import symbol_a, SymbolA
2+
3+
4+
def get_symbol_a() -> SymbolA:
5+
return symbol_a()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass(frozen=True)
5+
class SymbolA:
6+
"""强类型的 Symbol A"""
7+
value: str
8+
9+
def get_value(self) -> str:
10+
return self.value
11+
12+
13+
def symbol_a() -> SymbolA:
14+
return SymbolA(value="Symbol A from pkg_a")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from dataclasses import dataclass
2+
from pkg_a.module_a import SymbolA
3+
4+
5+
@dataclass(frozen=True)
6+
class SymbolB:
7+
"""强类型的 Symbol B"""
8+
value: str
9+
10+
def get_value(self) -> str:
11+
return self.value
12+
13+
14+
def symbol_b(a: SymbolA) -> SymbolB:
15+
result: str = a.get_value()
16+
return SymbolB(value=f"Symbol B uses: {result}")
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[build-system]
2+
requires = ["hatchling"]
3+
build-backend = "hatchling.build"
4+
5+
[project]
6+
name = "mock-python"
7+
version = "0.1.0"
8+
description = "Mock Python Project"
9+
readme = "README.md"
10+
requires-python = ">=3.10"
11+
12+
[tool.hatch.build.targets.wheel]
13+
packages = ["src/pkg_a", "src/pkg_b", "src"]
14+
15+
[tool.hatch.structure]
16+
root = "src"
Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
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

Comments
 (0)