Skip to content

Commit

Permalink
Improved error messages that include module names. Previously, there …
Browse files Browse the repository at this point in the history
…were inconsistencies in how module names were reported. This addresses #5907. (#5930)

Co-authored-by: Eric Traut <erictr@microsoft.com>
  • Loading branch information
erictraut and msfterictraut authored Sep 12, 2023
1 parent f330161 commit 669a738
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 28 deletions.
13 changes: 9 additions & 4 deletions packages/pyright-internal/src/analyzer/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1934,7 +1934,7 @@ export class Binder extends ParseTreeWalker {
loadSymbolsFromPath: true,
range: getEmptyRange(),
usesLocalName: false,
moduleName: this._fileInfo.moduleName,
moduleName: this._formatModuleName(node.module.nameParts),
isInExceptSuite: this._isInExceptSuite,
};

Expand All @@ -1961,7 +1961,7 @@ export class Binder extends ParseTreeWalker {
symbolName: importedName,
submoduleFallback,
range: convertTextRangeToRange(nameNode, this._fileInfo.lines),
moduleName: this._fileInfo.moduleName,
moduleName: this._formatModuleName(node.module.nameParts),
isInExceptSuite: this._isInExceptSuite,
isNativeLib: importInfo?.isNativeLib,
};
Expand Down Expand Up @@ -2336,6 +2336,11 @@ export class Binder extends ParseTreeWalker {
return true;
}

private _formatModuleName(nameParts: NameNode[]): string {
// Ignore the leading dots for purposes of module name formatting.
return nameParts.map((name) => name.value).join('.');
}

private _removeActiveTypeParameters(node: TypeParameterListNode) {
node.parameters.forEach((typeParamNode) => {
const entry = this._activeTypeParams.get(typeParamNode.name.value);
Expand Down Expand Up @@ -2539,7 +2544,7 @@ export class Binder extends ParseTreeWalker {
loadSymbolsFromPath: false,
range: getEmptyRange(),
usesLocalName: !!importAlias,
moduleName: firstNamePartValue,
moduleName: importAlias ? this._formatModuleName(node.module.nameParts) : firstNamePartValue,
firstNamePart: firstNamePartValue,
isInExceptSuite: this._isInExceptSuite,
};
Expand All @@ -2555,7 +2560,7 @@ export class Binder extends ParseTreeWalker {
range: getEmptyRange(),
usesLocalName: !!importAlias,
moduleName: importInfo?.importName ?? '',
firstNamePart: firstNamePartValue,
firstNamePart: importAlias ? this._formatModuleName(node.module.nameParts) : firstNamePartValue,
isUnresolved: true,
isInExceptSuite: this._isInExceptSuite,
};
Expand Down
18 changes: 16 additions & 2 deletions packages/pyright-internal/src/analyzer/declarationUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,19 @@ export function resolveAliasDeclaration(
}
}

return resolveAliasDeclaration(importLookup, curDeclaration.submoduleFallback, options);
let submoduleFallback = curDeclaration.submoduleFallback;
if (curDeclaration.symbolName) {
submoduleFallback = { ...curDeclaration.submoduleFallback };
let baseModuleName = submoduleFallback.moduleName;

if (baseModuleName) {
baseModuleName = `${baseModuleName}.`;
}

submoduleFallback.moduleName = `${baseModuleName}${curDeclaration.symbolName}`;
}

return resolveAliasDeclaration(importLookup, submoduleFallback, options);
}

// If the symbol comes from a native library, we won't
Expand Down Expand Up @@ -340,6 +352,8 @@ export function resolveAliasDeclaration(
return undefined;
}

const prevDeclaration = curDeclaration;

// Prefer the last unvisited declaration in the list. This ensures that
// we use all of the overloads if it's an overloaded function.
const unvisitedDecls = declarations.filter((decl) => !alreadyVisited.includes(decl));
Expand All @@ -352,7 +366,7 @@ export function resolveAliasDeclaration(
if (lookupResult?.isInPyTypedPackage) {
if (!sawPyTypedTransition) {
if (symbol.isPrivatePyTypedImport()) {
privatePyTypedImporter = curDeclaration?.moduleName;
privatePyTypedImporter = prevDeclaration?.moduleName;
}

// Note that we've seen a transition from a non-py.typed to a py.typed
Expand Down
23 changes: 1 addition & 22 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20502,28 +20502,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (resolvedDecl.type === DeclarationType.Alias) {
// Build a module type that corresponds to the declaration and
// its associated loader actions.
let moduleName = resolvedDecl.moduleName;
if (decl.type === DeclarationType.Alias) {
if (decl.symbolName) {
moduleName += '.' + decl.symbolName;
}

// If the module name is relative to the current file, use that
// file's module name as a reference.
if (moduleName.startsWith('.')) {
const fileInfo = AnalyzerNodeInfo.getFileInfo(decl.node);
const nameParts = fileInfo.moduleName.split('.');
moduleName = moduleName.substr(1);

while (moduleName.startsWith('.') && nameParts.length > 0) {
moduleName = moduleName.substr(1);
nameParts.pop();
}

moduleName = nameParts.join('.') + '.' + moduleName;
}
}
const moduleType = ModuleType.create(moduleName, resolvedDecl.path);
const moduleType = ModuleType.create(resolvedDecl.moduleName, resolvedDecl.path);
if (resolvedDecl.symbolName && resolvedDecl.submoduleFallback) {
return applyLoaderActionsToModuleType(moduleType, resolvedDecl.submoduleFallback, importLookup);
} else {
Expand Down

0 comments on commit 669a738

Please sign in to comment.