Skip to content

Commit 86668ac

Browse files
committed
Merge branch 'feat/function-aliases-api' into release/0.19.0
2 parents b135cf7 + 47518ab commit 86668ac

6 files changed

Lines changed: 108 additions & 9 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
### Plugins
1616

17+
- `ModuleFunction.aliases` gives a project plugin the module-level names bound to a function by assigning one name to another, transitively and in source order, so an integration declaring a handler by the name a module binds (`handlers.main`) can report a name the engine resolves to no function; a method or a nested function has none. The cache format changes.
1718
- An `EntryPoint` naming a project function or class applies through a module-level alias of it: `main = actual` makes `python.app.handlers.main` name `actual`, so a deployment declaring `handlers.main` reaches the function doing the work. An alias is an assignment of one name to another at module level, followed transitively and in source order, in a conditional or a `try` as well; a name bound inside a function or class, or to a call, an import or anything but a name, is no alias, and the engine still infers nothing from a name. A model naming a method wins over one naming its class, whichever name it uses.
1819

1920
## 0.18.0 (2026-09-28)

‎docs/plugins.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,11 @@ module graph, the dependency graph, every advisory the plugins and advisory file
259259
contributed, the policy, the project root, and each module's imports, call graph and
260260
functions. `ctx.functions(module)` gives every function as the call graph names it
261261
(`Admin.get`, `outer.inner`, `<module>`), with its span, so a finding can be placed in
262-
one, and the label of the entry point it is (`http` for a route, `argv` for a command)
263-
when the models make it one. `ctx.call_graph(module)` gives each function's call sites
262+
one, the label of the entry point it is (`http` for a route, `argv` for a command)
263+
when the models make it one, and its `aliases`, the module-level names bound to it by
264+
assigning one name to another (`main = actual`), transitively and in source order, so
265+
an integration declaring a handler by the name a module binds can tell a name the
266+
engine resolves to no function; a method or a nested function has none. `ctx.call_graph(module)` gives each function's call sites
264267
(`sites(function)`) and the symbols it reads, called or not (`reads(function)`: reading
265268
`request.form['name']` reads `python.flask.request.form`), each where it first reads it.
266269
`ctx.templates` gives the templates found under the project's `templates` directories:

‎src/coretrace_python/cache.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
from coretrace_python.semantic.symbols import SymbolId
4343
from coretrace_python.source import SourceId, SourceSpan
4444

45-
CACHE_FORMAT = 10
45+
CACHE_FORMAT = 11
4646

4747

4848
@dataclass(frozen=True)
@@ -134,7 +134,7 @@ def store(self, key: str, module: CachedModule) -> None:
134134
def encode(module: CachedModule) -> dict[str, Any]:
135135
return {
136136
"format": CACHE_FORMAT,
137-
"functions": [[f.name, _encode_span(f.span), f.entry_point] for f in module.functions],
137+
"functions": [[f.name, _encode_span(f.span), f.entry_point, list(f.aliases)] for f in module.functions],
138138
"summaries": {name: _encode_summary(s) for name, s in module.summaries.items()},
139139
"sites": [_encode_site(site) for site in module.sites],
140140
"findings": [_encode_finding(finding) for finding in module.findings],
@@ -196,8 +196,13 @@ def _decode_span(data: Any) -> SourceSpan:
196196

197197

198198
def _decode_function(data: Any) -> ModuleFunction:
199-
name, span, entry_point = data
200-
return ModuleFunction(_string(name), _decode_span(span), None if entry_point is None else _string(entry_point))
199+
name, span, entry_point, aliases = data
200+
return ModuleFunction(
201+
_string(name),
202+
_decode_span(span),
203+
None if entry_point is None else _string(entry_point),
204+
tuple(_string(alias) for alias in aliases),
205+
)
201206

202207

203208
def _decode_read(data: Any) -> SymbolRead:

‎src/coretrace_python/interprocedural/callgraph.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,16 @@ def compute(cls, ctx: AnalysisContext) -> tuple[Signature, ...]:
132132
@dataclass(frozen=True)
133133
class ModuleFunction:
134134
"""One function of a module as a project plugin sees it: the name the call graph
135-
gives it (``Class.method``, ``outer.inner``, ``<module>``), where it is, and the label
136-
of the entry point it is (``http`` for a route, ``argv`` for a command), if any."""
135+
gives it (``Class.method``, ``outer.inner``, ``<module>``), where it is, the label
136+
of the entry point it is (``http`` for a route, ``argv`` for a command), if any, and
137+
the module-level ``aliases`` bound to it by assigning one name to another
138+
(``main = actual``), transitively and in source order; a method or a nested
139+
function has none."""
137140

138141
name: str
139142
span: SourceSpan
140143
entry_point: str | None = None
144+
aliases: tuple[str, ...] = ()
141145

142146

143147
class CallGraph:

‎src/coretrace_python/taint/engine.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,9 @@ def compute(cls, ctx: AnalysisContext) -> tuple[ModuleFunction, ...]:
11521152
functions: list[ModuleFunction] = []
11531153
for name, function in graph.definitions.items():
11541154
entry = function_entry_point(function, ctx.module, models, scopes, symbols, instances, routes, project)
1155-
functions.append(ModuleFunction(name, function.span, entry.label if entry is not None else None))
1155+
label = entry.label if entry is not None else None
1156+
# A dotted name, a method's or a nested function's, is bound by no assignment.
1157+
functions.append(ModuleFunction(name, function.span, label, tuple(_aliases(ctx.module, name))))
11561158
return tuple(functions)
11571159

11581160

‎tests/test_function_aliases_api.py‎

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
"""Project plugins see the module-level aliases of a function.
2+
3+
An integration reading a deployment declaration names its handler as the module binds
4+
it, ``handlers.main``, and the engine applies an entry point named that way to the
5+
function a module-level alias, ``main = actual``, denotes. ``ModuleFunction.aliases``
6+
gives a project plugin the names the module binds to each function that way, in source
7+
order and transitively, so it can report a declared name that names no function instead
8+
of trusting the assignment. A name bound to anything but a name is no alias, a method
9+
or a nested function has none, and the aliases survive the cache.
10+
"""
11+
12+
from __future__ import annotations
13+
14+
from pathlib import Path
15+
16+
from coretrace_python import engine
17+
from coretrace_python.cache import ProjectCache
18+
from coretrace_python.findings import Finding
19+
from coretrace_python.plugins import ProjectContext, ProjectPlugin
20+
21+
HANDLERS = (
22+
"def actual(event, context):\n"
23+
" pass\n\n"
24+
"handler = actual\n"
25+
"main = handler\n\n"
26+
"class Impl:\n"
27+
" def run(self, event, context):\n"
28+
" pass\n\n"
29+
"Worker = Impl\n\n"
30+
"def outer():\n"
31+
" def inner():\n"
32+
" pass\n"
33+
" return inner\n\n"
34+
"made = outer()\n"
35+
)
36+
37+
38+
class Peek(ProjectPlugin):
39+
name = "peek"
40+
41+
def __init__(self) -> None:
42+
self.seen: dict[str, tuple[str, ...]] = {}
43+
44+
def analyze_project(self, ctx: ProjectContext) -> tuple[Finding, ...]:
45+
for module in ctx.modules:
46+
for function in ctx.functions(module):
47+
self.seen[f"{module}:{function.name}"] = function.aliases
48+
return ()
49+
50+
51+
def project(tmp_path: Path) -> Path:
52+
root = tmp_path / "project"
53+
for relative, text in {"app/__init__.py": "", "app/handlers.py": HANDLERS}.items():
54+
(root / relative).parent.mkdir(parents=True, exist_ok=True)
55+
(root / relative).write_text(text, encoding="utf-8")
56+
return root
57+
58+
59+
def peek(root: Path, cache: ProjectCache | None = None) -> tuple[dict[str, tuple[str, ...]], engine.ProjectAnalysis]:
60+
plugin = Peek()
61+
analysis = engine.analyze_project(root, [engine.BUNDLED_PLUGINS], plugins=[plugin], cache=cache)
62+
return plugin.seen, analysis
63+
64+
65+
def test_a_function_lists_the_module_level_names_bound_to_it(tmp_path: Path) -> None:
66+
seen, _ = peek(project(tmp_path))
67+
68+
assert seen["app.handlers:actual"] == ("handler", "main")
69+
assert seen["app.handlers:Impl.run"] == ()
70+
assert seen["app.handlers:outer"] == ()
71+
assert seen["app.handlers:outer.inner"] == ()
72+
assert seen["app.handlers:<module>"] == ()
73+
74+
75+
def test_the_aliases_survive_the_cache(tmp_path: Path) -> None:
76+
root = project(tmp_path)
77+
cache = ProjectCache(tmp_path / "cache")
78+
79+
first, _ = peek(root, cache)
80+
second, analysis = peek(root, cache)
81+
82+
assert analysis.reused == ("app", "app.handlers")
83+
assert second == first
84+
assert second["app.handlers:actual"] == ("handler", "main")

0 commit comments

Comments
 (0)