Fixed a recent regression that resulted in a false positive error when a multi-part import statement was used and useLibraryCodeForTypes was disabled. This addresses https://github.com/microsoft/pyright/issues/4810.

This commit is contained in:
Eric Traut 2023-03-21 00:30:37 -06:00
parent ae873e49dd
commit 3ae66f24b0
2 changed files with 17 additions and 2 deletions

View File

@ -5197,6 +5197,12 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
}
}
// If the field was not found and the module type is marked
// such that all fields should be Any/Unknown, return that type.
if (!type && baseType.notPresentFieldType) {
type = baseType.notPresentFieldType;
}
if (!type) {
if (!isIncomplete) {
addDiagnostic(
@ -20836,8 +20842,13 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
if (lookupResults) {
moduleType.fields = lookupResults.symbolTable;
moduleType.docString = lookupResults.docString;
} else if (!loaderActions.implicitImports) {
return evaluatorOptions.evaluateUnknownImportsAsAny ? AnyType.create() : UnknownType.create();
} else {
// Note that all module attributes that are not found in the
// symbol table should be treated as Any or Unknown rather than
// as an error.
moduleType.notPresentFieldType = evaluatorOptions.evaluateUnknownImportsAsAny
? AnyType.create()
: UnknownType.create();
}
}

View File

@ -332,6 +332,10 @@ export interface ModuleType extends TypeBase {
fields: SymbolTable;
docString?: string | undefined;
// If a field lookup isn't found, should the type of the
// resulting field be Any/Unknown or treated as an error?
notPresentFieldType?: AnyType | UnknownType;
// A "loader" module includes symbols that were injected by
// the module loader. We keep these separate so we don't
// pollute the symbols exported by the module itself.