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

Function resolvePythonModuleMember

src/resolution/import-resolver.ts:3699–3804  ·  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

3697 * to the other strategies untouched.
3698 */
3699function resolvePythonModuleMember(
3700 ref: UnresolvedRef,
3701 imports: ImportMapping[],
3702 context: ResolutionContext
3703): ResolvedRef | null {
3704 const dotIdx = ref.referenceName.indexOf('.');
3705 if (dotIdx <= 0) return null;
3706 const receiver = ref.referenceName.substring(0, dotIdx);
3707 // The immediate member of the module (first segment after the receiver).
3708 const member = ref.referenceName.substring(dotIdx + 1).split('.')[0];
3709 if (!member) return null;
3710
3711 // `import a.b as X` inside the calling function binds X there, ahead of any
3712 // module-level binding of X, and the file's mappings don't hold it (#2600).
3713 const local = pythonFunctionModuleAlias(receiver, ref, context);
3714 if (local) {
3715 // The module from the package roots, and its member as the module binds it,
3716 // the same lookup as for a module-level binding below.
3717 const localPath = resolveImportPath(local, ref.filePath, ref.language, context);
3718 const target = localPath && localPath !== ref.filePath
3719 ? findExportedSymbol(localPath, { isDefault: false, isNamespace: false, exportedName: member, memberName: null }, 'python', context, new Set())
3720 : null;
3721 return target ? { original: ref, targetNodeId: target.id, confidence: 0.85, resolvedBy: 'import' } : null;
3722 }
3723
3724 for (const imp of imports) {
3725 // `import pkg.mod` binds `pkg`, so a call spells the whole path out —
3726 // `pkg.mod.func()`. The mapping names the module by its last segment, which
3727 // never matches that receiver; match the full dotted source instead.
3728 const spelledOut = imp.isNamespace && imp.localName === imp.source.split('.').pop()
3729 && imp.source.includes('.') && ref.referenceName.startsWith(`${imp.source}.`);
3730 if (imp.localName !== receiver && !spelledOut) continue;
3731
3732 // `import mod` / `import numpy as np` bind the module at `source` itself;
3733 // `from . import certs` / `from pkg import mod` bind a SUBMODULE whose
3734 // dotted path is the source joined with the imported name.
3735 //
3736 // Join with the EXPORTED name, not the local one: under
3737 // `from pkg import mod as alias` the receiver is `alias` but the module on
3738 // disk is `pkg.mod`, and building `pkg.alias` looked for a file that does
3739 // not exist — so the aliased form dropped its `calls` edge while the plain
3740 // form (where the two names coincide) worked (#1626). For an unaliased
3741 // import the two are identical, so this changes nothing there.
3742 const moduleName = imp.exportedName === '*' ? imp.localName : imp.exportedName;
3743 let modulePath = imp.isNamespace
3744 ? imp.source
3745 : imp.source.endsWith('.')
3746 ? imp.source + moduleName
3747 : imp.source + '.' + moduleName;
3748 const rest = ref.referenceName
3749 .substring(spelledOut ? imp.source.length + 1 : dotIdx + 1)
3750 .split('.');
3751
3752 if (!imp.isNamespace && pythonPackageBinds(imp, ref.filePath, context)) continue;
3753 let resolvedPath = resolveImportPath(modulePath, ref.filePath, ref.language, context);
3754 if (!resolvedPath || resolvedPath === ref.filePath) continue;
3755
3756 // 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