MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / resolvePythonModuleMember

Function resolvePythonModuleMember

src/resolution/import-resolver.ts:3900–4005  ·  view source on GitHub ↗

* Resolve a Python qualified reference whose receiver is an imported MODULE: * `certs.where()` after `from . import certs`, `mod.func()` after `import mod` * or `from pkg import mod`. The receiver names a submodule (a file), not a * symbol, so the generic symbol lookup in `resolveViaImport` can't

(
  ref: UnresolvedRef,
  imports: ImportMapping[],
  context: ResolutionContext
)

Source from the content-addressed store, hash-verified

3898 * to the other strategies untouched.
3899 */
3900function resolvePythonModuleMember(
3901 ref: UnresolvedRef,
3902 imports: ImportMapping[],
3903 context: ResolutionContext
3904): ResolvedRef | null {
3905 const dotIdx = ref.referenceName.indexOf('.');
3906 if (dotIdx <= 0) return null;
3907 const receiver = ref.referenceName.substring(0, dotIdx);
3908 // The immediate member of the module (first segment after the receiver).
3909 const member = ref.referenceName.substring(dotIdx + 1).split('.')[0];
3910 if (!member) return null;
3911
3912 // `import a.b as X` inside the calling function binds X there, ahead of any
3913 // module-level binding of X, and the file's mappings don't hold it (#2600).
3914 const local = pythonFunctionModuleAlias(receiver, ref, context);
3915 if (local) {
3916 // The module from the package roots, and its member as the module binds it,
3917 // the same lookup as for a module-level binding below.
3918 const localPath = resolveImportPath(local, ref.filePath, ref.language, context);
3919 const target = localPath && localPath !== ref.filePath
3920 ? findExportedSymbol(localPath, { isDefault: false, isNamespace: false, exportedName: member, memberName: null }, 'python', context, new Set())
3921 : null;
3922 return target ? { original: ref, targetNodeId: target.id, confidence: 0.85, resolvedBy: 'import' } : null;
3923 }
3924
3925 for (const imp of imports) {
3926 // `import pkg.mod` binds `pkg`, so a call spells the whole path out —
3927 // `pkg.mod.func()`. The mapping names the module by its last segment, which
3928 // never matches that receiver; match the full dotted source instead.
3929 const spelledOut = imp.isNamespace && imp.localName === imp.source.split('.').pop()
3930 && imp.source.includes('.') && ref.referenceName.startsWith(`${imp.source}.`);
3931 if (imp.localName !== receiver && !spelledOut) continue;
3932
3933 // `import mod` / `import numpy as np` bind the module at `source` itself;
3934 // `from . import certs` / `from pkg import mod` bind a SUBMODULE whose
3935 // dotted path is the source joined with the imported name.
3936 //
3937 // Join with the EXPORTED name, not the local one: under
3938 // `from pkg import mod as alias` the receiver is `alias` but the module on
3939 // disk is `pkg.mod`, and building `pkg.alias` looked for a file that does
3940 // not exist — so the aliased form dropped its `calls` edge while the plain
3941 // form (where the two names coincide) worked (#1626). For an unaliased
3942 // import the two are identical, so this changes nothing there.
3943 const moduleName = imp.exportedName === '*' ? imp.localName : imp.exportedName;
3944 let modulePath = imp.isNamespace
3945 ? imp.source
3946 : imp.source.endsWith('.')
3947 ? imp.source + moduleName
3948 : imp.source + '.' + moduleName;
3949 const rest = ref.referenceName
3950 .substring(spelledOut ? imp.source.length + 1 : dotIdx + 1)
3951 .split('.');
3952
3953 if (!imp.isNamespace && pythonPackageBinds(imp, ref.filePath, context)) continue;
3954 let resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context);
3955 if (!resolvedPath || resolvedPath === ref.filePath) continue;
3956
3957 // The symbol `name` as the module at `filePath` binds it: a module-level

Callers 1

resolveViaImportFunction · 0.85

Calls 5

resolveImportPathFunction · 0.85
findExportedSymbolFunction · 0.85
pythonPackageBindsFunction · 0.85
boundFunction · 0.85
getNodesInFileMethod · 0.65

Tested by

no test coverage detected