mirror of
https://github.com/abhigyanpatwari/GitNexus.git
synced 2026-11-30 23:47:58 +00:00
* fix(python): resolve dotted imports into src-layout roots (#3949) A dotted absolute import from tests/, scripts/ or another directory outside a src/ root was rejected by the external-import guard, so those files got no IMPORTS or CALLS edges. Add a src-root tier for dotted targets: probe <ancestor>/src/<path> and then src/<path>, closest root first, after the ancestor walk and before the suffix fallback, and as the answer when the guard rejects the target. Only exact files match, so external imports stay unresolved. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(python): link a bare class call to the class it constructs (#3950) Python spells construction as an ordinary call, Plain(1), so the site is a free call. The free-call pass only accepts callables, so the class binding was recorded as a construction and then dropped with no edge. When a provider declares constructionSyntax.bare (only Python does), a free call whose name binds exactly one class and no callable now links through the existing constructor target choice. Python has no Constructor nodes, so the edge targets the Class, as the dotted models.Plain() form already did. The Python scope-resolution baselines gain one cross-file CALLS edge per generated file. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(python): honor a literal __all__ in star imports (#3951) A call through `from m import *` already resolves for modules without __all__ (#3504), but any module that binds __all__ was declined, so its star imports bound nothing. Python's import statement binds exactly the names in __all__ when a module defines it. Record __all__ on the Python capture side channel when every occurrence is a module-level = or += of a list or tuple of string literals, and expand a star import of that module to the listed names it defines or imports, underscore names included. Every other __all__ form keeps the decline. Pin the issue's bare call through a star import as well. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * refactor(python): avoid substring and token copies in src and __all__ scans Compare the src parent name with startsWith instead of slicing it, and check an identifier's length before reading its text when looking for __all__. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(review): apply review findings - Skip a src/ root that is itself a package (src/__init__.py): it is imported as src.x, so email.utils from tests/ must not bind to src/email/utils.py. - Link a bare class call only when every binding of the name is that class; a value from another star import (Shadowed = 1) leaves it unresolved. - Read a restored literal __all__ only from a python-capture snapshot. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * perf(python): keep the import gate and src tier linear in importer depth Both ancestor walks hashed a fresh `${ancestor}/${segment}/` key per step, so a non-matching walk cost O(depth^2) characters per import. The index now maps a directory name to its parent directories (and, for the src tier, to the directories holding its src/), and each step looks up the memoized ancestor string, whose hash is cached. Resolution results are unchanged: the import-target fingerprint and the prefix-set equivalence test pass as before. Measured on 1000 imports from an importer d directories deep: d=64 went from 23.2 ms to 1.7 ms, and each doubling of d now costs 0.5x-0.8x of linear instead of 1.6x-1.8x. A literal __all__ built by many += statements no longer copies the list on each statement. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(python): decline a literal __all__ that starts with += A += before any literal = extends a value bound somewhere this file does not show, such as a star import, so the full export list is unknown. Recording only the right-hand side made a star import of the module drop the inherited names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Gergo Magyar <gergomagyar0@gmail.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
192 lines
6.2 KiB
TypeScript
192 lines
6.2 KiB
TypeScript
/**
|
|
* #3950: a language whose free call constructs (`constructionSyntax.bare`,
|
|
* Python `Plain(1)`) links a bare call bound to exactly one class to that
|
|
* Class. Without the flag, or when the name binds several classes, the call
|
|
* still emits no edge.
|
|
*/
|
|
import { describe, it, expect } from 'vitest';
|
|
import {
|
|
buildDefIndex,
|
|
buildMethodDispatchIndex,
|
|
buildModuleScopeIndex,
|
|
buildQualifiedNameIndex,
|
|
buildScopeTree,
|
|
createEagerImportVisibility,
|
|
type NodeLabel,
|
|
type ParsedFile,
|
|
type Range,
|
|
type ReferenceSite,
|
|
type Scope,
|
|
type ScopeId,
|
|
type SymbolDefinition,
|
|
} from 'gitnexus-shared';
|
|
import { createKnowledgeGraph } from '../../../src/core/graph/graph.js';
|
|
import type { ScopeResolutionIndexes } from '../../../src/core/ingestion/model/scope-resolution-indexes.js';
|
|
import { buildGraphNodeLookup } from '../../../src/core/ingestion/scope-resolution/graph-bridge/node-lookup.js';
|
|
import { emitFreeCallFallback } from '../../../src/core/ingestion/scope-resolution/passes/free-call-fallback.js';
|
|
import { buildWorkspaceResolutionIndex } from '../../../src/core/ingestion/scope-resolution/workspace-index.js';
|
|
import { createSemanticModel } from '../../../src/core/ingestion/model/semantic-model.js';
|
|
import type { SourceBindingResolution } from '../../../src/core/ingestion/scope-resolution/source-binding-resolution.js';
|
|
|
|
const CALLER_FILE = 'core.py';
|
|
|
|
const range = (sl: number, sc: number, el = sl, ec = sc + 6): Range => ({
|
|
startLine: sl,
|
|
startCol: sc,
|
|
endLine: el,
|
|
endCol: ec,
|
|
});
|
|
|
|
const callerDef: SymbolDefinition = {
|
|
nodeId: 'def:make_plain',
|
|
filePath: CALLER_FILE,
|
|
type: 'Function',
|
|
qualifiedName: 'make_plain',
|
|
};
|
|
|
|
const classIn = (filePath: string): SymbolDefinition => ({
|
|
nodeId: `def:${filePath}:Plain`,
|
|
filePath,
|
|
type: 'Class',
|
|
qualifiedName: 'Plain',
|
|
});
|
|
|
|
/** `Plain(1)` — an ordinary free call, not a `new`-form constructor site. */
|
|
const site: ReferenceSite = {
|
|
name: 'Plain',
|
|
atRange: range(3, 11),
|
|
inScope: 'scope:caller-mod',
|
|
kind: 'call',
|
|
callForm: 'free',
|
|
arity: 1,
|
|
};
|
|
|
|
function mkScope(id: ScopeId, filePath: string, ownedDefs: SymbolDefinition[]): Scope {
|
|
return {
|
|
id,
|
|
parent: null,
|
|
kind: 'Module',
|
|
range: range(1, 0, 100, 0),
|
|
filePath,
|
|
bindings: new Map(),
|
|
ownedDefs,
|
|
imports: [],
|
|
typeBindings: new Map(),
|
|
};
|
|
}
|
|
|
|
function run(classes: readonly SymbolDefinition[], bareCallConstructs: boolean) {
|
|
const callerScope: Scope = {
|
|
...mkScope('scope:caller-mod', CALLER_FILE, [callerDef]),
|
|
bindings: new Map([['Plain', classes.map((def) => ({ def, origin: 'import' as const }))]]),
|
|
};
|
|
const targetScopes = classes.map((def, i) => mkScope(`scope:target-${i}`, def.filePath, [def]));
|
|
const parsedOf = (scope: Scope, sites: readonly ReferenceSite[]): ParsedFile => ({
|
|
filePath: scope.filePath,
|
|
moduleScope: scope.id,
|
|
scopes: [scope],
|
|
parsedImports: [],
|
|
localDefs: scope.ownedDefs,
|
|
referenceSites: sites,
|
|
});
|
|
const parsedFiles = [
|
|
parsedOf(callerScope, [site]),
|
|
...targetScopes.map((scope) => parsedOf(scope, [])),
|
|
];
|
|
const scopes = [callerScope, ...targetScopes];
|
|
const allDefs = [callerDef, ...classes];
|
|
const indexes = {
|
|
scopeTree: buildScopeTree(scopes),
|
|
defs: buildDefIndex(allDefs),
|
|
qualifiedNames: buildQualifiedNameIndex(allDefs),
|
|
moduleScopes: buildModuleScopeIndex(
|
|
scopes.map((s) => ({ filePath: s.filePath, moduleScopeId: s.id })),
|
|
),
|
|
methodDispatch: buildMethodDispatchIndex({
|
|
owners: [],
|
|
computeMro: () => [],
|
|
implementsOf: () => [],
|
|
}),
|
|
imports: createEagerImportVisibility(new Map()),
|
|
bindings: new Map(),
|
|
bindingAugmentations: new Map(),
|
|
workspaceFqnBindings: new Map(),
|
|
workspaceTypeBindings: new Map(),
|
|
namespaceFqnBindings: new Map(),
|
|
namespaceTypeBindings: new Map(),
|
|
accessibleNamespacesByScope: new Map(),
|
|
referenceSites: [],
|
|
sccs: [],
|
|
stats: {
|
|
totalFiles: parsedFiles.length,
|
|
totalEdges: 0,
|
|
linkedEdges: 0,
|
|
unresolvedEdges: 0,
|
|
sccCount: 0,
|
|
largestSccSize: 0,
|
|
ambiguousWildcardExports: [],
|
|
},
|
|
} as unknown as ScopeResolutionIndexes;
|
|
const graph = createKnowledgeGraph();
|
|
graph.addNode({
|
|
id: 'fn:make_plain',
|
|
label: 'Function' as NodeLabel,
|
|
properties: { name: 'make_plain', filePath: CALLER_FILE, qualifiedName: 'make_plain' },
|
|
});
|
|
for (const def of classes) {
|
|
graph.addNode({
|
|
id: `cls:${def.filePath}:Plain`,
|
|
label: 'Class' as NodeLabel,
|
|
properties: { name: 'Plain', filePath: def.filePath, qualifiedName: 'Plain' },
|
|
});
|
|
}
|
|
const bindings: SourceBindingResolution[] = [];
|
|
emitFreeCallFallback(
|
|
graph,
|
|
indexes,
|
|
parsedFiles,
|
|
buildGraphNodeLookup(graph),
|
|
{ bySourceScope: new Map() },
|
|
new Set<string>(),
|
|
createSemanticModel(),
|
|
buildWorkspaceResolutionIndex(parsedFiles),
|
|
{
|
|
bareCallConstructs,
|
|
recordSourceBinding: (_site, binding) => bindings.push(binding),
|
|
},
|
|
);
|
|
const calls = graph.relationships
|
|
.filter((r) => r.type === 'CALLS')
|
|
.map((r) => ({ targetId: r.targetId, confidence: r.confidence, reason: r.reason }));
|
|
return { calls, finalBinding: bindings.at(-1) };
|
|
}
|
|
|
|
describe('bare-call construction links a free call to its one bound class (#3950)', () => {
|
|
it('emits no edge when the language does not construct with a bare call', () => {
|
|
const { calls, finalBinding } = run([classIn('models.py')], false);
|
|
expect(calls).toEqual([]);
|
|
expect(finalBinding).toMatchObject({ status: 'resolved', basis: 'construction' });
|
|
});
|
|
|
|
it('links the call to the Class and records a construction binding', () => {
|
|
const { calls, finalBinding } = run([classIn('models.py')], true);
|
|
expect(calls).toEqual([
|
|
{ targetId: 'cls:models.py:Plain', confidence: 0.85, reason: 'import-resolved' },
|
|
]);
|
|
expect(finalBinding).toMatchObject({
|
|
status: 'resolved',
|
|
targetDefIds: ['def:models.py:Plain'],
|
|
basis: 'construction',
|
|
});
|
|
});
|
|
|
|
it('emits no edge when the name binds two classes', () => {
|
|
const { calls, finalBinding } = run([classIn('a.py'), classIn('b.py')], true);
|
|
expect(calls).toEqual([]);
|
|
expect(finalBinding).toMatchObject({
|
|
status: 'ambiguous',
|
|
targetDefIds: ['def:a.py:Plain', 'def:b.py:Plain'],
|
|
basis: 'construction',
|
|
});
|
|
});
|
|
});
|