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

Function resolvePythonModuleMember

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

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

Callers 1

resolveViaImportFunction · 0.85

Calls 6

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

Tested by

no test coverage detected