codiff 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codiff/__init__.py +1 -0
- codiff/__main__.py +5 -0
- codiff/cli.py +247 -0
- codiff/db/__init__.py +16 -0
- codiff/db/engine.py +31 -0
- codiff/db/models.py +119 -0
- codiff/db/operations.py +238 -0
- codiff/diff/__init__.py +1 -0
- codiff/diff/analysis.py +145 -0
- codiff/diff/differ.py +45 -0
- codiff/diff/engine.py +80 -0
- codiff/diff/indexer.py +134 -0
- codiff/diff/snapshot.py +325 -0
- codiff/export/__init__.py +5 -0
- codiff/export/json.py +43 -0
- codiff/export/mermaid.py +475 -0
- codiff/export/terminal.py +866 -0
- codiff/languages/__init__.py +29 -0
- codiff/languages/parser.py +214 -0
- codiff/languages/python/__init__.py +0 -0
- codiff/languages/python/parser.py +927 -0
- codiff/languages/python/resolver.py +178 -0
- codiff/languages/repository.py +245 -0
- codiff/languages/resolver.py +582 -0
- codiff/languages/typescript/__init__.py +0 -0
- codiff/languages/typescript/parser.py +612 -0
- codiff/languages/typescript/resolver.py +95 -0
- codiff/mcp_server.py +83 -0
- codiff/schema/__init__.py +25 -0
- codiff/schema/diff.py +110 -0
- codiff/schema/parsing.py +76 -0
- codiff/setup.py +103 -0
- codiff/utils/__init__.py +0 -0
- codiff/utils/args.py +114 -0
- codiff/utils/files.py +62 -0
- codiff/utils/git.py +101 -0
- codiff/utils/instructions.py +14 -0
- codiff/utils/instructions.yaml +50 -0
- codiff/utils/terminal.py +10 -0
- codiff-0.1.0.dist-info/METADATA +487 -0
- codiff-0.1.0.dist-info/RECORD +45 -0
- codiff-0.1.0.dist-info/WHEEL +5 -0
- codiff-0.1.0.dist-info/entry_points.txt +3 -0
- codiff-0.1.0.dist-info/licenses/LICENSE +201 -0
- codiff-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,927 @@
|
|
|
1
|
+
"""Python-specific parser implementation.
|
|
2
|
+
|
|
3
|
+
Implements LanguageParser for Python using tree-sitter-python. Contains all
|
|
4
|
+
Python-specific query definitions, AST node handling, call extraction, import
|
|
5
|
+
resolution, and docstring parsing.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import re
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
|
|
11
|
+
import tree_sitter_python as tspython
|
|
12
|
+
from tree_sitter import Language, Query
|
|
13
|
+
|
|
14
|
+
from codiff.languages.parser import LanguageParser
|
|
15
|
+
from codiff.schema.parsing import ClassChunk, FunctionChunk, Parameter
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class PythonParser(LanguageParser):
|
|
19
|
+
# ------------------------------------------------------------------
|
|
20
|
+
# LanguageParser interface
|
|
21
|
+
# ------------------------------------------------------------------
|
|
22
|
+
|
|
23
|
+
@property
|
|
24
|
+
def language(self) -> Language:
|
|
25
|
+
return Language(tspython.language())
|
|
26
|
+
|
|
27
|
+
@property
|
|
28
|
+
def extension(self) -> str:
|
|
29
|
+
return ".py"
|
|
30
|
+
|
|
31
|
+
# ------------------------------------------------------------------
|
|
32
|
+
# Module resolution (Python-specific overrides)
|
|
33
|
+
# ------------------------------------------------------------------
|
|
34
|
+
|
|
35
|
+
def file_to_module_id(self, rel_path: str) -> str:
|
|
36
|
+
"""'pkg/sub/mod.py' → 'pkg.sub.mod'"""
|
|
37
|
+
return rel_path.replace("/", ".").replace(".py", "")
|
|
38
|
+
|
|
39
|
+
def _is_package_init(self, filename: str) -> bool:
|
|
40
|
+
return filename == "__init__.py"
|
|
41
|
+
|
|
42
|
+
def build_package_exports(self, repo_path: Path, gitignore=None) -> dict[str, str]:
|
|
43
|
+
"""Scan __init__.py files for re-exports (from .sub import name)."""
|
|
44
|
+
import os
|
|
45
|
+
|
|
46
|
+
from codiff.utils.files import is_venv_dir
|
|
47
|
+
from codiff.utils.git import is_dir_ignored
|
|
48
|
+
|
|
49
|
+
package_exports: dict[str, str] = {}
|
|
50
|
+
repo_str = str(repo_path)
|
|
51
|
+
|
|
52
|
+
for root, dirs, files in os.walk(repo_path):
|
|
53
|
+
dirs[:] = [
|
|
54
|
+
d
|
|
55
|
+
for d in dirs
|
|
56
|
+
if d not in self.exclude_dirs
|
|
57
|
+
and not is_venv_dir(root, d)
|
|
58
|
+
and not is_dir_ignored(gitignore, repo_str, root, d)
|
|
59
|
+
]
|
|
60
|
+
if "__init__.py" not in files:
|
|
61
|
+
continue
|
|
62
|
+
init_path = Path(root) / "__init__.py"
|
|
63
|
+
rel_dir = str(init_path.parent.relative_to(repo_path))
|
|
64
|
+
package_name = "" if rel_dir == "." else rel_dir.replace("/", ".")
|
|
65
|
+
try:
|
|
66
|
+
content = init_path.read_text(encoding="utf-8", errors="ignore")
|
|
67
|
+
pattern = r"from\s+\.(\w+)\s+import\s+(?:\(([^)]+)\)|([^(\n]+))"
|
|
68
|
+
for match in re.finditer(pattern, content, re.DOTALL):
|
|
69
|
+
submodule = match.group(1)
|
|
70
|
+
names_str = re.sub(r"#[^\n]*", "", match.group(2) or match.group(3))
|
|
71
|
+
for part in names_str.split(","):
|
|
72
|
+
part = part.strip()
|
|
73
|
+
if not part:
|
|
74
|
+
continue
|
|
75
|
+
if " as " in part:
|
|
76
|
+
orig, alias = part.split(" as ")
|
|
77
|
+
orig, alias = orig.strip(), alias.strip()
|
|
78
|
+
else:
|
|
79
|
+
orig = alias = part
|
|
80
|
+
if not re.match(r"^[a-zA-Z_]\w*$", orig or ""):
|
|
81
|
+
continue
|
|
82
|
+
if not re.match(r"^[a-zA-Z_]\w*$", alias or ""):
|
|
83
|
+
continue
|
|
84
|
+
if package_name:
|
|
85
|
+
package_exports[f"{package_name}.{alias}"] = (
|
|
86
|
+
f"{package_name}.{submodule}.{orig}"
|
|
87
|
+
)
|
|
88
|
+
else:
|
|
89
|
+
package_exports[alias] = f"{submodule}.{orig}"
|
|
90
|
+
except Exception:
|
|
91
|
+
pass
|
|
92
|
+
|
|
93
|
+
return package_exports
|
|
94
|
+
|
|
95
|
+
def _extra_exclude_dirs(self) -> set[str]:
|
|
96
|
+
return {
|
|
97
|
+
"__pycache__",
|
|
98
|
+
".pytest_cache",
|
|
99
|
+
".mypy_cache",
|
|
100
|
+
".ruff_cache",
|
|
101
|
+
"venv",
|
|
102
|
+
"env",
|
|
103
|
+
".venv",
|
|
104
|
+
".eggs",
|
|
105
|
+
".tox",
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
def _init_queries(self) -> None:
|
|
109
|
+
lang = self.language
|
|
110
|
+
|
|
111
|
+
self.function_query = Query(
|
|
112
|
+
lang,
|
|
113
|
+
"""
|
|
114
|
+
(function_definition
|
|
115
|
+
name: (identifier) @func_name
|
|
116
|
+
parameters: (parameters
|
|
117
|
+
[
|
|
118
|
+
(identifier) @param.name
|
|
119
|
+
(typed_parameter
|
|
120
|
+
(identifier) @param.typed.name
|
|
121
|
+
type: (_) @param.typed.type)
|
|
122
|
+
(default_parameter
|
|
123
|
+
name: (identifier) @param.default.name
|
|
124
|
+
value: (_) @param.default.value)
|
|
125
|
+
(typed_default_parameter
|
|
126
|
+
(identifier) @param.typed_default.name
|
|
127
|
+
type: (_) @param.typed_default.type
|
|
128
|
+
value: (_) @param.typed_default.value)
|
|
129
|
+
]*
|
|
130
|
+
) @params
|
|
131
|
+
return_type: (_)? @return_type
|
|
132
|
+
body: (block
|
|
133
|
+
(expression_statement
|
|
134
|
+
(string) @docstring)?
|
|
135
|
+
(_)*
|
|
136
|
+
) @body
|
|
137
|
+
) @function
|
|
138
|
+
""",
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
self.class_query = Query(
|
|
142
|
+
lang,
|
|
143
|
+
"""
|
|
144
|
+
(class_definition
|
|
145
|
+
name: (identifier) @class_name
|
|
146
|
+
superclasses: (argument_list)? @superclasses
|
|
147
|
+
body: (block
|
|
148
|
+
(expression_statement
|
|
149
|
+
(string) @docstring)?
|
|
150
|
+
(_)*
|
|
151
|
+
) @body
|
|
152
|
+
) @class
|
|
153
|
+
""",
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
self.import_query = Query(
|
|
157
|
+
lang,
|
|
158
|
+
"""
|
|
159
|
+
[
|
|
160
|
+
(import_statement
|
|
161
|
+
name: (dotted_name) @import.module
|
|
162
|
+
)
|
|
163
|
+
(import_statement
|
|
164
|
+
name: (aliased_import
|
|
165
|
+
name: (dotted_name) @import.aliased.name
|
|
166
|
+
alias: (identifier) @import.aliased.alias
|
|
167
|
+
)
|
|
168
|
+
)
|
|
169
|
+
(import_from_statement
|
|
170
|
+
module_name: (dotted_name) @import_from.module
|
|
171
|
+
name: (dotted_name) @import_from.name
|
|
172
|
+
)
|
|
173
|
+
(import_from_statement
|
|
174
|
+
module_name: (dotted_name) @import_from.module
|
|
175
|
+
name: (aliased_import
|
|
176
|
+
name: (dotted_name) @import_from.aliased.name
|
|
177
|
+
alias: (identifier) @import_from.aliased.alias
|
|
178
|
+
)
|
|
179
|
+
)
|
|
180
|
+
(import_from_statement
|
|
181
|
+
module_name: (relative_import) @import_from.relative_module
|
|
182
|
+
name: (dotted_name) @import_from.relative_name
|
|
183
|
+
)
|
|
184
|
+
(import_from_statement
|
|
185
|
+
module_name: (relative_import) @import_from.relative_module
|
|
186
|
+
name: (aliased_import
|
|
187
|
+
name: (dotted_name) @import_from.relative_aliased.name
|
|
188
|
+
alias: (identifier) @import_from.relative_aliased.alias
|
|
189
|
+
)
|
|
190
|
+
)
|
|
191
|
+
]
|
|
192
|
+
""",
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
# ------------------------------------------------------------------
|
|
196
|
+
# Function extraction
|
|
197
|
+
# ------------------------------------------------------------------
|
|
198
|
+
|
|
199
|
+
def _extract_function(self, captures_dict: dict, relative_path: str) -> FunctionChunk | None:
|
|
200
|
+
if "func_name" not in captures_dict or "function" not in captures_dict:
|
|
201
|
+
return None
|
|
202
|
+
|
|
203
|
+
func_node = self.get_node(captures_dict, "function")
|
|
204
|
+
name_node = self.get_node(captures_dict, "func_name")
|
|
205
|
+
body_node = self.get_node(captures_dict, "body")
|
|
206
|
+
|
|
207
|
+
func_name = name_node.text.decode("utf-8")
|
|
208
|
+
start_line = func_node.start_point[0] + 1
|
|
209
|
+
end_line = func_node.end_point[0] + 1
|
|
210
|
+
func_code = func_node.text.decode("utf-8")
|
|
211
|
+
|
|
212
|
+
parameters = []
|
|
213
|
+
params_node = self.get_node(captures_dict, "params")
|
|
214
|
+
if params_node:
|
|
215
|
+
parameters = self._extract_parameters_from_node(params_node)
|
|
216
|
+
|
|
217
|
+
return_type = None
|
|
218
|
+
return_type_node = self.get_node(captures_dict, "return_type")
|
|
219
|
+
if return_type_node:
|
|
220
|
+
return_type = return_type_node.text.decode("utf-8")
|
|
221
|
+
|
|
222
|
+
docstring = None
|
|
223
|
+
docstring_node = self.get_node(captures_dict, "docstring")
|
|
224
|
+
if docstring_node:
|
|
225
|
+
docstring = docstring_node.text.decode("utf-8")
|
|
226
|
+
|
|
227
|
+
decorators, class_name, nested = self._get_function_context(func_node)
|
|
228
|
+
|
|
229
|
+
_id = relative_path + "/" + class_name + "." if class_name else relative_path + "/"
|
|
230
|
+
_id = _id + nested + "." if nested else _id
|
|
231
|
+
_id = _id + func_name
|
|
232
|
+
_id = _id.replace("/", ".").replace(".py", "")
|
|
233
|
+
|
|
234
|
+
var_types, var_sources = self.extract_variable_assignments(body_node)
|
|
235
|
+
param_names = {p.name for p in parameters if p.name}
|
|
236
|
+
calls = self.extract_calls_from_function_body(body_node, var_types, param_names)
|
|
237
|
+
|
|
238
|
+
return FunctionChunk(
|
|
239
|
+
id=_id,
|
|
240
|
+
name=func_name,
|
|
241
|
+
code=func_code,
|
|
242
|
+
docstring=docstring,
|
|
243
|
+
start_line=start_line,
|
|
244
|
+
end_line=end_line,
|
|
245
|
+
parameters=parameters,
|
|
246
|
+
decorators=decorators,
|
|
247
|
+
file_path=relative_path,
|
|
248
|
+
class_name=class_name,
|
|
249
|
+
nested=nested,
|
|
250
|
+
return_type=return_type,
|
|
251
|
+
calls=calls,
|
|
252
|
+
var_types=var_types if var_types else None,
|
|
253
|
+
var_sources=var_sources if var_sources else None,
|
|
254
|
+
)
|
|
255
|
+
|
|
256
|
+
def _extract_parameters_from_node(self, params_node) -> list[Parameter]:
|
|
257
|
+
parameters = []
|
|
258
|
+
for param in params_node.named_children:
|
|
259
|
+
name, type_, value = None, None, None
|
|
260
|
+
if param.type == "identifier":
|
|
261
|
+
name = param.text.decode("utf-8")
|
|
262
|
+
elif param.type == "typed_parameter":
|
|
263
|
+
name = param.named_children[0].text.decode("utf-8")
|
|
264
|
+
type_ = param.named_children[1].text.decode("utf-8")
|
|
265
|
+
elif param.type == "default_parameter":
|
|
266
|
+
name = param.named_children[0].text.decode("utf-8")
|
|
267
|
+
value = param.named_children[1].text.decode("utf-8")
|
|
268
|
+
elif param.type == "typed_default_parameter":
|
|
269
|
+
name = param.named_children[0].text.decode("utf-8")
|
|
270
|
+
type_ = param.named_children[1].text.decode("utf-8")
|
|
271
|
+
value = param.named_children[2].text.decode("utf-8")
|
|
272
|
+
if name is not None:
|
|
273
|
+
parameters.append(Parameter(name=name, type=type_, value=value))
|
|
274
|
+
return parameters
|
|
275
|
+
|
|
276
|
+
def _get_function_context(self, func_node) -> tuple[list[str], str | None, str | None]:
|
|
277
|
+
decorators: list[str] = []
|
|
278
|
+
nested = None
|
|
279
|
+
class_name = None
|
|
280
|
+
|
|
281
|
+
parent = func_node.parent
|
|
282
|
+
if parent:
|
|
283
|
+
func_index = list(parent.children).index(func_node)
|
|
284
|
+
for i in range(func_index - 1, -1, -1):
|
|
285
|
+
sibling = parent.children[i]
|
|
286
|
+
if sibling.type == "decorator":
|
|
287
|
+
decorator_text = sibling.text.decode("utf-8").strip()
|
|
288
|
+
if decorator_text.startswith("@"):
|
|
289
|
+
decorator_text = decorator_text[1:]
|
|
290
|
+
decorators.insert(0, decorator_text)
|
|
291
|
+
|
|
292
|
+
depth = 0
|
|
293
|
+
current = func_node.parent
|
|
294
|
+
while current and depth < 10:
|
|
295
|
+
if current.type == "class_definition":
|
|
296
|
+
for child in current.named_children:
|
|
297
|
+
if child.type == "identifier":
|
|
298
|
+
class_name = child.text.decode("utf-8")
|
|
299
|
+
break
|
|
300
|
+
elif current.type == "function_definition" and nested is None:
|
|
301
|
+
for child in current.named_children:
|
|
302
|
+
if child.type == "identifier":
|
|
303
|
+
nested = child.text.decode("utf-8")
|
|
304
|
+
break
|
|
305
|
+
current = current.parent
|
|
306
|
+
depth += 1
|
|
307
|
+
|
|
308
|
+
return decorators, class_name, nested
|
|
309
|
+
|
|
310
|
+
def _extract_decorator_call(self, decorator_text: str) -> str | None:
|
|
311
|
+
builtin_decorators = {
|
|
312
|
+
"staticmethod",
|
|
313
|
+
"classmethod",
|
|
314
|
+
"property",
|
|
315
|
+
"abstractmethod",
|
|
316
|
+
"dataclass",
|
|
317
|
+
"contextmanager",
|
|
318
|
+
"lru_cache",
|
|
319
|
+
"cached_property",
|
|
320
|
+
}
|
|
321
|
+
func_name = (
|
|
322
|
+
decorator_text.split("(")[0].strip()
|
|
323
|
+
if "(" in decorator_text
|
|
324
|
+
else decorator_text.strip()
|
|
325
|
+
)
|
|
326
|
+
if func_name.split(".")[-1] in builtin_decorators:
|
|
327
|
+
return None
|
|
328
|
+
return func_name
|
|
329
|
+
|
|
330
|
+
# ------------------------------------------------------------------
|
|
331
|
+
# Call extraction
|
|
332
|
+
# ------------------------------------------------------------------
|
|
333
|
+
|
|
334
|
+
def extract_calls_from_function_body(self, body_node, var_types=None, param_names=None) -> list:
|
|
335
|
+
calls = []
|
|
336
|
+
var_types = var_types or {}
|
|
337
|
+
param_names = param_names or set()
|
|
338
|
+
|
|
339
|
+
def walk(node):
|
|
340
|
+
for child in node.children:
|
|
341
|
+
if child.type == "function_definition":
|
|
342
|
+
continue
|
|
343
|
+
walk(child)
|
|
344
|
+
|
|
345
|
+
if node.type == "call":
|
|
346
|
+
call_info = self.parse_call_node(node)
|
|
347
|
+
if call_info:
|
|
348
|
+
calls.append(call_info)
|
|
349
|
+
dunder_calls = self._extract_builtin_dunder_calls(node)
|
|
350
|
+
calls.extend(dunder_calls)
|
|
351
|
+
func_refs = self._extract_function_references_from_call(
|
|
352
|
+
node, var_types, param_names
|
|
353
|
+
)
|
|
354
|
+
calls.extend(func_refs)
|
|
355
|
+
|
|
356
|
+
walk(body_node)
|
|
357
|
+
method_refs = self._extract_self_method_references(body_node, calls)
|
|
358
|
+
calls.extend(method_refs)
|
|
359
|
+
return calls
|
|
360
|
+
|
|
361
|
+
def _extract_self_method_references(self, body_node, existing_calls) -> list:
|
|
362
|
+
refs = []
|
|
363
|
+
existing_set = set(existing_calls)
|
|
364
|
+
|
|
365
|
+
def walk(node):
|
|
366
|
+
if node.type == "attribute":
|
|
367
|
+
named = node.named_children
|
|
368
|
+
if (
|
|
369
|
+
len(named) == 2
|
|
370
|
+
and named[0].type == "identifier"
|
|
371
|
+
and named[0].text == b"self"
|
|
372
|
+
and named[1].type == "identifier"
|
|
373
|
+
):
|
|
374
|
+
method_name = named[1].text.decode("utf-8")
|
|
375
|
+
ref_text = f"self.{method_name}"
|
|
376
|
+
if ref_text not in existing_set and self._is_method_reference_context(node):
|
|
377
|
+
refs.append(ref_text)
|
|
378
|
+
existing_set.add(ref_text)
|
|
379
|
+
return
|
|
380
|
+
for child in node.children:
|
|
381
|
+
if child.type == "function_definition":
|
|
382
|
+
continue
|
|
383
|
+
walk(child)
|
|
384
|
+
|
|
385
|
+
walk(body_node)
|
|
386
|
+
return refs
|
|
387
|
+
|
|
388
|
+
def _is_method_reference_context(self, attr_node) -> bool:
|
|
389
|
+
parent = attr_node.parent
|
|
390
|
+
if parent is None:
|
|
391
|
+
return False
|
|
392
|
+
if parent.type == "call" and parent.children and parent.children[0] == attr_node:
|
|
393
|
+
return False
|
|
394
|
+
if parent.type == "attribute":
|
|
395
|
+
return False
|
|
396
|
+
if parent.type == "comparison_operator":
|
|
397
|
+
return False
|
|
398
|
+
if parent.type == "assignment" and parent.children and parent.children[0] == attr_node:
|
|
399
|
+
return False
|
|
400
|
+
if (
|
|
401
|
+
parent.type == "augmented_assignment"
|
|
402
|
+
and parent.children
|
|
403
|
+
and parent.children[0] == attr_node
|
|
404
|
+
):
|
|
405
|
+
return False
|
|
406
|
+
if parent.type == "subscript" and parent.children and parent.children[0] == attr_node:
|
|
407
|
+
return False
|
|
408
|
+
current = parent
|
|
409
|
+
while current:
|
|
410
|
+
if current.type == "delete_statement":
|
|
411
|
+
return False
|
|
412
|
+
current = current.parent
|
|
413
|
+
if parent.type in ("interpolation", "format_expression"):
|
|
414
|
+
return False
|
|
415
|
+
return True
|
|
416
|
+
|
|
417
|
+
BUILTIN_TO_DUNDER = {
|
|
418
|
+
"len": "__len__",
|
|
419
|
+
"str": "__str__",
|
|
420
|
+
"repr": "__repr__",
|
|
421
|
+
"int": "__int__",
|
|
422
|
+
"float": "__float__",
|
|
423
|
+
"bool": "__bool__",
|
|
424
|
+
"bytes": "__bytes__",
|
|
425
|
+
"hash": "__hash__",
|
|
426
|
+
"iter": "__iter__",
|
|
427
|
+
"next": "__next__",
|
|
428
|
+
"reversed": "__reversed__",
|
|
429
|
+
"abs": "__abs__",
|
|
430
|
+
"round": "__round__",
|
|
431
|
+
"complex": "__complex__",
|
|
432
|
+
"format": "__format__",
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
def _extract_builtin_dunder_calls(self, call_node) -> list:
|
|
436
|
+
dunder_calls: list[str] = []
|
|
437
|
+
if not call_node.children:
|
|
438
|
+
return dunder_calls
|
|
439
|
+
func_node = call_node.children[0]
|
|
440
|
+
if func_node.type != "identifier":
|
|
441
|
+
return dunder_calls
|
|
442
|
+
func_name = func_node.text.decode("utf-8")
|
|
443
|
+
if func_name not in self.BUILTIN_TO_DUNDER:
|
|
444
|
+
return dunder_calls
|
|
445
|
+
dunder_method = self.BUILTIN_TO_DUNDER[func_name]
|
|
446
|
+
for child in call_node.children:
|
|
447
|
+
if child.type == "argument_list":
|
|
448
|
+
first_arg = self._get_first_positional_arg(child)
|
|
449
|
+
if first_arg:
|
|
450
|
+
dunder_calls.append(f"{first_arg}.{dunder_method}")
|
|
451
|
+
break
|
|
452
|
+
return dunder_calls
|
|
453
|
+
|
|
454
|
+
def _get_first_positional_arg(self, arg_list_node) -> str | None:
|
|
455
|
+
for child in arg_list_node.children:
|
|
456
|
+
if child.type in ("(", ")", ","):
|
|
457
|
+
continue
|
|
458
|
+
if child.type == "keyword_argument":
|
|
459
|
+
continue
|
|
460
|
+
if child.type in ("identifier", "attribute"):
|
|
461
|
+
return child.text.decode("utf-8")
|
|
462
|
+
break
|
|
463
|
+
return None
|
|
464
|
+
|
|
465
|
+
def _extract_function_references_from_call(
|
|
466
|
+
self, call_node, var_types=None, param_names=None
|
|
467
|
+
) -> list:
|
|
468
|
+
func_refs = []
|
|
469
|
+
var_types = var_types or {}
|
|
470
|
+
param_names = param_names or set()
|
|
471
|
+
for child in call_node.children:
|
|
472
|
+
if child.type == "argument_list":
|
|
473
|
+
for arg in child.children:
|
|
474
|
+
ref = self._extract_func_ref_from_arg(arg, var_types, param_names)
|
|
475
|
+
if ref:
|
|
476
|
+
func_refs.append(ref)
|
|
477
|
+
return func_refs
|
|
478
|
+
|
|
479
|
+
def _extract_func_ref_from_arg(self, arg_node, var_types=None, param_names=None) -> str | None:
|
|
480
|
+
var_types = var_types or {}
|
|
481
|
+
param_names = param_names or set()
|
|
482
|
+
|
|
483
|
+
if arg_node.type == "identifier":
|
|
484
|
+
name = arg_node.text.decode("utf-8")
|
|
485
|
+
if name in var_types or name in param_names:
|
|
486
|
+
return None
|
|
487
|
+
if not self._is_likely_function_reference(name):
|
|
488
|
+
return None
|
|
489
|
+
return name
|
|
490
|
+
|
|
491
|
+
elif arg_node.type == "attribute":
|
|
492
|
+
return arg_node.text.decode("utf-8")
|
|
493
|
+
|
|
494
|
+
elif arg_node.type == "keyword_argument":
|
|
495
|
+
callback_keywords = {
|
|
496
|
+
"key",
|
|
497
|
+
"target",
|
|
498
|
+
"func",
|
|
499
|
+
"function",
|
|
500
|
+
"callback",
|
|
501
|
+
"handler",
|
|
502
|
+
"processor",
|
|
503
|
+
"factory",
|
|
504
|
+
"side_effect",
|
|
505
|
+
"default",
|
|
506
|
+
"default_factory",
|
|
507
|
+
}
|
|
508
|
+
key_name = None
|
|
509
|
+
value_node = None
|
|
510
|
+
for child in arg_node.children:
|
|
511
|
+
if child.type == "identifier" and key_name is None:
|
|
512
|
+
key_name = child.text.decode("utf-8")
|
|
513
|
+
elif child.type in ("identifier", "attribute"):
|
|
514
|
+
value_node = child
|
|
515
|
+
if key_name in callback_keywords and value_node:
|
|
516
|
+
value_text = value_node.text.decode("utf-8")
|
|
517
|
+
if value_text in var_types or value_text in param_names:
|
|
518
|
+
return None
|
|
519
|
+
return value_text
|
|
520
|
+
|
|
521
|
+
return None
|
|
522
|
+
|
|
523
|
+
def _is_likely_function_reference(self, name: str) -> bool:
|
|
524
|
+
non_function_names = {
|
|
525
|
+
"self",
|
|
526
|
+
"cls",
|
|
527
|
+
"args",
|
|
528
|
+
"kwargs",
|
|
529
|
+
"data",
|
|
530
|
+
"items",
|
|
531
|
+
"result",
|
|
532
|
+
"value",
|
|
533
|
+
"values",
|
|
534
|
+
"key",
|
|
535
|
+
"keys",
|
|
536
|
+
"item",
|
|
537
|
+
"x",
|
|
538
|
+
"y",
|
|
539
|
+
"z",
|
|
540
|
+
"i",
|
|
541
|
+
"j",
|
|
542
|
+
"k",
|
|
543
|
+
"n",
|
|
544
|
+
"m",
|
|
545
|
+
"a",
|
|
546
|
+
"b",
|
|
547
|
+
"c",
|
|
548
|
+
"d",
|
|
549
|
+
"e",
|
|
550
|
+
"f",
|
|
551
|
+
"None",
|
|
552
|
+
"True",
|
|
553
|
+
"False",
|
|
554
|
+
"str",
|
|
555
|
+
"int",
|
|
556
|
+
"float",
|
|
557
|
+
"list",
|
|
558
|
+
"dict",
|
|
559
|
+
"set",
|
|
560
|
+
"tuple",
|
|
561
|
+
"bytes",
|
|
562
|
+
"bool",
|
|
563
|
+
"type",
|
|
564
|
+
"object",
|
|
565
|
+
"path",
|
|
566
|
+
"file",
|
|
567
|
+
"name",
|
|
568
|
+
"msg",
|
|
569
|
+
"message",
|
|
570
|
+
"text",
|
|
571
|
+
"string",
|
|
572
|
+
"config",
|
|
573
|
+
"options",
|
|
574
|
+
"settings",
|
|
575
|
+
"params",
|
|
576
|
+
}
|
|
577
|
+
if name in non_function_names:
|
|
578
|
+
return False
|
|
579
|
+
if len(name) == 1 and name.islower():
|
|
580
|
+
return False
|
|
581
|
+
return True
|
|
582
|
+
|
|
583
|
+
def parse_call_node(self, call_node) -> str | None:
|
|
584
|
+
if not call_node.children:
|
|
585
|
+
return None
|
|
586
|
+
function_expr = call_node.children[0]
|
|
587
|
+
|
|
588
|
+
if function_expr.type == "attribute":
|
|
589
|
+
for child in function_expr.children:
|
|
590
|
+
if child.type == "call":
|
|
591
|
+
inner_call_text = self.parse_call_node(child)
|
|
592
|
+
if inner_call_text:
|
|
593
|
+
parts = inner_call_text.split(".")
|
|
594
|
+
base = parts[0]
|
|
595
|
+
special_calls = {"super", "cls", "type"}
|
|
596
|
+
if base.startswith("super(") or base in special_calls:
|
|
597
|
+
return function_expr.text.decode("utf-8")
|
|
598
|
+
method_name = None
|
|
599
|
+
for sibling in function_expr.children:
|
|
600
|
+
if sibling.type == "identifier":
|
|
601
|
+
method_name = sibling.text.decode("utf-8")
|
|
602
|
+
if method_name:
|
|
603
|
+
return f"{base}.{method_name}"
|
|
604
|
+
return function_expr.text.decode("utf-8")
|
|
605
|
+
return function_expr.text.decode("utf-8")
|
|
606
|
+
|
|
607
|
+
return function_expr.text.decode("utf-8")
|
|
608
|
+
|
|
609
|
+
# ------------------------------------------------------------------
|
|
610
|
+
# Variable assignment extraction (for call resolution)
|
|
611
|
+
# ------------------------------------------------------------------
|
|
612
|
+
|
|
613
|
+
def extract_variable_assignments(self, body_node) -> tuple[dict, dict]:
|
|
614
|
+
var_types = {}
|
|
615
|
+
var_sources = {}
|
|
616
|
+
|
|
617
|
+
def walk(node):
|
|
618
|
+
if node.type == "assignment":
|
|
619
|
+
var_name, class_name = self._extract_assignment_info(node)
|
|
620
|
+
if var_name and class_name:
|
|
621
|
+
var_types[var_name] = [class_name]
|
|
622
|
+
else:
|
|
623
|
+
var_name, class_names = self._extract_conditional_assignment(node)
|
|
624
|
+
if var_name and class_names:
|
|
625
|
+
var_types[var_name] = class_names
|
|
626
|
+
else:
|
|
627
|
+
var_name, source = self._extract_chained_call_assignment_info(node)
|
|
628
|
+
if var_name and source:
|
|
629
|
+
var_sources[var_name] = source
|
|
630
|
+
elif node.type == "with_statement":
|
|
631
|
+
ctx_vars = self._extract_context_manager_vars(node)
|
|
632
|
+
for k, v in ctx_vars.items():
|
|
633
|
+
var_types[k] = [v]
|
|
634
|
+
elif node.type in ("for_statement", "for_in_clause"):
|
|
635
|
+
var_sources.update(self._extract_for_loop_vars(node))
|
|
636
|
+
for child in node.children:
|
|
637
|
+
if child.type == "function_definition":
|
|
638
|
+
continue
|
|
639
|
+
walk(child)
|
|
640
|
+
|
|
641
|
+
walk(body_node)
|
|
642
|
+
return var_types, var_sources
|
|
643
|
+
|
|
644
|
+
def _extract_assignment_info(self, assignment_node) -> tuple[str | None, str | None]:
|
|
645
|
+
var_name = None
|
|
646
|
+
class_name = None
|
|
647
|
+
for child in assignment_node.children:
|
|
648
|
+
if child.type == "identifier" and var_name is None:
|
|
649
|
+
var_name = child.text.decode("utf-8")
|
|
650
|
+
elif child.type == "call":
|
|
651
|
+
if child.children:
|
|
652
|
+
func_expr = child.children[0]
|
|
653
|
+
if func_expr.type == "identifier":
|
|
654
|
+
class_name = func_expr.text.decode("utf-8")
|
|
655
|
+
elif func_expr.type == "attribute":
|
|
656
|
+
is_chained = any(c.type == "call" for c in func_expr.children)
|
|
657
|
+
if not is_chained:
|
|
658
|
+
class_name = func_expr.text.decode("utf-8")
|
|
659
|
+
return var_name, class_name
|
|
660
|
+
|
|
661
|
+
def _extract_conditional_assignment(self, assignment_node) -> tuple[str | None, list | None]:
|
|
662
|
+
var_name = None
|
|
663
|
+
class_names = []
|
|
664
|
+
for child in assignment_node.children:
|
|
665
|
+
if child.type == "identifier" and var_name is None:
|
|
666
|
+
var_name = child.text.decode("utf-8")
|
|
667
|
+
elif child.type == "conditional_expression" and var_name is not None:
|
|
668
|
+
parts = child.children
|
|
669
|
+
if len(parts) >= 5:
|
|
670
|
+
for expr in (parts[0], parts[-1]):
|
|
671
|
+
if expr.type in ("identifier", "attribute"):
|
|
672
|
+
class_names.append(expr.text.decode("utf-8"))
|
|
673
|
+
if var_name and class_names:
|
|
674
|
+
return var_name, [f"@ref:{c}" for c in class_names]
|
|
675
|
+
return None, None
|
|
676
|
+
|
|
677
|
+
def _extract_chained_call_assignment_info(
|
|
678
|
+
self, assignment_node
|
|
679
|
+
) -> tuple[str | None, str | None]:
|
|
680
|
+
var_name = None
|
|
681
|
+
source = None
|
|
682
|
+
for child in assignment_node.children:
|
|
683
|
+
if child.type == "identifier" and var_name is None:
|
|
684
|
+
var_name = child.text.decode("utf-8")
|
|
685
|
+
elif child.type == "call" and var_name is not None:
|
|
686
|
+
if not child.children:
|
|
687
|
+
continue
|
|
688
|
+
func_expr = child.children[0]
|
|
689
|
+
if func_expr.type != "attribute":
|
|
690
|
+
continue
|
|
691
|
+
inner_call = None
|
|
692
|
+
method_name = None
|
|
693
|
+
for attr_child in func_expr.children:
|
|
694
|
+
if attr_child.type == "call":
|
|
695
|
+
inner_call = attr_child
|
|
696
|
+
elif attr_child.type == "identifier":
|
|
697
|
+
method_name = attr_child.text.decode("utf-8")
|
|
698
|
+
if inner_call is None or method_name is None:
|
|
699
|
+
continue
|
|
700
|
+
if inner_call.children:
|
|
701
|
+
inner_func = inner_call.children[0]
|
|
702
|
+
if inner_func.type == "identifier":
|
|
703
|
+
source = f"{inner_func.text.decode('utf-8')}.{method_name}"
|
|
704
|
+
return var_name, source
|
|
705
|
+
|
|
706
|
+
def _extract_for_loop_vars(self, node) -> dict:
|
|
707
|
+
var_sources = {}
|
|
708
|
+
loop_var = None
|
|
709
|
+
iterable_node = None
|
|
710
|
+
found_in = False
|
|
711
|
+
for child in node.children:
|
|
712
|
+
if child.type == "identifier" and loop_var is None and not found_in:
|
|
713
|
+
loop_var = child.text.decode("utf-8")
|
|
714
|
+
elif child.text == b"in":
|
|
715
|
+
found_in = True
|
|
716
|
+
elif found_in and iterable_node is None:
|
|
717
|
+
if child.type in (":", "block", "comment"):
|
|
718
|
+
continue
|
|
719
|
+
iterable_node = child
|
|
720
|
+
break
|
|
721
|
+
if loop_var and iterable_node:
|
|
722
|
+
if iterable_node.type == "identifier":
|
|
723
|
+
var_sources[loop_var] = f"@iter:{iterable_node.text.decode('utf-8')}"
|
|
724
|
+
elif iterable_node.type == "call" and iterable_node.children:
|
|
725
|
+
raw_call = iterable_node.children[0].text.decode("utf-8")
|
|
726
|
+
var_sources[loop_var] = f"@iter_call:{raw_call}"
|
|
727
|
+
return var_sources
|
|
728
|
+
|
|
729
|
+
def _extract_context_manager_vars(self, with_node) -> dict:
|
|
730
|
+
var_types = {}
|
|
731
|
+
for child in with_node.children:
|
|
732
|
+
if child.type == "with_clause":
|
|
733
|
+
for item in child.children:
|
|
734
|
+
if item.type == "with_item":
|
|
735
|
+
var_name, class_name = self._extract_with_item_info(item)
|
|
736
|
+
if var_name and class_name:
|
|
737
|
+
var_types[var_name] = class_name
|
|
738
|
+
elif child.type == "with_item":
|
|
739
|
+
var_name, class_name = self._extract_with_item_info(child)
|
|
740
|
+
if var_name and class_name:
|
|
741
|
+
var_types[var_name] = class_name
|
|
742
|
+
return var_types
|
|
743
|
+
|
|
744
|
+
def _extract_with_item_info(self, with_item_node) -> tuple[str | None, str | None]:
|
|
745
|
+
var_name = None
|
|
746
|
+
class_name = None
|
|
747
|
+
for child in with_item_node.children:
|
|
748
|
+
if child.type == "as_pattern":
|
|
749
|
+
for as_child in child.children:
|
|
750
|
+
if as_child.type == "as_pattern_target":
|
|
751
|
+
for target_child in as_child.children:
|
|
752
|
+
if target_child.type == "identifier":
|
|
753
|
+
var_name = target_child.text.decode("utf-8")
|
|
754
|
+
break
|
|
755
|
+
elif as_child.type == "call" and as_child.children:
|
|
756
|
+
func_expr = as_child.children[0]
|
|
757
|
+
if func_expr.type in ("identifier", "attribute"):
|
|
758
|
+
class_name = func_expr.text.decode("utf-8")
|
|
759
|
+
elif as_child.type == "identifier" and var_name is None:
|
|
760
|
+
var_name = as_child.text.decode("utf-8")
|
|
761
|
+
elif child.type == "call" and child.children:
|
|
762
|
+
func_expr = child.children[0]
|
|
763
|
+
if func_expr.type in ("identifier", "attribute"):
|
|
764
|
+
class_name = func_expr.text.decode("utf-8")
|
|
765
|
+
elif child.type == "identifier" and var_name is None:
|
|
766
|
+
var_name = child.text.decode("utf-8")
|
|
767
|
+
return var_name, class_name
|
|
768
|
+
|
|
769
|
+
# ------------------------------------------------------------------
|
|
770
|
+
# Class extraction
|
|
771
|
+
# ------------------------------------------------------------------
|
|
772
|
+
|
|
773
|
+
def _extract_class(self, captures_dict: dict, relative_path: str) -> ClassChunk | None:
|
|
774
|
+
class_node = self.get_node(captures_dict, "class")
|
|
775
|
+
name_node = self.get_node(captures_dict, "class_name")
|
|
776
|
+
super_class_node = self.get_node(captures_dict, "superclasses")
|
|
777
|
+
if class_node is None or name_node is None:
|
|
778
|
+
return None
|
|
779
|
+
|
|
780
|
+
class_name = name_node.text.decode("utf-8")
|
|
781
|
+
start_line = class_node.start_point[0] + 1
|
|
782
|
+
end_line = class_node.end_point[0] + 1
|
|
783
|
+
class_code = class_node.text.decode("utf-8")
|
|
784
|
+
|
|
785
|
+
super_classes = []
|
|
786
|
+
if super_class_node:
|
|
787
|
+
super_classes = [c.text.decode("utf-8") for c in super_class_node.named_children]
|
|
788
|
+
|
|
789
|
+
decorators, _, _ = self._get_function_context(class_node)
|
|
790
|
+
|
|
791
|
+
docstring = None
|
|
792
|
+
docstring_node = self.get_node(captures_dict, "docstring")
|
|
793
|
+
if docstring_node:
|
|
794
|
+
docstring = docstring_node.text.decode("utf-8")
|
|
795
|
+
|
|
796
|
+
_id = (relative_path + "/" + class_name).replace("/", ".").replace(".py", "")
|
|
797
|
+
|
|
798
|
+
return ClassChunk(
|
|
799
|
+
id=_id,
|
|
800
|
+
name=class_name,
|
|
801
|
+
code=class_code,
|
|
802
|
+
docstring=docstring,
|
|
803
|
+
start_line=start_line,
|
|
804
|
+
end_line=end_line,
|
|
805
|
+
decorators=decorators,
|
|
806
|
+
superclasses=super_classes,
|
|
807
|
+
file_path=relative_path,
|
|
808
|
+
)
|
|
809
|
+
|
|
810
|
+
# ------------------------------------------------------------------
|
|
811
|
+
# Import extraction
|
|
812
|
+
# ------------------------------------------------------------------
|
|
813
|
+
|
|
814
|
+
def _extract_imports(self, captures_dict: dict, all_modules: dict, path: str | None) -> dict:
|
|
815
|
+
k, v = None, None
|
|
816
|
+
|
|
817
|
+
if "import.module" in captures_dict:
|
|
818
|
+
k = captures_dict["import.module"][0].text.decode("utf-8")
|
|
819
|
+
v = k
|
|
820
|
+
elif "import_from.module" in captures_dict and "import_from.name" in captures_dict:
|
|
821
|
+
k = captures_dict["import_from.name"][0].text.decode("utf-8")
|
|
822
|
+
v = captures_dict["import_from.module"][0].text.decode("utf-8") + "." + k
|
|
823
|
+
elif "import.aliased.name" in captures_dict and "import.aliased.alias" in captures_dict:
|
|
824
|
+
k = captures_dict["import.aliased.alias"][0].text.decode("utf-8")
|
|
825
|
+
v = captures_dict["import.aliased.name"][0].text.decode("utf-8")
|
|
826
|
+
elif (
|
|
827
|
+
"import_from.module" in captures_dict
|
|
828
|
+
and "import_from.aliased.name" in captures_dict
|
|
829
|
+
and "import_from.aliased.alias" in captures_dict
|
|
830
|
+
):
|
|
831
|
+
k = captures_dict["import_from.aliased.alias"][0].text.decode("utf-8")
|
|
832
|
+
v = (
|
|
833
|
+
captures_dict["import_from.module"][0].text.decode("utf-8")
|
|
834
|
+
+ "."
|
|
835
|
+
+ captures_dict["import_from.aliased.name"][0].text.decode("utf-8")
|
|
836
|
+
)
|
|
837
|
+
elif (
|
|
838
|
+
"import_from.relative_module" in captures_dict
|
|
839
|
+
and "import_from.relative_name" in captures_dict
|
|
840
|
+
):
|
|
841
|
+
k = captures_dict["import_from.relative_name"][0].text.decode("utf-8")
|
|
842
|
+
relative_module = captures_dict["import_from.relative_module"][0].text.decode("utf-8")
|
|
843
|
+
if path:
|
|
844
|
+
absolute_module = self._resolve_relative_import(relative_module, path, all_modules)
|
|
845
|
+
if absolute_module:
|
|
846
|
+
v = absolute_module + "." + k
|
|
847
|
+
elif (
|
|
848
|
+
"import_from.relative_module" in captures_dict
|
|
849
|
+
and "import_from.relative_aliased.name" in captures_dict
|
|
850
|
+
and "import_from.relative_aliased.alias" in captures_dict
|
|
851
|
+
):
|
|
852
|
+
k = captures_dict["import_from.relative_aliased.alias"][0].text.decode("utf-8")
|
|
853
|
+
relative_module = captures_dict["import_from.relative_module"][0].text.decode("utf-8")
|
|
854
|
+
original_name = captures_dict["import_from.relative_aliased.name"][0].text.decode(
|
|
855
|
+
"utf-8"
|
|
856
|
+
)
|
|
857
|
+
if path:
|
|
858
|
+
absolute_module = self._resolve_relative_import(relative_module, path, all_modules)
|
|
859
|
+
if absolute_module:
|
|
860
|
+
v = absolute_module + "." + original_name
|
|
861
|
+
|
|
862
|
+
if k is not None and v is not None and k != v:
|
|
863
|
+
if v in all_modules:
|
|
864
|
+
return {k: all_modules[v]}
|
|
865
|
+
module = ".".join(v.split(".")[:-1])
|
|
866
|
+
name = v.split(".")[-1]
|
|
867
|
+
if module in all_modules:
|
|
868
|
+
v = f"{all_modules[module]}.{name}"
|
|
869
|
+
return {k: v}
|
|
870
|
+
|
|
871
|
+
return {}
|
|
872
|
+
|
|
873
|
+
def _resolve_relative_import(
|
|
874
|
+
self, relative_module_text: str, current_file_path: str, all_modules: dict
|
|
875
|
+
) -> str | None:
|
|
876
|
+
dot_count = 0
|
|
877
|
+
for char in relative_module_text:
|
|
878
|
+
if char == ".":
|
|
879
|
+
dot_count += 1
|
|
880
|
+
else:
|
|
881
|
+
break
|
|
882
|
+
|
|
883
|
+
relative_module_name = relative_module_text[dot_count:]
|
|
884
|
+
current_module = current_file_path.replace("/", ".").replace(".py", "")
|
|
885
|
+
current_parts = current_module.split(".")
|
|
886
|
+
|
|
887
|
+
if dot_count > len(current_parts):
|
|
888
|
+
return None
|
|
889
|
+
|
|
890
|
+
parent_parts = current_parts[:-dot_count] if dot_count > 0 else current_parts[:-1]
|
|
891
|
+
|
|
892
|
+
if relative_module_name:
|
|
893
|
+
absolute_module = (
|
|
894
|
+
".".join(parent_parts) + "." + relative_module_name
|
|
895
|
+
if parent_parts
|
|
896
|
+
else relative_module_name
|
|
897
|
+
)
|
|
898
|
+
else:
|
|
899
|
+
absolute_module = ".".join(parent_parts)
|
|
900
|
+
|
|
901
|
+
return absolute_module
|
|
902
|
+
|
|
903
|
+
# ------------------------------------------------------------------
|
|
904
|
+
# Module docstring
|
|
905
|
+
# ------------------------------------------------------------------
|
|
906
|
+
|
|
907
|
+
def _extract_module_docstring(self, source: str) -> str | None:
|
|
908
|
+
tree = self.parser.parse(bytes(source, "utf8"))
|
|
909
|
+
for child in tree.root_node.children:
|
|
910
|
+
if child.type == "comment":
|
|
911
|
+
continue
|
|
912
|
+
if child.type == "expression_statement":
|
|
913
|
+
for sub in child.children:
|
|
914
|
+
if sub.type == "string" and sub.text is not None:
|
|
915
|
+
raw = sub.text.decode("utf-8")
|
|
916
|
+
for quote in ['"""', "'''"]:
|
|
917
|
+
if raw.startswith(quote) and raw.endswith(quote):
|
|
918
|
+
return raw[3:-3].strip()
|
|
919
|
+
for quote in ['"', "'"]:
|
|
920
|
+
if raw.startswith(quote) and raw.endswith(quote):
|
|
921
|
+
return raw[1:-1].strip()
|
|
922
|
+
return raw
|
|
923
|
+
return None
|
|
924
|
+
return None
|
|
925
|
+
|
|
926
|
+
# Keep old method name as alias for any direct callers
|
|
927
|
+
parse_module_docstring = _extract_module_docstring
|