deproc-python 0.1.0__tar.gz → 0.1.1__tar.gz
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.
- {deproc_python-0.1.0 → deproc_python-0.1.1}/PKG-INFO +5 -4
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/parser/main.py +1 -1
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/parser/utils/extraction.py +1 -1
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/resolver/main.py +2 -10
- deproc_python-0.1.1/deproc/plugins/python/utils/exports.py +16 -0
- deproc_python-0.1.1/deproc/plugins/python/utils/imports.py +14 -0
- deproc_python-0.1.1/deproc/plugins/python/utils/mro.py +34 -0
- deproc_python-0.1.1/deproc/plugins/python/utils/serialization.py +319 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc_python.egg-info/PKG-INFO +5 -4
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc_python.egg-info/SOURCES.txt +7 -0
- deproc_python-0.1.1/deproc_python.egg-info/requires.txt +4 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/pyproject.toml +6 -4
- deproc_python-0.1.1/tests/test_exports.py +35 -0
- deproc_python-0.1.1/tests/test_imports.py +29 -0
- deproc_python-0.1.1/tests/test_mro.py +46 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/tests/test_symbol_cache.py +0 -2
- deproc_python-0.1.0/deproc_python.egg-info/requires.txt +0 -3
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/__init__.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/linker/__init__.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/linker/main.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/linker/models.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/parser/__init__.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/parser/models.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/parser/utils/misc.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/parser/utils/tree_sitter_python.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/resolver/__init__.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/resolver/models.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/symbol_cache/__init__.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/symbol_cache/main.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/symbol_cache/models.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc_python.egg-info/dependency_links.txt +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/deproc_python.egg-info/top_level.txt +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/setup.cfg +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/tests/test_linker.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/tests/test_parser.py +0 -0
- {deproc_python-0.1.0 → deproc_python-0.1.1}/tests/test_resolver.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: deproc-python
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.1
|
|
4
4
|
Summary: Python language plugin for deproc.
|
|
5
5
|
Author-email: Gurveer Singh Virk <gurveervirk.15517@gmail.com>
|
|
6
6
|
License: Apache-2.0
|
|
@@ -17,6 +17,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.12
|
|
18
18
|
Classifier: Topic :: Software Development :: Libraries
|
|
19
19
|
Requires-Python: >=3.10
|
|
20
|
-
Requires-Dist: deproc-core
|
|
21
|
-
Requires-Dist: tree-sitter
|
|
22
|
-
Requires-Dist: tree-sitter
|
|
20
|
+
Requires-Dist: deproc-core>=0.1.0
|
|
21
|
+
Requires-Dist: deproc-utils-tree-sitter>=0.1.1
|
|
22
|
+
Requires-Dist: tree-sitter>=0.25.2
|
|
23
|
+
Requires-Dist: tree-sitter-python>=0.25.0
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from deproc.core.interfaces import SourceParser
|
|
2
2
|
from deproc.core.context import Context
|
|
3
|
-
from deproc.utils import iter_children, first_child
|
|
3
|
+
from deproc.utils.tree_walk import iter_children, first_child
|
|
4
4
|
from tree_sitter import Query, Node, QueryCursor
|
|
5
5
|
import os
|
|
6
6
|
|
{deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/parser/utils/extraction.py
RENAMED
|
@@ -14,7 +14,7 @@ from .tree_sitter_python import (
|
|
|
14
14
|
create_source_range
|
|
15
15
|
)
|
|
16
16
|
from tree_sitter import Node
|
|
17
|
-
from deproc.utils import iter_children, first_child
|
|
17
|
+
from deproc.utils.tree_walk import iter_children, first_child
|
|
18
18
|
|
|
19
19
|
def extract_docstring_range(node: Node, source_file_id: str | None = None) -> SourceRange | None:
|
|
20
20
|
if node.type == "module":
|
|
@@ -14,6 +14,7 @@ from ..parser.models import (
|
|
|
14
14
|
PythonImportAlias,
|
|
15
15
|
SymbolID,
|
|
16
16
|
)
|
|
17
|
+
from ..utils.imports import resolve_relative_import_path
|
|
17
18
|
import logging
|
|
18
19
|
|
|
19
20
|
logger = logging.getLogger(__name__)
|
|
@@ -66,16 +67,7 @@ class PythonResolver(Resolver[PythonResolverResult]):
|
|
|
66
67
|
parent_fqn = parent_module.fqn
|
|
67
68
|
if parent_fqn:
|
|
68
69
|
is_package = getattr(parent_module, "path", "").endswith(("__init__.py", "__init__.pyi"))
|
|
69
|
-
|
|
70
|
-
parent_parts = parent_fqn.split(".")
|
|
71
|
-
num_leading_dots = len(path) - len(path.lstrip("."))
|
|
72
|
-
levels_to_pop = num_leading_dots - (1 if is_package else 0)
|
|
73
|
-
for _ in range(levels_to_pop):
|
|
74
|
-
if parent_parts:
|
|
75
|
-
parent_parts.pop()
|
|
76
|
-
relative_parts = [p for p in relative_parts if p]
|
|
77
|
-
target_fqn = ".".join(parent_parts + relative_parts)
|
|
78
|
-
return target_fqn
|
|
70
|
+
return resolve_relative_import_path(path, parent_fqn, is_package)
|
|
79
71
|
|
|
80
72
|
return path
|
|
81
73
|
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
from typing import TYPE_CHECKING
|
|
3
|
+
from collections import defaultdict
|
|
4
|
+
from ..parser.models import PythonModule
|
|
5
|
+
|
|
6
|
+
if TYPE_CHECKING:
|
|
7
|
+
from deproc.core.runtime.registries.entity import EntityRegistry
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def build_module_exports(registry: EntityRegistry) -> dict[str, set[str]]:
|
|
11
|
+
exports: dict[str, set[str]] = defaultdict(set)
|
|
12
|
+
for entity in registry.values():
|
|
13
|
+
if isinstance(entity, PythonModule) and hasattr(entity, "all_exports") and entity.all_exports:
|
|
14
|
+
for name in entity.all_exports:
|
|
15
|
+
exports[entity.fqn].add(name)
|
|
16
|
+
return dict(exports)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
def resolve_relative_import_path(
|
|
2
|
+
relative_path: str,
|
|
3
|
+
parent_fqn: str,
|
|
4
|
+
parent_is_package: bool,
|
|
5
|
+
) -> str:
|
|
6
|
+
relative_parts = relative_path.split(".")
|
|
7
|
+
parent_parts = parent_fqn.split(".")
|
|
8
|
+
num_leading_dots = len(relative_path) - len(relative_path.lstrip("."))
|
|
9
|
+
levels_to_pop = num_leading_dots - (1 if parent_is_package else 0)
|
|
10
|
+
for _ in range(levels_to_pop):
|
|
11
|
+
if parent_parts:
|
|
12
|
+
parent_parts.pop()
|
|
13
|
+
relative_parts = [p for p in relative_parts if p]
|
|
14
|
+
return ".".join(parent_parts + relative_parts)
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
def c3_merge(seqs: list[list[str]]) -> list[str]:
|
|
2
|
+
result: list[str] = []
|
|
3
|
+
while True:
|
|
4
|
+
nonempty = [s for s in seqs if s]
|
|
5
|
+
if not nonempty:
|
|
6
|
+
return result
|
|
7
|
+
for seq in nonempty:
|
|
8
|
+
candidate = seq[0]
|
|
9
|
+
if not any(candidate in s[1:] for s in nonempty):
|
|
10
|
+
result.append(candidate)
|
|
11
|
+
for s in nonempty:
|
|
12
|
+
if s and s[0] == candidate:
|
|
13
|
+
s.pop(0)
|
|
14
|
+
break
|
|
15
|
+
else:
|
|
16
|
+
raise ValueError(f"Inconsistent MRO hierarchy: cannot merge {seqs}")
|
|
17
|
+
|
|
18
|
+
def compute_mro_from_bases(
|
|
19
|
+
self_fqn: str,
|
|
20
|
+
base_mros: dict[str, list[str] | None],
|
|
21
|
+
base_fqns: list[str],
|
|
22
|
+
) -> list[str] | None:
|
|
23
|
+
base_mro_lists: list[list[str]] = []
|
|
24
|
+
for fqn in base_fqns:
|
|
25
|
+
mro = base_mros.get(fqn)
|
|
26
|
+
if mro is None:
|
|
27
|
+
return None
|
|
28
|
+
base_mro_lists.append(list(mro))
|
|
29
|
+
|
|
30
|
+
merge_lists: list[list[str]] = base_mro_lists + [list(base_fqns)]
|
|
31
|
+
try:
|
|
32
|
+
return [self_fqn] + c3_merge(merge_lists)
|
|
33
|
+
except ValueError:
|
|
34
|
+
return None
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from deproc.core.interfaces.parser.models import (
|
|
4
|
+
ControlFlowBlock,
|
|
5
|
+
ControlFlowGroup,
|
|
6
|
+
Entity,
|
|
7
|
+
FunctionLike,
|
|
8
|
+
SourceRange,
|
|
9
|
+
TypeDefinition,
|
|
10
|
+
VariableDeclaration,
|
|
11
|
+
)
|
|
12
|
+
from deproc.plugins.python.parser.models import (
|
|
13
|
+
PythonClass,
|
|
14
|
+
PythonConstant,
|
|
15
|
+
PythonFunctionLike,
|
|
16
|
+
PythonImportAlias,
|
|
17
|
+
PythonImportStatement,
|
|
18
|
+
PythonModule,
|
|
19
|
+
PythonTypeAlias,
|
|
20
|
+
)
|
|
21
|
+
from deproc.plugins.python.linker.models import (
|
|
22
|
+
PythonNamespacePackage,
|
|
23
|
+
PythonPackage,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
TYPE_TO_CLASS = {
|
|
27
|
+
"FUNCTION": PythonFunctionLike,
|
|
28
|
+
"METHOD": PythonFunctionLike,
|
|
29
|
+
"CLASS": PythonClass,
|
|
30
|
+
"IMPORT_ALIAS": PythonImportAlias,
|
|
31
|
+
"IMPORT_STATEMENT": PythonImportStatement,
|
|
32
|
+
"CONSTANT": PythonConstant,
|
|
33
|
+
"TYPE_ALIAS": PythonTypeAlias,
|
|
34
|
+
"VARIABLE": VariableDeclaration,
|
|
35
|
+
"MODULE": PythonModule,
|
|
36
|
+
"PACKAGE": PythonPackage,
|
|
37
|
+
"NAMESPACE_PACKAGE": PythonNamespacePackage,
|
|
38
|
+
"CONTROL_FLOW_BLOCK": ControlFlowBlock,
|
|
39
|
+
"CONTROL_FLOW_GROUP": ControlFlowGroup,
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
def _module_fqn(full_path: str) -> str | None:
|
|
43
|
+
parts = full_path.rsplit(".", 1)
|
|
44
|
+
return parts[0] if len(parts) > 1 else None
|
|
45
|
+
|
|
46
|
+
def _get_module_fqn_from_registry(entity_id: str, registry: dict) -> str | None:
|
|
47
|
+
seen: set[str] = set()
|
|
48
|
+
current = entity_id
|
|
49
|
+
while current and current not in seen:
|
|
50
|
+
seen.add(current)
|
|
51
|
+
entity = registry.get(current)
|
|
52
|
+
if entity is None:
|
|
53
|
+
break
|
|
54
|
+
if hasattr(entity, "fqn") and entity.fqn:
|
|
55
|
+
return entity.fqn
|
|
56
|
+
current = getattr(entity, "parent_id", None)
|
|
57
|
+
return None
|
|
58
|
+
|
|
59
|
+
def entity_to_record(entity, language: str = "python", module_exports: dict[str, set[str]] | None = None, registry=None) -> dict | None:
|
|
60
|
+
if isinstance(entity, PythonImportAlias):
|
|
61
|
+
name = entity.alias or entity.name
|
|
62
|
+
if not entity.fqn:
|
|
63
|
+
return None
|
|
64
|
+
full_path = entity.fqn
|
|
65
|
+
entity_type = "IMPORT_ALIAS"
|
|
66
|
+
elif isinstance(entity, PythonImportStatement):
|
|
67
|
+
name = entity.path
|
|
68
|
+
entity_type = "IMPORT_STATEMENT"
|
|
69
|
+
module_fqn = None
|
|
70
|
+
if entity.parent_id and registry:
|
|
71
|
+
parent = registry.get(entity.parent_id)
|
|
72
|
+
if parent and hasattr(parent, "fqn"):
|
|
73
|
+
module_fqn = parent.fqn
|
|
74
|
+
full_path = f"{module_fqn}.__import__.{entity.id}" if module_fqn else entity.id
|
|
75
|
+
elif isinstance(entity, (PythonConstant, PythonTypeAlias)):
|
|
76
|
+
vb = getattr(entity, "variable_binding", None)
|
|
77
|
+
if not vb:
|
|
78
|
+
return None
|
|
79
|
+
name = vb.name
|
|
80
|
+
full_path = vb.fqn or name
|
|
81
|
+
entity_type = "CONSTANT" if isinstance(entity, PythonConstant) else "TYPE_ALIAS"
|
|
82
|
+
elif isinstance(entity, VariableDeclaration):
|
|
83
|
+
vb = getattr(entity, "variable_binding", None)
|
|
84
|
+
if not vb:
|
|
85
|
+
return None
|
|
86
|
+
name = vb.name
|
|
87
|
+
full_path = vb.fqn or name
|
|
88
|
+
entity_type = "VARIABLE"
|
|
89
|
+
elif isinstance(entity, PythonModule):
|
|
90
|
+
name = entity.fqn.split(".")[-1] if entity.fqn else Path(entity.path).stem
|
|
91
|
+
full_path = entity.fqn or entity.path
|
|
92
|
+
entity_type = "PACKAGE" if isinstance(entity, PythonPackage) else "MODULE"
|
|
93
|
+
elif isinstance(entity, PythonNamespacePackage):
|
|
94
|
+
name = entity.fqn.split(".")[-1] if entity.fqn else Path(entity.path).stem
|
|
95
|
+
full_path = entity.fqn or entity.path
|
|
96
|
+
entity_type = "NAMESPACE_PACKAGE"
|
|
97
|
+
elif isinstance(entity, (FunctionLike, TypeDefinition)):
|
|
98
|
+
name = entity.name
|
|
99
|
+
full_path = entity.fqn
|
|
100
|
+
entity_type = entity.type
|
|
101
|
+
elif isinstance(entity, ControlFlowBlock):
|
|
102
|
+
name = entity.branch
|
|
103
|
+
entity_type = "CONTROL_FLOW_BLOCK"
|
|
104
|
+
module_fqn = None
|
|
105
|
+
if entity.parent_id and registry:
|
|
106
|
+
module_fqn = _get_module_fqn_from_registry(entity.parent_id, registry)
|
|
107
|
+
full_path = f"{module_fqn}.__branch__.{entity.branch}.{entity.source_range.lineno}" if module_fqn else f"__branch__.{entity.branch}.{entity.source_range.lineno}"
|
|
108
|
+
elif isinstance(entity, ControlFlowGroup):
|
|
109
|
+
name = entity.group_type
|
|
110
|
+
entity_type = "CONTROL_FLOW_GROUP"
|
|
111
|
+
module_fqn = None
|
|
112
|
+
if entity.parent_id and registry:
|
|
113
|
+
module_fqn = _get_module_fqn_from_registry(entity.parent_id, registry)
|
|
114
|
+
full_path = f"{module_fqn}.__group__.{entity.group_type}.{entity.source_range.lineno}" if module_fqn else f"__group__.{entity.group_type}.{entity.source_range.lineno}"
|
|
115
|
+
else:
|
|
116
|
+
return None
|
|
117
|
+
|
|
118
|
+
if not name or not full_path:
|
|
119
|
+
return None
|
|
120
|
+
|
|
121
|
+
metadata = {}
|
|
122
|
+
sr = entity.source_range if hasattr(entity, "source_range") else None
|
|
123
|
+
if sr:
|
|
124
|
+
metadata["lineno"] = sr.lineno
|
|
125
|
+
metadata["end_lineno"] = sr.end_lineno
|
|
126
|
+
metadata["col_offset"] = sr.col_offset
|
|
127
|
+
metadata["end_col_offset"] = sr.end_col_offset
|
|
128
|
+
metadata["source_id"] = sr.source_id
|
|
129
|
+
if isinstance(entity, PythonImportAlias):
|
|
130
|
+
metadata["original_name"] = entity.name
|
|
131
|
+
if entity.alias:
|
|
132
|
+
metadata["alias"] = entity.alias
|
|
133
|
+
if isinstance(entity, PythonImportStatement):
|
|
134
|
+
metadata["import_type"] = entity.type
|
|
135
|
+
metadata["wildcard"] = entity.wildcard
|
|
136
|
+
metadata["name_ids"] = entity.name_ids
|
|
137
|
+
if isinstance(entity, ControlFlowBlock):
|
|
138
|
+
if entity.condition_range:
|
|
139
|
+
cr = entity.condition_range
|
|
140
|
+
metadata["condition_lineno"] = cr.lineno
|
|
141
|
+
metadata["condition_end_lineno"] = cr.end_lineno
|
|
142
|
+
metadata["condition_col_offset"] = cr.col_offset
|
|
143
|
+
metadata["condition_end_col_offset"] = cr.end_col_offset
|
|
144
|
+
metadata["import_stmt_ids"] = entity.import_stmt_ids
|
|
145
|
+
metadata["type_ids"] = entity.type_ids
|
|
146
|
+
metadata["function_ids"] = entity.function_ids
|
|
147
|
+
metadata["variable_ids"] = entity.variable_ids
|
|
148
|
+
metadata["nested_group_ids"] = entity.nested_group_ids
|
|
149
|
+
if isinstance(entity, ControlFlowGroup):
|
|
150
|
+
metadata["block_ids"] = entity.block_ids
|
|
151
|
+
if hasattr(entity, "fqn") and entity.fqn:
|
|
152
|
+
metadata["fqn"] = entity.fqn
|
|
153
|
+
if hasattr(entity, "path"):
|
|
154
|
+
metadata["path"] = entity.path
|
|
155
|
+
if isinstance(entity, PythonModule) and entity.all_exports:
|
|
156
|
+
metadata["all_exports"] = entity.all_exports
|
|
157
|
+
if hasattr(entity, "visibility") and entity.visibility:
|
|
158
|
+
metadata["visibility"] = entity.visibility
|
|
159
|
+
if hasattr(entity, "docstring_range") and entity.docstring_range:
|
|
160
|
+
dr = entity.docstring_range
|
|
161
|
+
metadata["docstring_lineno"] = dr.lineno
|
|
162
|
+
metadata["docstring_end_lineno"] = dr.end_lineno
|
|
163
|
+
if isinstance(entity, FunctionLike):
|
|
164
|
+
if hasattr(entity, "signature") and entity.signature:
|
|
165
|
+
sig = entity.signature
|
|
166
|
+
metadata["signature_lineno"] = sig.signature_range.lineno
|
|
167
|
+
metadata["signature_end_lineno"] = sig.signature_range.end_lineno
|
|
168
|
+
if sig.arguments_range:
|
|
169
|
+
metadata["arguments_lineno"] = sig.arguments_range.lineno
|
|
170
|
+
metadata["arguments_end_lineno"] = sig.arguments_range.end_lineno
|
|
171
|
+
if sig.return_type_range:
|
|
172
|
+
metadata["return_type_lineno"] = sig.return_type_range.lineno
|
|
173
|
+
metadata["return_type_end_lineno"] = sig.return_type_range.end_lineno
|
|
174
|
+
if hasattr(entity, "annotations") and entity.annotations:
|
|
175
|
+
metadata["decorators"] = [a.name for a in entity.annotations]
|
|
176
|
+
if isinstance(entity, TypeDefinition):
|
|
177
|
+
if hasattr(entity, "inherits") and entity.inherits:
|
|
178
|
+
metadata["parent_classes"] = entity.inherits
|
|
179
|
+
if hasattr(entity, "method_ids") and entity.method_ids:
|
|
180
|
+
metadata["method_ids"] = entity.method_ids
|
|
181
|
+
if hasattr(entity, "inner_type_ids") and entity.inner_type_ids:
|
|
182
|
+
metadata["inner_type_ids"] = entity.inner_type_ids
|
|
183
|
+
if hasattr(entity, "property_ids") and entity.property_ids:
|
|
184
|
+
metadata["property_ids"] = entity.property_ids
|
|
185
|
+
if isinstance(entity, VariableDeclaration):
|
|
186
|
+
if hasattr(entity, "type_annotation") and entity.type_annotation:
|
|
187
|
+
ta = entity.type_annotation
|
|
188
|
+
metadata["type_annotation_lineno"] = ta.lineno
|
|
189
|
+
metadata["type_annotation_end_lineno"] = ta.end_lineno
|
|
190
|
+
if hasattr(entity, "modifiers") and entity.modifiers:
|
|
191
|
+
metadata["modifiers"] = entity.modifiers
|
|
192
|
+
|
|
193
|
+
mfqn = entity.fqn if isinstance(entity, (PythonModule, PythonNamespacePackage)) else _module_fqn(full_path)
|
|
194
|
+
if mfqn and name in (module_exports or {}).get(mfqn, set()):
|
|
195
|
+
metadata["exported"] = True
|
|
196
|
+
|
|
197
|
+
return {
|
|
198
|
+
"id": entity.id,
|
|
199
|
+
"language": language,
|
|
200
|
+
"full_path": full_path,
|
|
201
|
+
"name": name,
|
|
202
|
+
"type": entity_type,
|
|
203
|
+
"metadata_json": json.dumps(metadata, default=str),
|
|
204
|
+
"parent_id": getattr(entity, "parent_id", None),
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
def record_to_entity(record: dict) -> Entity | None:
|
|
208
|
+
entity_class = TYPE_TO_CLASS.get(record["type"])
|
|
209
|
+
if entity_class is None:
|
|
210
|
+
return None
|
|
211
|
+
meta = json.loads(record["metadata_json"])
|
|
212
|
+
sr = SourceRange(
|
|
213
|
+
lineno=meta.get("lineno", 0),
|
|
214
|
+
end_lineno=meta.get("end_lineno", 0),
|
|
215
|
+
col_offset=meta.get("col_offset", 0),
|
|
216
|
+
end_col_offset=meta.get("end_col_offset", 0),
|
|
217
|
+
)
|
|
218
|
+
common = {
|
|
219
|
+
"id": record["id"],
|
|
220
|
+
"fqn": meta.get("fqn") or record["full_path"],
|
|
221
|
+
}
|
|
222
|
+
parent_id = record.get("parent_id") or meta.get("parent_id")
|
|
223
|
+
if entity_class is PythonImportAlias:
|
|
224
|
+
return PythonImportAlias(
|
|
225
|
+
name=meta.get("original_name", ""),
|
|
226
|
+
alias=meta.get("alias"),
|
|
227
|
+
parent_id=parent_id,
|
|
228
|
+
source_range=sr,
|
|
229
|
+
**common,
|
|
230
|
+
)
|
|
231
|
+
if entity_class is PythonImportStatement:
|
|
232
|
+
return PythonImportStatement(
|
|
233
|
+
path=record["name"],
|
|
234
|
+
type=meta.get("import_type", ""),
|
|
235
|
+
wildcard=meta.get("wildcard", False),
|
|
236
|
+
name_ids=meta.get("name_ids", []),
|
|
237
|
+
parent_id=parent_id,
|
|
238
|
+
source_range=sr,
|
|
239
|
+
id=record["id"],
|
|
240
|
+
)
|
|
241
|
+
if entity_class is PythonFunctionLike:
|
|
242
|
+
return PythonFunctionLike(
|
|
243
|
+
name=record["name"],
|
|
244
|
+
source_range=sr,
|
|
245
|
+
docstring_range=None,
|
|
246
|
+
signature=None,
|
|
247
|
+
type=record["type"],
|
|
248
|
+
annotations=[],
|
|
249
|
+
visibility=meta.get("visibility"),
|
|
250
|
+
**common,
|
|
251
|
+
)
|
|
252
|
+
if entity_class is PythonClass:
|
|
253
|
+
return PythonClass(
|
|
254
|
+
name=record["name"],
|
|
255
|
+
source_range=sr,
|
|
256
|
+
docstring_range=None,
|
|
257
|
+
visibility=meta.get("visibility"),
|
|
258
|
+
**common,
|
|
259
|
+
)
|
|
260
|
+
if entity_class is PythonModule:
|
|
261
|
+
return PythonModule(
|
|
262
|
+
all_exports=meta.get("all_exports"),
|
|
263
|
+
path=meta.get("path", ""),
|
|
264
|
+
source="",
|
|
265
|
+
docstring_range=None,
|
|
266
|
+
**common,
|
|
267
|
+
)
|
|
268
|
+
if entity_class is PythonPackage:
|
|
269
|
+
return PythonPackage(
|
|
270
|
+
submodule_ids=meta.get("submodule_ids", []),
|
|
271
|
+
all_exports=meta.get("all_exports"),
|
|
272
|
+
path=meta.get("path", ""),
|
|
273
|
+
source="",
|
|
274
|
+
docstring_range=None,
|
|
275
|
+
**common,
|
|
276
|
+
)
|
|
277
|
+
if entity_class is PythonNamespacePackage:
|
|
278
|
+
return PythonNamespacePackage(
|
|
279
|
+
path=meta.get("path", ""),
|
|
280
|
+
submodule_ids=meta.get("submodule_ids", []),
|
|
281
|
+
**common,
|
|
282
|
+
)
|
|
283
|
+
null_fields = {"variable_binding": None, "value_range": None, "type_annotation": None, "modifiers": []}
|
|
284
|
+
if entity_class is PythonConstant:
|
|
285
|
+
return PythonConstant(source_range=sr, **null_fields, **common)
|
|
286
|
+
if entity_class is PythonTypeAlias:
|
|
287
|
+
return PythonTypeAlias(source_range=sr, **null_fields, **common)
|
|
288
|
+
if entity_class is VariableDeclaration:
|
|
289
|
+
return VariableDeclaration(source_range=sr, **null_fields, **common)
|
|
290
|
+
if entity_class is ControlFlowBlock:
|
|
291
|
+
condition_range = None
|
|
292
|
+
if "condition_lineno" in meta:
|
|
293
|
+
condition_range = SourceRange(
|
|
294
|
+
lineno=meta["condition_lineno"],
|
|
295
|
+
end_lineno=meta["condition_end_lineno"],
|
|
296
|
+
col_offset=meta["condition_col_offset"],
|
|
297
|
+
end_col_offset=meta["condition_end_col_offset"],
|
|
298
|
+
)
|
|
299
|
+
return ControlFlowBlock(
|
|
300
|
+
id=record["id"],
|
|
301
|
+
parent_id=parent_id,
|
|
302
|
+
branch=record["name"],
|
|
303
|
+
source_range=sr,
|
|
304
|
+
condition_range=condition_range,
|
|
305
|
+
import_stmt_ids=meta.get("import_stmt_ids", []),
|
|
306
|
+
type_ids=meta.get("type_ids", []),
|
|
307
|
+
function_ids=meta.get("function_ids", []),
|
|
308
|
+
variable_ids=meta.get("variable_ids", []),
|
|
309
|
+
nested_group_ids=meta.get("nested_group_ids", []),
|
|
310
|
+
)
|
|
311
|
+
if entity_class is ControlFlowGroup:
|
|
312
|
+
return ControlFlowGroup(
|
|
313
|
+
id=record["id"],
|
|
314
|
+
parent_id=parent_id,
|
|
315
|
+
group_type=record["name"],
|
|
316
|
+
source_range=sr,
|
|
317
|
+
block_ids=meta.get("block_ids", []),
|
|
318
|
+
)
|
|
319
|
+
return None
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: deproc-python
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.1
|
|
4
4
|
Summary: Python language plugin for deproc.
|
|
5
5
|
Author-email: Gurveer Singh Virk <gurveervirk.15517@gmail.com>
|
|
6
6
|
License: Apache-2.0
|
|
@@ -17,6 +17,7 @@ Classifier: Programming Language :: Python :: 3.11
|
|
|
17
17
|
Classifier: Programming Language :: Python :: 3.12
|
|
18
18
|
Classifier: Topic :: Software Development :: Libraries
|
|
19
19
|
Requires-Python: >=3.10
|
|
20
|
-
Requires-Dist: deproc-core
|
|
21
|
-
Requires-Dist: tree-sitter
|
|
22
|
-
Requires-Dist: tree-sitter
|
|
20
|
+
Requires-Dist: deproc-core>=0.1.0
|
|
21
|
+
Requires-Dist: deproc-utils-tree-sitter>=0.1.1
|
|
22
|
+
Requires-Dist: tree-sitter>=0.25.2
|
|
23
|
+
Requires-Dist: tree-sitter-python>=0.25.0
|
|
@@ -15,12 +15,19 @@ deproc/plugins/python/resolver/models.py
|
|
|
15
15
|
deproc/plugins/python/symbol_cache/__init__.py
|
|
16
16
|
deproc/plugins/python/symbol_cache/main.py
|
|
17
17
|
deproc/plugins/python/symbol_cache/models.py
|
|
18
|
+
deproc/plugins/python/utils/exports.py
|
|
19
|
+
deproc/plugins/python/utils/imports.py
|
|
20
|
+
deproc/plugins/python/utils/mro.py
|
|
21
|
+
deproc/plugins/python/utils/serialization.py
|
|
18
22
|
deproc_python.egg-info/PKG-INFO
|
|
19
23
|
deproc_python.egg-info/SOURCES.txt
|
|
20
24
|
deproc_python.egg-info/dependency_links.txt
|
|
21
25
|
deproc_python.egg-info/requires.txt
|
|
22
26
|
deproc_python.egg-info/top_level.txt
|
|
27
|
+
tests/test_exports.py
|
|
28
|
+
tests/test_imports.py
|
|
23
29
|
tests/test_linker.py
|
|
30
|
+
tests/test_mro.py
|
|
24
31
|
tests/test_parser.py
|
|
25
32
|
tests/test_resolver.py
|
|
26
33
|
tests/test_symbol_cache.py
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "deproc-python"
|
|
7
|
-
version = "0.1.
|
|
7
|
+
version = "0.1.1"
|
|
8
8
|
description = "Python language plugin for deproc."
|
|
9
9
|
authors = [
|
|
10
10
|
{ name = "Gurveer Singh Virk", email = "gurveervirk.15517@gmail.com" }
|
|
@@ -12,9 +12,10 @@ authors = [
|
|
|
12
12
|
license = {text = "Apache-2.0"}
|
|
13
13
|
requires-python = ">=3.10"
|
|
14
14
|
dependencies = [
|
|
15
|
-
"deproc-core
|
|
16
|
-
"tree-sitter
|
|
17
|
-
"tree-sitter
|
|
15
|
+
"deproc-core>=0.1.0",
|
|
16
|
+
"deproc-utils-tree-sitter>=0.1.1",
|
|
17
|
+
"tree-sitter>=0.25.2",
|
|
18
|
+
"tree-sitter-python>=0.25.0",
|
|
18
19
|
]
|
|
19
20
|
classifiers = [
|
|
20
21
|
"Development Status :: 3 - Alpha",
|
|
@@ -39,3 +40,4 @@ include = ["deproc.plugins.python*"]
|
|
|
39
40
|
|
|
40
41
|
[tool.uv.sources]
|
|
41
42
|
deproc-core = { workspace = true }
|
|
43
|
+
deproc-utils-tree-sitter = { workspace = true }
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
from deproc.plugins.python.utils.exports import build_module_exports
|
|
2
|
+
from deproc.core.runtime.registries.entity import EntityRegistry
|
|
3
|
+
from deproc.plugins.python.parser.models import PythonModule
|
|
4
|
+
|
|
5
|
+
def _make_module(fqn, all_exports=None):
|
|
6
|
+
m = PythonModule(
|
|
7
|
+
fqn=fqn,
|
|
8
|
+
path=f"{fqn.replace('.', '/')}.py",
|
|
9
|
+
docstring_range=None,
|
|
10
|
+
source="# placeholder",
|
|
11
|
+
)
|
|
12
|
+
m.all_exports = all_exports
|
|
13
|
+
return m
|
|
14
|
+
|
|
15
|
+
class TestBuildModuleExports:
|
|
16
|
+
def test_with_all_exports(self):
|
|
17
|
+
reg = EntityRegistry()
|
|
18
|
+
m = _make_module("pkg.mod", ["foo", "bar"])
|
|
19
|
+
reg.add(m)
|
|
20
|
+
result = build_module_exports(reg)
|
|
21
|
+
assert result == {"pkg.mod": {"foo", "bar"}}
|
|
22
|
+
|
|
23
|
+
def test_without_all_exports(self):
|
|
24
|
+
reg = EntityRegistry()
|
|
25
|
+
m = _make_module("pkg.mod")
|
|
26
|
+
reg.add(m)
|
|
27
|
+
result = build_module_exports(reg)
|
|
28
|
+
assert result == {}
|
|
29
|
+
|
|
30
|
+
def test_ignores_non_module_entities(self):
|
|
31
|
+
reg = EntityRegistry()
|
|
32
|
+
func = PythonModule(fqn="pkg.func", path="pkg/func.py", docstring_range=None, source="# x")
|
|
33
|
+
reg.add(func)
|
|
34
|
+
result = build_module_exports(reg)
|
|
35
|
+
assert result == {}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
from deproc.plugins.python.utils.imports import resolve_relative_import_path
|
|
2
|
+
|
|
3
|
+
class TestResolveRelativeImportPath:
|
|
4
|
+
def test_single_dot_module(self):
|
|
5
|
+
assert resolve_relative_import_path(".sibling", "pkg.sub.mod", False) == "pkg.sub.sibling"
|
|
6
|
+
|
|
7
|
+
def test_single_dot_nested(self):
|
|
8
|
+
assert resolve_relative_import_path(".subpkg.module", "pkg.sub.mod", False) == "pkg.sub.subpkg.module"
|
|
9
|
+
|
|
10
|
+
def test_double_dot(self):
|
|
11
|
+
assert resolve_relative_import_path("..sibling", "pkg.sub.mod", False) == "pkg.sibling"
|
|
12
|
+
|
|
13
|
+
def test_triple_dot(self):
|
|
14
|
+
assert resolve_relative_import_path("...sibling", "pkg.sub.mod", False) == "sibling"
|
|
15
|
+
|
|
16
|
+
def test_single_dot_in_package(self):
|
|
17
|
+
assert resolve_relative_import_path(".module", "package", True) == "package.module"
|
|
18
|
+
|
|
19
|
+
def test_double_dot_in_package(self):
|
|
20
|
+
assert resolve_relative_import_path("..other", "package.subpackage", True) == "package.other"
|
|
21
|
+
|
|
22
|
+
def test_triple_dot_in_package(self):
|
|
23
|
+
assert resolve_relative_import_path("...sibling", "pkg.sub.mod", True) == "pkg.sibling"
|
|
24
|
+
|
|
25
|
+
def test_empty_parent_parts(self):
|
|
26
|
+
assert resolve_relative_import_path("...sibling", "pkg", True) == "sibling"
|
|
27
|
+
|
|
28
|
+
def test_quadruple_dot(self):
|
|
29
|
+
assert resolve_relative_import_path("....deep", "a.b.c.d.e", False) == "a.deep"
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
from deproc.plugins.python.utils.mro import c3_merge, compute_mro_from_bases
|
|
3
|
+
|
|
4
|
+
class TestC3Merge:
|
|
5
|
+
def test_simple_merge(self):
|
|
6
|
+
assert c3_merge([["B", "O"], ["C", "O"], ["B", "C"]]) == ["B", "C", "O"]
|
|
7
|
+
|
|
8
|
+
def test_inconsistent_hierarchy_raises(self):
|
|
9
|
+
with pytest.raises(ValueError):
|
|
10
|
+
c3_merge([["X", "O"], ["Y", "O"], ["Y", "X"], ["X", "Y"]])
|
|
11
|
+
|
|
12
|
+
def test_empty_lists(self):
|
|
13
|
+
assert c3_merge([]) == []
|
|
14
|
+
|
|
15
|
+
def test_single_list(self):
|
|
16
|
+
assert c3_merge([["A", "B", "C"]]) == ["A", "B", "C"]
|
|
17
|
+
|
|
18
|
+
class TestComputeMroFromBases:
|
|
19
|
+
def test_single_inheritance(self):
|
|
20
|
+
result = compute_mro_from_bases("Child", {"Base": ["Base"]}, ["Base"])
|
|
21
|
+
assert result == ["Child", "Base"]
|
|
22
|
+
|
|
23
|
+
def test_diamond(self):
|
|
24
|
+
base_mros = {"Base": ["Base", "object"], "Mixin": ["Mixin", "object"]}
|
|
25
|
+
base_fqns = ["Base", "Mixin"]
|
|
26
|
+
result = compute_mro_from_bases("Child", base_mros, base_fqns)
|
|
27
|
+
assert result is not None
|
|
28
|
+
assert result[0] == "Child"
|
|
29
|
+
assert result.index("Base") < result.index("Mixin")
|
|
30
|
+
assert "object" in result
|
|
31
|
+
|
|
32
|
+
def test_unresolved_base_returns_none(self):
|
|
33
|
+
result = compute_mro_from_bases("Child", {"Base": None}, ["Base"])
|
|
34
|
+
assert result is None
|
|
35
|
+
|
|
36
|
+
def test_no_bases(self):
|
|
37
|
+
result = compute_mro_from_bases("Standalone", {}, [])
|
|
38
|
+
assert result == ["Standalone"]
|
|
39
|
+
|
|
40
|
+
def test_inconsistent_returns_none(self):
|
|
41
|
+
result = compute_mro_from_bases(
|
|
42
|
+
"Child",
|
|
43
|
+
{"A": ["A", "O"], "B": ["B", "O"]},
|
|
44
|
+
["A", "B", "O"],
|
|
45
|
+
)
|
|
46
|
+
assert result is None
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{deproc_python-0.1.0 → deproc_python-0.1.1}/deproc/plugins/python/parser/utils/tree_sitter_python.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|