* The file's import mappings as a bare Python name reads them (#3229). The * mappings read from-imports at any depth, so `from pkg.core import reverts` * written inside one function was the whole file's, and every other call of * `reverts` linked the import, though the file's own `def reverts` is
(ref: UnresolvedRef, imports: ImportMapping[], context: ResolutionContext)
| 4114 | * binding other than an import, and the module's own imports, change nothing. |
| 4115 | */ |
| 4116 | function pythonBareImportsInScope(ref: UnresolvedRef, imports: ImportMapping[], context: ResolutionContext): ImportMapping[] { |
| 4117 | const name = ref.referenceName; |
| 4118 | const binds = (m: ImportMapping): boolean => m.localName === name; |
| 4119 | if (!imports.some(binds)) return imports; |
| 4120 | const local = pythonFunctionImportKeys(name, ref.filePath, context); |
| 4121 | if (local.size === 0) return imports; |
| 4122 | const keyOf = (m: ImportMapping): string => (m.isNamespace ? `${m.source}:*` : `${m.source}:${m.exportedName}`); |
| 4123 | const binding = pythonReceiverBinding(name, ref, context); |
| 4124 | if (binding === 'module') { |
| 4125 | const moduleLevel = pythonModuleImportKeys(name, ref.filePath, context); |
| 4126 | return imports.filter((m) => !binds(m) || moduleLevel.has(keyOf(m)) || !local.has(keyOf(m))); |
| 4127 | } |
| 4128 | if (binding === 'local' || 'module' in binding) return imports; |
| 4129 | return imports.filter((m) => !binds(m) || (!m.isNamespace && m.source === binding.source && m.exportedName === binding.exportedName)); |
| 4130 | } |
| 4131 | |
| 4132 | /** |
| 4133 | * Resolve a Python qualified reference whose receiver is an imported MODULE: |
no test coverage detected